field_mapper 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c5deabe6cc8ef7c58243cb797ebea59ad4affcb
4
- data.tar.gz: 536688f17cc3afd08428f772cd49ecbdabd06dab
3
+ metadata.gz: 45d995d3afffa49b4dc07c9c956372125e898dda
4
+ data.tar.gz: 2b286ecf10742d95f6042d1aa68bd0b3cb1ea9cf
5
5
  SHA512:
6
- metadata.gz: 9e1599b63efea5c5e21366ddcb9797f00eee43118305e6bddb44cf93720abec31bf805780c0ce76a3ddd34f64f5a04a1f0148d9a462b7c17e19ad56409bfe210
7
- data.tar.gz: 7ee6d66aa959a5e5e084820b0e64e2e28c23b8c30198e053f4b10b926c93aa7a45dfdfa627ee0d7c357b67ca3ffae20b1aad3d6cd61d75c8e59f0f93e352d8a9
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 return Time.parse(value.to_s) rescue nil # TODO: log error?
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 return Monetize.parse(value) rescue nil # TODO: log error?
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?
@@ -1,3 +1,3 @@
1
1
  module FieldMapper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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, type: String do
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, type: Money, default: 0
21
+ field :net_worth,
22
+ type: Money,
23
+ default: 0
23
24
 
24
25
  # field that holds a list of plats
25
- field :friends, type: FieldMapper::Types::List[StandardUser], default: []
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, standard: :screen_name,
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: field_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins