Class: TypedModel::Validator
- Inherits:
-
Object
- Object
- TypedModel::Validator
- Defined in:
- lib/typed_model/validator.rb
Instance Attribute Summary collapse
-
#f ⇒ Object
readonly
Returns the value of attribute f.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name, &blk) ⇒ Validator
constructor
A new instance of Validator.
- #validate(value) ⇒ Object
Constructor Details
#initialize(name, &blk) ⇒ Validator
Returns a new instance of Validator.
38 39 40 41 |
# File 'lib/typed_model/validator.rb', line 38 def initialize(name, &blk) @name = name @f = blk end |
Instance Attribute Details
#f ⇒ Object (readonly)
Returns the value of attribute f.
36 37 38 |
# File 'lib/typed_model/validator.rb', line 36 def f @f end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/typed_model/validator.rb', line 36 def name @name end |
Class Method Details
.build(arg) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/typed_model/validator.rb', line 6 def build(arg) return arg if arg.respond_to?(:validate) && arg.respond_to?(:name) raise "failed to create validator from '#{arg}'" unless arg.is_a?(Symbol) raise "Unrecognized validation '#{arg}'" unless Validators.recognized?(arg) new_builtin_validator(arg) end |
.from_primitive(t) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/typed_model/validator.rb', line 13 def from_primitive(t) new(t) do |v| errors = [] if !v.nil? && (msg = send(:"assert_#{t}", v)) errors << msg end errors end end |
Instance Method Details
#validate(value) ⇒ Object
43 44 45 |
# File 'lib/typed_model/validator.rb', line 43 def validate(value) f.call(value) end |