Class: Adhearsion::Components::ComponentManager

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/component_manager.rb

Defined Under Namespace

Classes: ComponentDefinitionContainer, ComponentMethodDefinitionContainer, LazyConfigLoader

Constant Summary collapse

SCOPE_NAMES =
[:dialplan, :events, :generators, :rpc, :global]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_container_directory) ⇒ ComponentManager

Returns a new instance of ComponentManager.



23
24
25
26
27
28
29
30
# File 'lib/adhearsion/component_manager.rb', line 23

def initialize(path_to_container_directory)
  @path_to_container_directory = path_to_container_directory
  @scopes = SCOPE_NAMES.inject({}) do |scopes, name|
    scopes[name] = Module.new
    scopes
  end
  @lazy_config_loader = LazyConfigLoader.new(self)
end

Instance Attribute Details

#lazy_config_loaderObject (readonly)

Returns the value of attribute lazy_config_loader.



22
23
24
# File 'lib/adhearsion/component_manager.rb', line 22

def lazy_config_loader
  @lazy_config_loader
end

#scopesObject (readonly)

Returns the value of attribute scopes.



22
23
24
# File 'lib/adhearsion/component_manager.rb', line 22

def scopes
  @scopes
end

Class Method Details

.scopes_valid?(*scopes) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/adhearsion/component_manager.rb', line 12

def scopes_valid?(*scopes)
  unrecognized_scopes = (scopes.flatten - SCOPE_NAMES).map(&:inspect)
  raise ArgumentError, "Unrecognized scopes #{unrecognized_scopes.to_sentence}" if unrecognized_scopes.any?
  true
end

Instance Method Details

#configuration_for_component_named(component_name) ⇒ Hash

Loads the configuration file for a given component name.

Returns:

  • (Hash)

    The loaded YAML for the given component name. An empty Hash if no YAML file exists.



61
62
63
64
65
66
67
68
69
# File 'lib/adhearsion/component_manager.rb', line 61

def configuration_for_component_named(component_name)
  component_dir = File.join(@path_to_container_directory, component_name)
  config_file = File.join component_dir, "#{component_name}.yml"
  if File.exists?(config_file)
    YAML.load_file config_file
  else
    return {}
  end
end

#extend_object_with(object, *scopes) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/adhearsion/component_manager.rb', line 71

def extend_object_with(object, *scopes)
  raise ArgumentError, "Must supply at least one scope!" if scopes.empty?
  
  self.class.scopes_valid? scopes
  
  scopes.each do |scope|
    methods = @scopes[scope]
    if object.kind_of?(Module)
      object.send :include, methods
    else
      object.extend methods
    end
  end
  object
end

#globalize_global_scope!Object

Includes the anonymous Module created for the :global scope in Object, making its methods globally accessible.



35
36
37
# File 'lib/adhearsion/component_manager.rb', line 35

def globalize_global_scope!
  Object.send :include, @scopes[:global]
end

#load_code(code) ⇒ Object



87
88
89
# File 'lib/adhearsion/component_manager.rb', line 87

def load_code(code)
  load_container ComponentDefinitionContainer.load_code(code)
end

#load_componentsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/adhearsion/component_manager.rb', line 39

def load_components
  components = Dir.glob(File.join(@path_to_container_directory + "/*")).select do |path|
    File.directory?(path)
  end
  components.map! { |path| File.basename path }
  components.each do |component|
    next if component == "disabled"
    component_file = File.join(@path_to_container_directory, component, component + ".rb")
    if File.exists? component_file
      load_file component_file
    else
      ahn_log.warn "Component directory does not contain a matching .rb file! Was expecting #{component_file.inspect}"
    end
  end
  
end

#load_file(filename) ⇒ Object



91
92
93
# File 'lib/adhearsion/component_manager.rb', line 91

def load_file(filename)
  load_container ComponentDefinitionContainer.load_file(filename)
end