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
#initialize(&block) ⇒ Valuedate
Returns 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
#method_missing(method, *args, &block) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/valuedate.rb', line 94
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
.matchers ⇒ Object
Returns the value of attribute matchers.
106
107
108
|
# File 'lib/valuedate.rb', line 106
def matchers
@matchers
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
39
40
41
|
# File 'lib/valuedate.rb', line 39
def errors
@errors
end
|
Class Method Details
.matcher(name, &block) ⇒ Object
116
117
118
119
|
# File 'lib/valuedate.rb', line 116
def matcher(name, &block)
undef_method(name) if respond_to?(name)
@matchers[name] = block
end
|
.schema(&block) ⇒ Object
112
113
114
|
# File 'lib/valuedate.rb', line 112
def schema(&block)
new(&block)
end
|
.validate(value, &block) ⇒ Object
108
109
110
|
# File 'lib/valuedate.rb', line 108
def validate(value, &block)
schema(&block).validate(value)
end
|
Instance Method Details
#call(value) ⇒ Object
68
69
70
|
# File 'lib/valuedate.rb', line 68
def call(value)
validate(value)
end
|
#collect_errors!(validator, key = nil) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/valuedate.rb', line 77
def collect_errors!(validator, key=nil)
case validator
when Valuedate
@errors.concat(validator.errors.map { |error| error.options[:key] ||= key; error })
end
false
end
|
#error(options = {}) ⇒ Object
89
90
91
92
|
# File 'lib/valuedate.rb', line 89
def error(options={})
@errors << Error.new(options)
false
end
|
#hash(schema = {}) ⇒ Object
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
|
#valid?(&block) ⇒ Boolean
72
73
74
75
|
# File 'lib/valuedate.rb', line 72
def valid?(&block)
@validators << block
self
end
|
#validate(value = nil) ⇒ Object
59
60
61
62
|
# File 'lib/valuedate.rb', line 59
def validate(value = nil)
@errors.clear
@validators.all? { |validator| validator.call(value) || collect_errors!(validator) }
end
|
#validate!(value = nil) ⇒ Object
64
65
66
|
# File 'lib/valuedate.rb', line 64
def validate!(value = nil)
validate(value) or raise ValidationFailed.new(@errors)
end
|