Class: Parametric::BlockValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/parametric/block_validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BlockValidator

Returns a new instance of BlockValidator.



38
39
40
41
42
43
44
45
# File 'lib/parametric/block_validator.rb', line 38

def initialize(*args)
  @args = args
  @message = 'is invalid'
  @validate_block = self.class.validate || ->(*args) { true }
  @coerce_block = self.class.coerce || ->(v, *_) { v }
  @eligible_block = self.class.eligible || ->(*args) { true }
  @meta_data_block = self.class. || ->(*args) { {} }
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



36
37
38
# File 'lib/parametric/block_validator.rb', line 36

def message
  @message
end

Class Method Details

.build(meth, &block) ⇒ Object



5
6
7
8
9
# File 'lib/parametric/block_validator.rb', line 5

def self.build(meth, &block)
  klass = Class.new(self)
  klass.public_send(meth, &block)
  klass
end

.coerce(&coerce_block) ⇒ Object



21
22
23
24
# File 'lib/parametric/block_validator.rb', line 21

def self.coerce(&coerce_block)
  @coerce_block = coerce_block if block_given?
  @coerce_block
end

.eligible(&block) ⇒ Object



26
27
28
29
# File 'lib/parametric/block_validator.rb', line 26

def self.eligible(&block)
  @eligible_block = block if block_given?
  @eligible_block if instance_variable_defined?('@eligible_block')
end

.message(&block) ⇒ Object



11
12
13
14
# File 'lib/parametric/block_validator.rb', line 11

def self.message(&block)
  @message_block = block if block_given?
  @message_block if instance_variable_defined?('@message_block')
end

.meta_data(&block) ⇒ Object



31
32
33
34
# File 'lib/parametric/block_validator.rb', line 31

def self.(&block)
  @meta_data_block = block if block_given?
  @meta_data_block if instance_variable_defined?('@meta_data_block')
end

.validate(&validate_block) ⇒ Object



16
17
18
19
# File 'lib/parametric/block_validator.rb', line 16

def self.validate(&validate_block)
  @validate_block = validate_block if block_given?
  @validate_block if instance_variable_defined?('@validate_block')
end

Instance Method Details

#coerce(value, key, context) ⇒ Object



52
53
54
# File 'lib/parametric/block_validator.rb', line 52

def coerce(value, key, context)
  @coerce_block.call(value, key, context)
end

#eligible?(value, key, payload) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/parametric/block_validator.rb', line 47

def eligible?(value, key, payload)
  args = (@args + [value, key, payload])
  @eligible_block.call(*args)
end

#meta_dataObject



62
63
64
# File 'lib/parametric/block_validator.rb', line 62

def 
  @meta_data_block.call *@args
end

#valid?(value, key, payload) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/parametric/block_validator.rb', line 56

def valid?(value, key, payload)
  args = (@args + [value, key, payload])
  @message = self.class.message.call(*args) if self.class.message
  @validate_block.call(*args)
end