Class: Middle::Rule
- Inherits:
-
Object
- Object
- Middle::Rule
- Defined in:
- lib/middle/rule.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#block ⇒ Object
Returns the value of attribute block.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #block_call(obj, opts = {}) ⇒ Object
- #boolean_value? ⇒ Boolean
-
#initialize(action, subject, options = {}, &block) ⇒ Rule
constructor
A new instance of Rule.
- #result(object, opts = {}) ⇒ Object
Constructor Details
#initialize(action, subject, options = {}, &block) ⇒ Rule
Returns a new instance of Rule.
7 8 9 10 11 12 13 14 |
# File 'lib/middle/rule.rb', line 7 def initialize(action, subject, = {}, &block) @action = action @subject = subject @options = @block = block @value = nil @name = "#{action}_#{subject.to_s.downcase}" end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
4 5 6 |
# File 'lib/middle/rule.rb', line 4 def action @action end |
#block ⇒ Object
Returns the value of attribute block.
4 5 6 |
# File 'lib/middle/rule.rb', line 4 def block @block end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/middle/rule.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/middle/rule.rb', line 4 def @options end |
#subject ⇒ Object
Returns the value of attribute subject.
4 5 6 |
# File 'lib/middle/rule.rb', line 4 def subject @subject end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/middle/rule.rb', line 5 def value @value end |
Instance Method Details
#block_call(obj, opts = {}) ⇒ Object
26 27 28 |
# File 'lib/middle/rule.rb', line 26 def block_call(obj, opts = {}) opts.any? ? block.call(obj, opts) : block.call(obj) end |
#boolean_value? ⇒ Boolean
30 31 32 |
# File 'lib/middle/rule.rb', line 30 def boolean_value? value.kind_of?(TrueClass) || value.kind_of?(FalseClass) end |
#result(object, opts = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/middle/rule.rb', line 16 def result(object, opts = {}) if object.respond_to?(:to_a) @value = object.to_a.all? { |obj| block_call(obj, opts) } else @value = block_call(object, opts) end fail 'Result value is not a boolean type' unless boolean_value? @value end |