module Smith::Utils

Public Class Methods

check_and_create_directory(dir) click to toggle source

Check for the existance of a directory and create if it doesn't exist. @param dir [Pathname]

# 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) click to toggle source

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

@param name [String] @return [Class] the agent class

# 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) click to toggle source

Returns a Constant based on the pathname.

# 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) click to toggle source

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

@param root [Pathname] the root path. @param clazz [String] the fully qualified class. @@return [Pathname] the path

# 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) click to toggle source

Slipts a path into it's component parts.

@param pathname [Pathname] the path to split.

# File lib/smith/utils.rb, line 51
def split_path(pathname)
  pathname.each_filename.inject([]) { |acc, p| acc << p }
end

Public Instance Methods

agent_directories(name) click to toggle source

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

@param name [String] the name of the agent. @return [Pathname] the path of the agent.

# 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

Private Instance Methods

check_and_create_directory(dir) click to toggle source

Check for the existance of a directory and create if it doesn't exist. @param dir [Pathname]

# 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) click to toggle source

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

@param name [String] @return [Class] the agent class

# 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) click to toggle source

Returns a Constant based on the pathname.

# 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) click to toggle source

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

@param root [Pathname] the root path. @param clazz [String] the fully qualified class. @@return [Pathname] the path

# 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) click to toggle source

Slipts a path into it's component parts.

@param pathname [Pathname] the path to split.

# File lib/smith/utils.rb, line 51
def split_path(pathname)
  pathname.each_filename.inject([]) { |acc, p| acc << p }
end