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]
DEFAULT_CONFIG_NAME =
"config.yml"

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.



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

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.



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

def lazy_config_loader
  @lazy_config_loader
end

#scopesObject (readonly)

Returns the value of attribute scopes.



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

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.



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

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

#extend_object_with(object, *scopes) ⇒ Object

Raises:

  • (ArgumentError)


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

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.



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

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

#load_code(code) ⇒ Object



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

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

#load_componentsObject



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

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



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

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