Class: Adhearsion::Components::ComponentManager::ComponentDefinitionContainer
Class Method Summary collapse
- .get_gem_path_for(filename) ⇒ Object
- .get_system_path_for(filename) ⇒ Object
- .load_code(code) ⇒ Object
- .load_file(filename) ⇒ Object
- .method_added(method_name) ⇒ Object
- .require(filename) ⇒ Object
Instance Method Summary collapse
- #initialization(&block) ⇒ Object (also: #initialisation)
-
#initialize(&block) ⇒ ComponentDefinitionContainer
constructor
A new instance of ComponentDefinitionContainer.
- #methods_for(*scopes, &block) ⇒ Object
Methods inherited from Module
Constructor Details
#initialize(&block) ⇒ ComponentDefinitionContainer
Returns a new instance of ComponentDefinitionContainer.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/adhearsion/component_manager.rb', line 176 def initialize(&block) # Hide our instance variables in the singleton class = {} .send(:instance_variable_set, :@metadata, ) [:scopes] = ComponentManager::SCOPE_NAMES.inject({}) do |scopes, name| scopes[name] = [] scopes end super (:initialize) { raise "This object has already been instantiated. Are you sure you didn't mean initialization()?" } end |
Class Method Details
.get_gem_path_for(filename) ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/adhearsion/component_manager.rb', line 219 def get_gem_path_for(filename) # Look for component files provided by rubygems spec = Gem.searcher.find(filename) return nil if spec.nil? File.join(spec.full_gem_path, spec.require_path, filename) rescue NameError # In case Rubygems are not available nil end |
.get_system_path_for(filename) ⇒ Object
229 230 231 232 233 234 235 236 237 |
# File 'lib/adhearsion/component_manager.rb', line 229 def get_system_path_for(filename) $:.each do |path| filepath = File.join(path, filename) return filepath if File.exists?(filepath) end # Not found? Return nil return nil end |
.load_code(code) ⇒ Object
142 143 144 145 146 |
# File 'lib/adhearsion/component_manager.rb', line 142 def load_code(code) new.tap do |instance| instance.module_eval code end end |
.load_file(filename) ⇒ Object
148 149 150 151 152 |
# File 'lib/adhearsion/component_manager.rb', line 148 def load_file(filename) new.tap do |instance| instance.module_eval File.read(filename), filename end end |
.method_added(method_name) ⇒ Object
214 215 216 217 |
# File 'lib/adhearsion/component_manager.rb', line 214 def self.method_added(method_name) @methods ||= [] @methods << method_name end |
.require(filename) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/adhearsion/component_manager.rb', line 154 def require(filename) filename = filename + ".rb" if !(filename =~ /\.rb$/) begin # Try loading the exact filename first load_file(filename) rescue LoadError, Errno::ENOENT end # Next try Rubygems filepath = get_gem_path_for(filename) return load_file(filepath) if !filepath.nil? # Finally try the system search path filepath = get_system_path_for(filename) return load_file(filepath) if !filepath.nil? # Raise a LoadError exception if the file is still not found raise LoadError, "File not found: #{filename}" end |
Instance Method Details
#initialization(&block) ⇒ Object Also known as: initialisation
200 201 202 203 204 205 206 207 208 |
# File 'lib/adhearsion/component_manager.rb', line 200 def initialization(&block) # Raise an exception if the initialization block has already been set = .send(:instance_variable_get, :@metadata) if [:initialization_block] raise "You should only have one initialization() block!" else [:initialization_block] = block end end |
#methods_for(*scopes, &block) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/adhearsion/component_manager.rb', line 191 def methods_for(*scopes, &block) raise ArgumentError if scopes.empty? ComponentManager.scopes_valid? scopes = .send(:instance_variable_get, :@metadata) scopes.each { |scope| [:scopes][scope] << block } end |