Class: Valuedate

Inherits:
Object
  • Object
show all
Defined in:
lib/valuedate.rb

Direct Known Subclasses

OptionalValue, Value

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



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

.matchersObject (readonly)

Returns the value of attribute matchers



104
105
106
# File 'lib/valuedate.rb', line 104

def matchers
  @matchers
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors



39
40
41
# File 'lib/valuedate.rb', line 39

def errors
  @errors.uniq
end

Class Method Details

.matcher(name, &block) ⇒ Object



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

.schema(&block) ⇒ Object



114
115
116
# File 'lib/valuedate.rb', line 114

def schema(&block)
  new(&block)
end

.validate(value, &block) ⇒ Object



106
107
108
# File 'lib/valuedate.rb', line 106

def validate(value, &block)
  schema(&block).call(value)
end

.validate!(value, &block) ⇒ Object



110
111
112
# File 'lib/valuedate.rb', line 110

def validate!(value, &block)
  schema(&block).call!(value)
end

Instance Method Details

#call(value = nil) ⇒ Object 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

#call!(value = nil) ⇒ Object 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

#collect_errors!(validator, key = nil) ⇒ Object



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

#error(options = {}) ⇒ Object



87
88
89
90
# File 'lib/valuedate.rb', line 87

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

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/valuedate.rb', line 70

def valid?(&block)
  @validators << block
  self
end