Class: Inquirex::Rules::GreaterThan
- Defined in:
- lib/inquirex/rules/greater_than.rb
Overview
Rule: the answer for the given field (coerced to integer) is greater than the threshold.
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.from_h(hash) ⇒ GreaterThan
Deserializes a GreaterThan rule from a plain Hash.
Instance Method Summary collapse
-
#evaluate(answers) ⇒ Boolean
True when the field's answer, coerced to an Integer, exceeds the threshold.
-
#initialize(field, value) ⇒ GreaterThan
constructor
A new instance of GreaterThan.
-
#to_h ⇒ Hash{String => Object}
Wire format, same shape .from_h accepts.
-
#to_s ⇒ String
Human-readable form, e.g.
Constructor Details
#initialize(field, value) ⇒ GreaterThan
Returns a new instance of GreaterThan.
11 12 13 14 15 16 |
# File 'lib/inquirex/rules/greater_than.rb', line 11 def initialize(field, value) super() @field = field.to_sym @value = value freeze end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
7 8 9 |
# File 'lib/inquirex/rules/greater_than.rb', line 7 def field @field end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/inquirex/rules/greater_than.rb', line 7 def value @value end |
Class Method Details
.from_h(hash) ⇒ GreaterThan
Deserializes a GreaterThan rule from a plain Hash.
40 41 42 43 44 |
# File 'lib/inquirex/rules/greater_than.rb', line 40 def self.from_h(hash) field = hash["field"] || hash[:field] value = (hash["value"] || hash[:value]).to_i new(field, value) end |
Instance Method Details
#evaluate(answers) ⇒ Boolean
True when the field's answer, coerced to an Integer, exceeds the threshold.
22 23 24 |
# File 'lib/inquirex/rules/greater_than.rb', line 22 def evaluate(answers) answers[@field].to_i > @value end |
#to_h ⇒ Hash{String => Object}
Returns wire format, same shape .from_h accepts.
27 28 29 |
# File 'lib/inquirex/rules/greater_than.rb', line 27 def to_h { "op" => "greater_than", "field" => @field.to_s, "value" => @value } end |
#to_s ⇒ String
Returns human-readable form, e.g. "dependents > 2".
32 33 34 |
# File 'lib/inquirex/rules/greater_than.rb', line 32 def to_s "#{@field} > #{@value}" end |