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.



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

def configuration_for_component_named(component_name)
  # Look for configuration in #{AHN_ROOT}/config/components first
  if File.exists?("#{AHN_ROOT}/config/components/#{component_name}.yml")
    return YAML.load_file "#{AHN_ROOT}/config/components/#{component_name}.yml"
  end

  # Next try the local app component directory
  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
    # Nothing found? Return an empty hash
    ahn_log.warn "No configuration found for requested component #{component_name}"
    return {}
  end
end

#extend_object_with(object, *scopes) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/adhearsion/component_manager.rb', line 91

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



107
108
109
# File 'lib/adhearsion/component_manager.rb', line 107

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
55
56
57
58
59
60
61
62
63
64
65
66
# 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, 'lib', component + ".rb")
    if File.exists? component_file
      load_file component_file
      next
    end

    # Try the old-style components/<component>/<component>.rb
    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

  # Load configured system- or gem-provided components
  AHN_CONFIG.components_to_load.each do |component|
    require component
  end

end

#load_file(filename) ⇒ Object



111
112
113
# File 'lib/adhearsion/component_manager.rb', line 111

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

#require(filename) ⇒ Object



115
116
117
# File 'lib/adhearsion/component_manager.rb', line 115

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