Class: PyrRules::Action
- Inherits:
-
Object
- Object
- PyrRules::Action
- Includes:
- Mongoid::Document
- Defined in:
- app/models/pyr_rules/action.rb
Class Method Summary collapse
-
.add_mapping_type_equivalency(target_type, equivalent_types) ⇒ Object
A mapping equivalency is used to indicate two types can be mapped to each other, even though they do not have the same exact name.
- .check_mapping_type?(action_type, rule_type) ⇒ Boolean
Instance Method Summary collapse
-
#lookup_context_mapping(field, type = "string") ⇒ Object
returns an array [field,type].
- #needs ⇒ Object
-
#ready? ⇒ Boolean
An action is ready if all of it’s needs are mapped.
- #set_default_mappings(rule_context = rule.context) ⇒ Object
- #template_body(template_name) ⇒ Object
-
#template_configuration ⇒ Object
What templates are defined for this Action’s ActionHandler? This reads from template DSL inside of ActionHandler class Templates have to be defined through the DSL!!!.
-
#template_fields(template_name) ⇒ Object
Extract the template fields for the provided template.
- #template_names ⇒ Object
Class Method Details
.add_mapping_type_equivalency(target_type, equivalent_types) ⇒ Object
A mapping equivalency is used to indicate two types can be mapped to each other, even though
they do not have the same exact name
87 88 89 |
# File 'app/models/pyr_rules/action.rb', line 87 def self.add_mapping_type_equivalency(target_type, equivalent_types) @@context_mapping_equivalencies[target_type] = equivalent_types end |
.check_mapping_type?(action_type, rule_type) ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'app/models/pyr_rules/action.rb', line 91 def self.check_mapping_type?(action_type, rule_type) return true if action_type == rule_type || action_type == "string" equivalencies = @@context_mapping_equivalencies[action_type.to_sym] if equivalencies && equivalencies.index(rule_type.to_sym) return true end false end |
Instance Method Details
#lookup_context_mapping(field, type = "string") ⇒ Object
returns an array [field,type]
70 71 72 |
# File 'app/models/pyr_rules/action.rb', line 70 def lookup_context_mapping(field, type = "string") context_mapping["#{field}:#{type}"].split(":") rescue nil end |
#needs ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/pyr_rules/action.rb', line 23 def needs n = (type.constantize.configuration[:needs].dup rescue {}) || {} class_lookup_cols = {} n.each do |field,type| if type == :class_lookup klazz = self.lookup_context_mapping(field,:class_lookup)[0] rescue nil class_lookup_cols.merge! PyrRules::Rule.get_columns(klazz.camelcase.constantize) if klazz end end class_lookup_cols.each do |column, col_type| n[column] = col_type end template_names.each do |name| template_fields(name).each do |f| n[f.to_sym] = :string end end n end |
#ready? ⇒ Boolean
An action is ready if all of it’s needs are mapped
16 17 18 19 20 21 |
# File 'app/models/pyr_rules/action.rb', line 16 def ready? needs.each do |field, type| return false unless lookup_context_mapping(field,type) end true end |
#set_default_mappings(rule_context = rule.context) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/pyr_rules/action.rb', line 74 def set_default_mappings(rule_context = rule.context) needs.each do |action_field, type| puts "...Considering mapping for #{action_field}(#{type})" rule_context.keys.each do |rule_field| unless try_rule_mapping(rule_context,rule_field,rule_field,action_field,type) try_rule_mapping(rule_context,rule_field,rule_field.split(".").last.to_sym,action_field,type) end end end end |
#template_body(template_name) ⇒ Object
55 56 57 58 |
# File 'app/models/pyr_rules/action.rb', line 55 def template_body(template_name) body = template[template_name.to_s] # First try to read from Mongo Document template Hash body ||= template_configuration[template_name.to_sym] # Not found, use the default configuration from the ActionHandler DSL end |
#template_configuration ⇒ Object
What templates are defined for this Action’s ActionHandler? This reads from template DSL inside of ActionHandler class Templates have to be defined through the DSL!!!
46 47 48 |
# File 'app/models/pyr_rules/action.rb', line 46 def template_configuration (type.constantize.configuration[:templates] rescue {}) || {} end |
#template_fields(template_name) ⇒ Object
Extract the template fields for the provided template
eg: body = “Hello name” - the template field is :name
64 65 66 67 |
# File 'app/models/pyr_rules/action.rb', line 64 def template_fields(template_name) body = template_body(template_name) body.scan(/\{([^\s]*)\}/).flatten rescue [] end |
#template_names ⇒ Object
51 52 53 |
# File 'app/models/pyr_rules/action.rb', line 51 def template_names template_configuration.keys rescue [] end |