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.



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

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. || ->() { {} }
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



34
35
36
# File 'lib/parametric/block_validator.rb', line 34

def message
  @message
end

Class Method Details

.build(meth, &block) ⇒ Object



3
4
5
6
7
# File 'lib/parametric/block_validator.rb', line 3

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

.coerce(&coerce_block) ⇒ Object



19
20
21
22
# File 'lib/parametric/block_validator.rb', line 19

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

.eligible(&block) ⇒ Object



24
25
26
27
# File 'lib/parametric/block_validator.rb', line 24

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

.message(&block) ⇒ Object



9
10
11
12
# File 'lib/parametric/block_validator.rb', line 9

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

.meta_data(&block) ⇒ Object



29
30
31
32
# File 'lib/parametric/block_validator.rb', line 29

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

.validate(&validate_block) ⇒ Object



14
15
16
17
# File 'lib/parametric/block_validator.rb', line 14

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

Instance Method Details

#coerce(value, key, context) ⇒ Object



50
51
52
# File 'lib/parametric/block_validator.rb', line 50

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

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

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/parametric/block_validator.rb', line 45

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

#meta_dataObject



60
61
62
# File 'lib/parametric/block_validator.rb', line 60

def 
  @meta_data_block.call *@args
end

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

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/parametric/block_validator.rb', line 54

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