Class: Middle::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/middle/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, subject, options = {}, &block) ⇒ Rule

Returns a new instance of Rule.

[View source]

7
8
9
10
11
12
13
14
# File 'lib/middle/rule.rb', line 7

def initialize(action, subject, options = {}, &block)
  @action     = action
  @subject    = subject
  @options    = options
  @block      = block
  @value      = nil
  @name       = "#{action}_#{subject.to_s.downcase}"
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.


4
5
6
# File 'lib/middle/rule.rb', line 4

def action
  @action
end

#blockObject

Returns the value of attribute block.


4
5
6
# File 'lib/middle/rule.rb', line 4

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.


5
6
7
# File 'lib/middle/rule.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.


4
5
6
# File 'lib/middle/rule.rb', line 4

def options
  @options
end

#subjectObject

Returns the value of attribute subject.


4
5
6
# File 'lib/middle/rule.rb', line 4

def subject
  @subject
end

#valueObject (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

[View source]

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

Returns:

  • (Boolean)
[View source]

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

[View source]

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