field_mapper 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/field_mapper/standard/field.rb +6 -2
- data/lib/field_mapper/version.rb +1 -1
- data/test/readme_test.rb +31 -24
- 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: 45d995d3afffa49b4dc07c9c956372125e898dda
|
4
|
+
data.tar.gz: 2b286ecf10742d95f6042d1aa68bd0b3cb1ea9cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f6f40c1b701ad81e1d757be6a47190ea4c3f673b65ef69a05d29151a77aa5820023a6a27a290fa05f591f8b1ec360b7f64ba86374b77f9250651543c44cddef
|
7
|
+
data.tar.gz: 11a239c60479220f0f2d6ed5566bcf9c8f75dbe524c658739309d0900cb3415c4a52c0cf58fac419ae3bdc1b02acef53a81870f06697ee6cb7593c5736bc6713
|
@@ -111,10 +111,14 @@ module FieldMapper
|
|
111
111
|
case type.name
|
112
112
|
when "String" then return value.to_s.strip
|
113
113
|
when "FieldMapper::Types::Boolean" then return FieldMapper::Types::Boolean.parse(value)
|
114
|
-
when "Time" then
|
114
|
+
when "Time" then
|
115
|
+
return value if value.is_a?(Time)
|
116
|
+
return Time.parse(value.to_s) rescue nil
|
115
117
|
when "Integer" then return value.to_i
|
116
118
|
when "Float" then return value.to_f
|
117
|
-
when "Money" then
|
119
|
+
when "Money" then
|
120
|
+
return value if value.is_a?(Money)
|
121
|
+
return Monetize.parse(value) rescue nil
|
118
122
|
when "FieldMapper::Types::Plat" then return plat_instance(type, value)
|
119
123
|
when "FieldMapper::Types::List" then
|
120
124
|
return value if value.is_a?(Array) && value.empty?
|
data/lib/field_mapper/version.rb
CHANGED
data/test/readme_test.rb
CHANGED
@@ -9,20 +9,23 @@ class ReadmeTest < MicroTest::Test
|
|
9
9
|
field :screen_name, type: String
|
10
10
|
field :info, type: String
|
11
11
|
field :website, type: String
|
12
|
+
field :age, type: Integer
|
12
13
|
|
13
14
|
# field with allowed values
|
14
|
-
field :gender,
|
15
|
+
field :gender, type: String do
|
15
16
|
value "F"
|
16
17
|
value "M"
|
17
18
|
end
|
18
19
|
|
19
|
-
field :age, type: Integer
|
20
|
-
|
21
20
|
# field with a default value
|
22
|
-
field :net_worth,
|
21
|
+
field :net_worth,
|
22
|
+
type: Money,
|
23
|
+
default: 0
|
23
24
|
|
24
25
|
# field that holds a list of plats
|
25
|
-
field :friends,
|
26
|
+
field :friends,
|
27
|
+
type: FieldMapper::Types::List[StandardUser],
|
28
|
+
default: []
|
26
29
|
end
|
27
30
|
|
28
31
|
class FacebookUser < FieldMapper::Custom::Plat
|
@@ -31,9 +34,27 @@ class ReadmeTest < MicroTest::Test
|
|
31
34
|
|
32
35
|
# fields are mapped to the standard
|
33
36
|
field :name, standard: :name
|
37
|
+
field :bio, standard: :info
|
38
|
+
field :website, standard: :website
|
39
|
+
field :net_worth, standard: :net_worth
|
40
|
+
|
41
|
+
# field with mapped values
|
42
|
+
field :gender, standard: :gender do
|
43
|
+
value "female", standard: "F"
|
44
|
+
value "male", standard: "M"
|
45
|
+
end
|
46
|
+
|
47
|
+
# some fields don't map to a standard
|
48
|
+
field :birthday, type: Time
|
49
|
+
|
50
|
+
field :friends,
|
51
|
+
standard: :friends,
|
52
|
+
type: FieldMapper::Types::List[FacebookUser],
|
53
|
+
default: []
|
34
54
|
|
35
55
|
# field with complex transformation rules
|
36
|
-
field :username,
|
56
|
+
field :username,
|
57
|
+
standard: :screen_name,
|
37
58
|
custom_to_standard: -> (value, standard_instance: nil) {
|
38
59
|
# value passed is the custom value
|
39
60
|
# value returned is the standard value
|
@@ -44,27 +65,16 @@ class ReadmeTest < MicroTest::Test
|
|
44
65
|
# value returned is the custom value
|
45
66
|
value.to_s.split(/:/).last
|
46
67
|
}
|
47
|
-
|
48
|
-
field :bio, standard: :info
|
49
|
-
field :website, standard: :website
|
50
|
-
|
51
|
-
# field with mapped values
|
52
|
-
field :gender, standard: :gender do
|
53
|
-
value "female", standard: "F"
|
54
|
-
value "male", standard: "M"
|
55
|
-
end
|
56
|
-
|
57
|
-
field :net_worth, standard: :net_worth
|
58
|
-
field :friends, standard: :friends, type: FieldMapper::Types::List[FacebookUser], default: []
|
59
|
-
|
60
|
-
# not all custom fields are required to map to a standard
|
61
|
-
field :birthday, type: Time
|
62
68
|
end
|
63
69
|
|
64
70
|
class TwitterUser < FieldMapper::Custom::Plat
|
65
71
|
set_standard StandardUser
|
66
72
|
|
67
73
|
field :name, standard: :name
|
74
|
+
field :description, standard: :info
|
75
|
+
field :url, standard: :website
|
76
|
+
field :followers_count, type: Integer
|
77
|
+
|
68
78
|
field :screen_name, standard: :screen_name,
|
69
79
|
custom_to_standard: -> (value, standard_instance: nil) {
|
70
80
|
"Twitter:#{value.to_s.strip}"
|
@@ -72,9 +82,6 @@ class ReadmeTest < MicroTest::Test
|
|
72
82
|
standard_to_custom: -> (value, standard_instance: nil) {
|
73
83
|
value.to_s.split(/:/).last
|
74
84
|
}
|
75
|
-
field :description, standard: :info
|
76
|
-
field :url, standard: :website
|
77
|
-
field :followers_count, type: Integer
|
78
85
|
|
79
86
|
# callback method that runs after tranformation
|
80
87
|
def after_convert(from: nil, to: nil)
|