Module: Smith::Utils

Included in:
ACL::Factory, AgentBootstrap, AgentProcess
Defined in:
lib/smith/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_and_create_directory(dir) ⇒ Object

Check for the existance of a directory and create if it doesn’t exist.

Parameters:

  • dir (Pathname)


58
59
60
61
62
# File 'lib/smith/utils.rb', line 58

def check_and_create_directory(dir)
  dir.tap do
    dir.exist? || dir.mkpath
  end
end

.class_from_name(name) ⇒ Class

Performs a Kernel.const_get on each element of the class.

Parameters:

  • name (String)

Returns:

  • (Class)

    the agent class



42
43
44
# File 'lib/smith/utils.rb', line 42

def class_from_name(name)
  name.to_s.split(/::/).inject(Kernel) { |acc, t| acc.const_get(t) }
end

.class_name_from_path(path, root = Pathname.new('.'), segment_to_remove = nil) ⇒ Object

Returns a Constant based on the pathname.



30
31
32
33
34
35
# File 'lib/smith/utils.rb', line 30

def class_name_from_path(path, root=Pathname.new('.'), segment_to_remove=nil)
  relative_path = path.relative_path_from(root)
  parts = split_path(relative_path.sub_ext('')).reject { |p| p == segment_to_remove }

  parts.map { |p| p.to_s.camel_case }.join('::')
end

.path_from_class(root, clazz) ⇒ Object

Constructs a path from a root and a fully qualified class.

@@return [Pathname] the path

Parameters:

  • root (Pathname)

    the root path.

  • clazz (String)

    the fully qualified class.



22
23
24
25
26
# File 'lib/smith/utils.rb', line 22

def path_from_class(root, clazz)
  parts = clazz.split(/::/).map(&:snake_case)
  parts[-1] = "#{parts[-1]}.rb"
  Pathname.new(root).join(*parts)
end

.split_path(pathname) ⇒ Object

Slipts a path into it’s component parts.

Parameters:

  • pathname (Pathname)

    the path to split.



51
52
53
# File 'lib/smith/utils.rb', line 51

def split_path(pathname)
  pathname.each_filename.inject([]) { |acc, p| acc << p }
end

Instance Method Details

#agent_directories(name) ⇒ Pathname

Searches the agent load path for agents. If there are multiple agents with the same name in different directories the first wins.

Parameters:

  • name (String)

    the name of the agent.

Returns:

  • (Pathname)

    the path of the agent.



9
10
11
12
13
14
15
# File 'lib/smith/utils.rb', line 9

def agent_directories(name)
  Smith.agent_directories.each do |path|
    p = path_from_class(path, name)
    return p if p.exist?
  end
  return nil
end