Class: Parametric::BlockValidator
- Inherits:
-
Object
- Object
- Parametric::BlockValidator
- Defined in:
- lib/parametric/block_validator.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Class Method Summary collapse
- .build(meth, &block) ⇒ Object
- .coerce(&coerce_block) ⇒ Object
- .eligible(&block) ⇒ Object
- .message(&block) ⇒ Object
- .meta_data(&block) ⇒ Object
- .validate(&validate_block) ⇒ Object
Instance Method Summary collapse
- #coerce(value, key, context) ⇒ Object
- #eligible?(value, key, payload) ⇒ Boolean
-
#initialize(*args) ⇒ BlockValidator
constructor
A new instance of BlockValidator.
- #meta_data ⇒ Object
- #valid?(value, key, payload) ⇒ Boolean
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
#message ⇒ Object (readonly)
Returns the value of attribute message.
36 37 38 |
# File 'lib/parametric/block_validator.rb', line 36 def @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.(&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
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_data ⇒ Object
62 63 64 |
# File 'lib/parametric/block_validator.rb', line 62 def @meta_data_block.call *@args end |
#valid?(value, key, payload) ⇒ 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..call(*args) if self.class. @validate_block.call(*args) end |