Class: Aequitas::Rule::Block

Inherits:
Aequitas::Rule show all
Defined in:
lib/aequitas/rule/block.rb

Instance Attribute Summary collapse

Attributes inherited from Aequitas::Rule

#attribute_name, #custom_message, #guard, #skip_condition

Attributes included from ValueObject

#equalizer

Instance Method Summary collapse

Methods inherited from Aequitas::Rule

#attribute_value, #execute?, rules_for, #skip?, #violation_data, #violation_info, #violation_values

Methods included from ValueObject

#equalize_on

Constructor Details

#initialize(attribute_name, options, &block) ⇒ Block

Returns a new instance of Block.



11
12
13
14
15
16
17
18
19
20
# File 'lib/aequitas/rule/block.rb', line 11

def initialize(attribute_name, options, &block)
  unless block_given?
    raise ArgumentError, 'cannot initialize a Block validator without a block'
  end

  super

  @block = block
  @violation_type = options.fetch(:violation_type, :unsatisfied_condition)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/aequitas/rule/block.rb', line 9

def block
  @block
end

Instance Method Details

#validate(resource) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/aequitas/rule/block.rb', line 22

def validate(resource)
  result, error_message = resource.instance_eval(&self.block)

  if result
    nil
  else
    Violation.new(resource, error_message, self)
  end
end

#violation_type(resource) ⇒ Object



32
33
34
# File 'lib/aequitas/rule/block.rb', line 32

def violation_type(resource)
  @violation_type
end