Module: BluePrint::Behavior
- Defined in:
- lib/blue_print/behavior.rb
Class Method Summary collapse
Instance Method Summary collapse
- #behavior_name ⇒ Object
- #context ⇒ Object
- #context_name ⇒ Object
- #define_safe_method(target, punctuation, method_name) ⇒ Object
- #method_added(method_name) ⇒ Object
- #override_methods ⇒ Object
- #prepended(base) ⇒ Object
Class Method Details
.auto_generate_class_methods?(base) ⇒ Boolean
7 8 9 |
# File 'lib/blue_print/behavior.rb', line 7 def self.auto_generate_class_methods?(base) base.is_a?(Module) && !base.name.match(/ClassMethods$/) end |
.extended(base) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/blue_print/behavior.rb', line 11 def self.extended(base) if auto_generate_class_methods?(base) base.module_eval <<-EOC module ClassMethods extend BluePrint::Behavior end EOC end end |
Instance Method Details
#behavior_name ⇒ Object
41 42 43 44 |
# File 'lib/blue_print/behavior.rb', line 41 def behavior_name @behavior_name ||= name.to_s.underscore.sub(/\/class_methods$/, '').gsub('/', '__').to_sym end |
#context ⇒ Object
37 38 39 |
# File 'lib/blue_print/behavior.rb', line 37 def context context_name.to_s.camelize.safe_constantize end |
#context_name ⇒ Object
30 31 32 33 34 35 |
# File 'lib/blue_print/behavior.rb', line 30 def context_name @context_name ||= name.to_s.underscore.tap do |name| name.sub!(/\/class_methods$/, '') name.sub!(%r{/[^/]+?$}, '') end.to_sym end |
#define_safe_method(target, punctuation, method_name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/blue_print/behavior.rb', line 50 def define_safe_method(target, punctuation, method_name) alias_method("#{target}_with_#{behavior_name}#{punctuation}", method_name) remove_method(method_name) module_eval <<-EOC def #{method_name}(*args) if #{context}.active? #{target}_with_#{behavior_name}#{punctuation}(*args) else super(*args) end end EOC end |
#method_added(method_name) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/blue_print/behavior.rb', line 65 def method_added(method_name) return if @ignore_added_hook override_methods.push(method_name) @ignore_added_hook = true aliased_target = method_name.to_s.sub(/([?!=])$/, '') punctuation = Regexp.last_match ? Regexp.last_match[1] : '' define_safe_method(aliased_target, punctuation, method_name) context.try(:reaction!) @ignore_added_hook = false end |
#override_methods ⇒ Object
46 47 48 |
# File 'lib/blue_print/behavior.rb', line 46 def override_methods @override_methods ||= [] end |