Class: ActiveBehavior::Loader
- Inherits:
-
Object
- Object
- ActiveBehavior::Loader
- Defined in:
- lib/active-behavior/loader.rb
Class Method Summary collapse
- .define(hash = self.imports) ⇒ Object
- .define_behavior(name) ⇒ Object
- .import ⇒ Object
- .imports ⇒ Object
- .imports=(value) ⇒ Object
- .load ⇒ Object
- .loaded? ⇒ Boolean
- .require_files ⇒ Object
- .requires ⇒ Object
- .requires=(value) ⇒ Object
Class Method Details
.define(hash = self.imports) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active-behavior/loader.rb', line 63 def self.define(hash = self.imports) hash.each do |key, value| name = "::#{key.split(".").map(&:camelize).join("::")}" define_behavior(name) value.keys.each do |child_key| value["#{key}.#{child_key}"] = value.delete(child_key) end define value if value.is_a?(Hash) end end |
.define_behavior(name) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active-behavior/loader.rb', line 75 def self.define_behavior(name) begin define_module(name).class_eval %{ unloadable module ClassMethods; unloadable; end module InstanceMethods; unloadable; end } rescue Exception => e puts e.inspect end end |
.import ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active-behavior/loader.rb', line 45 def self.import # http://snippets.dzone.com/posts/show/12537 behavior_dir = "#{Rails.root}/app/behaviors" self.requires = [] self.imports = Dir["#{behavior_dir}/**/*"].sort.inject({}) do |hash, file| if File.extname(file) == ".rb" self.requires << file tree = hash nodes = file.gsub("#{behavior_dir}/", "").gsub(/\.rb$/, "").split("/") nodes.each do |node| tree[node] ||= {} tree = tree[node] end end hash end end |
.imports ⇒ Object
29 30 31 |
# File 'lib/active-behavior/loader.rb', line 29 def self.imports @imports end |
.imports=(value) ⇒ Object
33 34 35 |
# File 'lib/active-behavior/loader.rb', line 33 def self.imports=(value) @imports = value end |
.load ⇒ Object
93 94 95 96 97 |
# File 'lib/active-behavior/loader.rb', line 93 def self.load import define ENV["BEHAVIOR_LOADED"] = "true" end |
.loaded? ⇒ Boolean
99 100 101 |
# File 'lib/active-behavior/loader.rb', line 99 def self.loaded? ENV.has_key?("BEHAVIOR_LOADED") && ENV["BEHAVIOR_LOADED"] == "true" end |
.require_files ⇒ Object
87 88 89 90 91 |
# File 'lib/active-behavior/loader.rb', line 87 def self.require_files self.requires.each do |file| require file end end |
.requires ⇒ Object
41 42 43 |
# File 'lib/active-behavior/loader.rb', line 41 def self.requires @requires end |
.requires=(value) ⇒ Object
37 38 39 |
# File 'lib/active-behavior/loader.rb', line 37 def self.requires=(value) @requires = value end |