Module: SafeType
- Defined in:
- lib/safe_type.rb,
lib/safe_type/rule.rb,
lib/safe_type/errors.rb,
lib/safe_type/converter.rb,
lib/safe_type/mixin/hash.rb,
lib/safe_type/mixin/boolean.rb,
lib/safe_type/primitive/date.rb,
lib/safe_type/primitive/time.rb,
lib/safe_type/primitive/float.rb,
lib/safe_type/primitive/string.rb,
lib/safe_type/primitive/symbol.rb,
lib/safe_type/primitive/boolean.rb,
lib/safe_type/primitive/integer.rb,
lib/safe_type/primitive/date_time.rb
Defined Under Namespace
Modules: BooleanMixin, HashMixin
Classes: Boolean, CoercionError, Converter, Date, DateTime, EmptyValueError, Float, Integer, InvalidRuleError, Rule, String, Symbol, Time, ValidationError
Class Method Summary
collapse
Class Method Details
.coerce(input, rule, coerce_key = nil) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/safe_type.rb', line 15
def coerce(input, rule, coerce_key=nil)
return rule.coerce(input, coerce_key) if rule.is_a?(SafeType::Rule)
if rule.class == ::Hash
result = {}
rule.each do |key, val|
result[key] = coerce(input[key], val, key)
end
return result
end
if rule.class == ::Array
return [] if input.nil?
result = ::Array.new(input.length)
i = 0
while i < input.length
result[i] = coerce(input[i], rule[i % rule.length], i)
i += 1
end
return result
end
raise SafeType::InvalidRuleError
end
|
.coerce!(input, rule) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/safe_type.rb', line 37
def coerce!(input, rule)
if rule.class == ::Hash
rule.each do |key, val|
if val.class == ::Hash
coerce!(input[key], val)
else
input[key] = coerce(input[key], val, key)
end
end
return nil
end
if rule.class == ::Array
i = 0
while i < input.length
input[i] = coerce(input[i], rule[i % rule.length], i)
i += 1
end
return nil
end
raise SafeType::InvalidRuleError
end
|