Class: Valuedate
- Inherits:
-
Object
show all
- Defined in:
- lib/valuedate.rb
Defined Under Namespace
Classes: Error, OptionalValue, Scope, ValidationFailed, Value
Class Attribute Summary (collapse)
Instance Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Constructor Details
- (Valuedate) initialize(&block)
A new instance of Valuedate
41
42
43
44
45
46
47
48
|
# File 'lib/valuedate.rb', line 41
def initialize(&block)
@errors = []
@validators = []
if block
validator = Scope.new.instance_eval(&block)
@validators << validator if validator.respond_to?(:call)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args, &block)
92
93
94
95
96
97
98
99
100
|
# File 'lib/valuedate.rb', line 92
def method_missing(method, *args, &block)
if matcher = Valuedate.matchers[method]
valid? do |value|
matcher.call(value, *args, &block) || error(:matcher => method, :value => value, :args => args)
end
else
super
end
end
|
Class Attribute Details
+ (Object) matchers
Returns the value of attribute matchers
104
105
106
|
# File 'lib/valuedate.rb', line 104
def matchers
@matchers
end
|
Instance Attribute Details
- (Object) errors
Returns the value of attribute errors
39
40
41
|
# File 'lib/valuedate.rb', line 39
def errors
@errors.uniq
end
|
Class Method Details
+ (Object) matcher(name, &block)
118
119
120
121
|
# File 'lib/valuedate.rb', line 118
def matcher(name, &block)
undef_method(name) if respond_to?(name)
@matchers[name] = block
end
|
+ (Object) schema(&block)
114
115
116
|
# File 'lib/valuedate.rb', line 114
def schema(&block)
new(&block)
end
|
+ (Object) validate(value, &block)
106
107
108
|
# File 'lib/valuedate.rb', line 106
def validate(value, &block)
schema(&block).call(value)
end
|
+ (Object) validate!(value, &block)
110
111
112
|
# File 'lib/valuedate.rb', line 110
def validate!(value, &block)
schema(&block).call!(value)
end
|
Instance Method Details
- (Object) call(value = nil)
Also known as:
validate
59
60
61
62
|
# File 'lib/valuedate.rb', line 59
def call(value = nil)
@errors.clear
@validators.all? { |validator| validator.call(value) || collect_errors!(validator) }
end
|
- (Object) call!(value = nil)
Also known as:
validate!
65
66
67
|
# File 'lib/valuedate.rb', line 65
def call!(value = nil)
call(value) or raise ValidationFailed.new(@errors)
end
|
- (Object) collect_errors!(validator, key = nil)
75
76
77
78
79
80
81
|
# File 'lib/valuedate.rb', line 75
def collect_errors!(validator, key=nil)
case validator
when Valuedate
@errors.concat(validator.errors.map { |error| error.options[:key] ||= key; error })
end
false
end
|
- (Object) error(options = {})
87
88
89
90
|
# File 'lib/valuedate.rb', line 87
def error(options={})
@errors << Error.new(options)
false
end
|
- (Object) hash(schema = {})
50
51
52
53
54
55
56
57
|
# File 'lib/valuedate.rb', line 50
def hash(schema={})
valid? do |value|
value ||= {}
schema.all? do |(key, validator)|
validator.call(value[key]) || collect_errors!(validator, key)
end
end
end
|
- (Boolean) valid?(&block)
70
71
72
73
|
# File 'lib/valuedate.rb', line 70
def valid?(&block)
@validators << block
self
end
|