Class: HungryForm::Resolver
- Inherits:
-
Object
- Object
- HungryForm::Resolver
- Defined in:
- lib/hungryform/resolver.rb
Overview
The class is responsible for dependency resolving. It contains all form elements and params
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
-
#get_value(name, element = nil) ⇒ Object
Gets element value by element’s name.
-
#initialize(options = {}) ⇒ Resolver
constructor
A new instance of Resolver.
-
#resolve_dependency(dependency) ⇒ Object
Gets dependency rules hash and returns true or false depending on the result of a recursive processing of the rules.
Constructor Details
#initialize(options = {}) ⇒ Resolver
Returns a new instance of Resolver.
7 8 9 10 11 |
# File 'lib/hungryform/resolver.rb', line 7 def initialize( = {}) self.params = ActiveSupport::HashWithIndifferentAccess.new([:params] || {}) self.elements = {} end |
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
5 6 7 |
# File 'lib/hungryform/resolver.rb', line 5 def elements @elements end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/hungryform/resolver.rb', line 5 def params @params end |
Instance Method Details
#get_value(name, element = nil) ⇒ Object
Gets element value by element’s name. If name is lambda - returns lambda’s result If name is present in the resolvers’ elements hash - returns element’s value If name is present in the resolvers’ params hash - returns params value Otherwise returns the argument without changes
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hungryform/resolver.rb', line 18 def get_value(name, element = nil) return name.call(element) if name.respond_to? :call # We don't want to mess up elements names name = name.to_s.dup # Apply placeholders to the name. # A sample name string can look like this: page1_group[GROUP_NUMBER]_field # where [GROUP_NUMBER] is a placeholder. When an element is present # we get its placeholders and replace substrings in the name argument element.placeholders.each { |k, v| name[k] &&= v } if element elements[name].try(:value) || params[name] || name end |
#resolve_dependency(dependency) ⇒ Object
Gets dependency rules hash and returns true or false depending on the result of a recursive processing of the rules
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hungryform/resolver.rb', line 35 def resolve_dependency(dependency) dependency.each do |operator, arguments| operator = operator.to_sym case operator when :and, :or return resolve_multi_dependency(operator, arguments) when :not return !resolve_dependency(arguments) end arguments = [arguments] unless arguments.is_a?(Array) values = arguments[0..1].map { |name| get_value(name) } return false if values.any?(&:nil?) case operator when :eq return values[0].to_s == values[1].to_s when :lt return values[0].to_f < values[1].to_f when :gt return values[0].to_f > values[1].to_f when :set return !values[0].empty? end end end |