data_steroid 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/data_steroid/properties.rb +55 -9
- data/lib/data_steroid/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1ea709eb2675cbdd294ab7d4e55977e918488d0
|
4
|
+
data.tar.gz: 3eec8d55cdd716cb92de5110b8fe85f8f2d6c59b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 175d002dac45ff1aa456ee6fe96de540e2dc95f28845287dee63ec9c15ea517a9195e03cb7b0afa821a92280a496601165c29c06d80172f6ab8405e9be474007
|
7
|
+
data.tar.gz: 9cf46386c6b0c24f1bf703d5866a49f2adde8556028d4d0a86c560f4b151382e000b6f2194d1045e0e22be7cca1ebb2d117af1a5e96c7c7eb404caffc800f7a7
|
@@ -45,14 +45,66 @@ module DataSteroid
|
|
45
45
|
def coercer
|
46
46
|
@coercer ||= Coercible::Coercer.new
|
47
47
|
end
|
48
|
+
|
49
|
+
def coerce(value, type)
|
50
|
+
if value.nil? || !type
|
51
|
+
value
|
52
|
+
else
|
53
|
+
case
|
54
|
+
when Array == type || Array === type then coerce_array(value, type)
|
55
|
+
when Hash == type || Hash === type then coerce_hash(value, type)
|
56
|
+
when type == Time then coerce_time(value)
|
57
|
+
else coerce_other(value, type)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def coerce_array(value, type)
|
63
|
+
# type: generic Array
|
64
|
+
if type == Array
|
65
|
+
coerce(element, type)
|
66
|
+
# type: Array[Something]
|
67
|
+
elsif value.respond_to?(:map)
|
68
|
+
value.map do |element|
|
69
|
+
coerce(element, type[0])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def coerce_hash(value, type)
|
77
|
+
# type: generic Hash
|
78
|
+
if type == Hash
|
79
|
+
coerce(element, type)
|
80
|
+
# type: Hash[Something => Other thing]
|
81
|
+
elsif value.is_a?(Hash)
|
82
|
+
k_type, v_type = type.to_a[0]
|
83
|
+
value.map{ |k, v| [ coerce(k, k_type), coerce(v, v_type) ] }.to_h
|
84
|
+
else
|
85
|
+
raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def coerce_time(value)
|
90
|
+
case value
|
91
|
+
when Integer then Time.at(value)
|
92
|
+
when String then Time.parse(value)
|
93
|
+
else value
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def coerce_other(value, type)
|
98
|
+
coercer[value.class].send("to_#{type.to_s.downcase}", value)
|
99
|
+
end
|
48
100
|
end
|
49
101
|
|
50
102
|
class_methods do
|
51
103
|
protected
|
52
104
|
|
53
105
|
def add_property(name, *args)
|
54
|
-
options = args[-1].is_a?(Hash) ? args.
|
55
|
-
options[:type] = args[0] if args[0]
|
106
|
+
options = args[-1].is_a?(Hash) ? args[-1].slice(:default) : {}
|
107
|
+
options[:type] = args[0] if args[0]
|
56
108
|
properties[name.to_s] = options
|
57
109
|
create_accessors(name, options)
|
58
110
|
self
|
@@ -65,13 +117,7 @@ module DataSteroid
|
|
65
117
|
end
|
66
118
|
|
67
119
|
define_method("#{name}=") do |value| # Define set method
|
68
|
-
|
69
|
-
if options[:type] && !value.nil?
|
70
|
-
coercer[value.class].send("to_#{options[:type].to_s.downcase}", value)
|
71
|
-
else
|
72
|
-
value
|
73
|
-
end
|
74
|
-
instance_variable_set("@#{name}", coerced_value)
|
120
|
+
instance_variable_set("@#{name}", coerce(value, options[:type]))
|
75
121
|
end
|
76
122
|
end
|
77
123
|
end
|
data/lib/data_steroid/version.rb
CHANGED