Class: PyrRules::ActionHandler
- Inherits:
-
Object
- Object
- PyrRules::ActionHandler
- Defined in:
- app/rules/pyr_rules/action_handler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#event ⇒ Object
Returns the value of attribute event.
Class Method Summary collapse
- ._add_needs_and_recurse(klazz) ⇒ Object
- .action_listing ⇒ Object
-
.configuration ⇒ Object
def self.templates @@templates end.
- .needs(name, type = :string) ⇒ Object
- .reload_configuration ⇒ Object
-
.template(name) ⇒ Object
def self.needs_map @@needs end.
Instance Method Summary collapse
- #eval_template(template_name) ⇒ Object
- #handle ⇒ Object
-
#initialize(event, action) ⇒ ActionHandler
constructor
A new instance of ActionHandler.
-
#method_missing(meth, *args, &block) ⇒ Object
Our context methods will be accessible through here.
Constructor Details
#initialize(event, action) ⇒ ActionHandler
Returns a new instance of ActionHandler.
73 74 75 76 77 |
# File 'app/rules/pyr_rules/action_handler.rb', line 73 def initialize(event, action) @event = event @action = action # Allow use of symbols or strings as keys target = event_hash[:klazz].constantize.find event_hash[:id].to_i rescue nil @context = PyrRules::Rule.rule_context(@event) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Our context methods will be accessible through here
61 62 63 64 65 66 67 68 69 |
# File 'app/rules/pyr_rules/action_handler.rb', line 61 def method_missing(meth, *args, &block) mapping_type = @action.needs[meth] if mapping_type rule_field = @action.lookup_context_mapping(meth, mapping_type)[0] value = eval rule_field, @context.get_binding rescue nil return value end super end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
72 73 74 |
# File 'app/rules/pyr_rules/action_handler.rb', line 72 def action @action end |
#event ⇒ Object
Returns the value of attribute event.
72 73 74 |
# File 'app/rules/pyr_rules/action_handler.rb', line 72 def event @event end |
Class Method Details
._add_needs_and_recurse(klazz) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/rules/pyr_rules/action_handler.rb', line 51 def self._add_needs_and_recurse(klazz) klazz.subclasses.each do |c| @@action_configuration[c] ||= {} @@action_configuration[c][:needs] = @@needs[c] @@action_configuration[c][:templates] = @@templates[c] _add_needs_and_recurse(c) end end |
.action_listing ⇒ Object
46 47 48 49 |
# File 'app/rules/pyr_rules/action_handler.rb', line 46 def self.action_listing configuration # force load @@action_configuration.keys end |
.configuration ⇒ Object
def self.templates
@@templates[self]
end
34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/rules/pyr_rules/action_handler.rb', line 34 def self.configuration return @@action_configuration[self] unless @@action_configuration.empty? #if Rails.env.development? Dir["#{Rails.root}/../**/*/app/**/action_handler/*.rb"].each do |p| puts "require_dependency #{p}" require_dependency p end #end _add_needs_and_recurse(PyrRules::ActionHandler) @@action_configuration[self] end |
.needs(name, type = :string) ⇒ Object
10 11 12 13 |
# File 'app/rules/pyr_rules/action_handler.rb', line 10 def self.needs(name, type=:string) @@needs[self] ||= {} @@needs[self][name] = type end |
.reload_configuration ⇒ Object
24 25 26 27 28 29 |
# File 'app/rules/pyr_rules/action_handler.rb', line 24 def self.reload_configuration puts "Reloading ActionHandler configuration" @@action_configuration = {} configuration @@action_configuration.keys end |
.template(name) ⇒ Object
def self.needs_map
@@needs[self]
end
19 20 21 22 |
# File 'app/rules/pyr_rules/action_handler.rb', line 19 def self.template(name) @@templates[self] ||= {} @@templates[self][name] = (block_given?) ? yield : "" end |
Instance Method Details
#eval_template(template_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/rules/pyr_rules/action_handler.rb', line 83 def eval_template(template_name) template_name = template_name.to_s puts "DEBUG: Looking up #{template_name} from #{@action.template}" t = @action.template_body(template_name) interpolated_fields = @action.template_fields(template_name) interpolated_fields.each do |f| t.gsub!("{#{f}}","\#{#{f}}") end puts "DEBUG: Using #{t}" eval '"' + t + '"' end |
#handle ⇒ Object
79 80 81 |
# File 'app/rules/pyr_rules/action_handler.rb', line 79 def handle puts "\n\n------- #{self.class.name} ------ \n\nHANDLE called for #{@event.inspect} \n\n---- #{@action.inspect}\n\n" end |