field_mapper 0.1.0
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 +7 -0
- data/lib/field_mapper.rb +18 -0
- data/lib/field_mapper/custom/converter.rb +118 -0
- data/lib/field_mapper/custom/field.rb +110 -0
- data/lib/field_mapper/custom/plat.rb +60 -0
- data/lib/field_mapper/custom/value.rb +41 -0
- data/lib/field_mapper/errors.rb +11 -0
- data/lib/field_mapper/marshaller.rb +24 -0
- data/lib/field_mapper/name_helper.rb +17 -0
- data/lib/field_mapper/standard/converter.rb +111 -0
- data/lib/field_mapper/standard/field.rb +157 -0
- data/lib/field_mapper/standard/plat.rb +279 -0
- data/lib/field_mapper/standard/value.rb +24 -0
- data/lib/field_mapper/types/boolean.rb +15 -0
- data/lib/field_mapper/types/list.rb +58 -0
- data/lib/field_mapper/types/plat.rb +35 -0
- data/lib/field_mapper/version.rb +3 -0
- data/test/custom/converter_test.rb +123 -0
- data/test/custom/field_test.rb +137 -0
- data/test/custom/plat_example.rb +58 -0
- data/test/custom/plat_example_alt.rb +34 -0
- data/test/custom/plat_test.rb +102 -0
- data/test/custom/value_test.rb +43 -0
- data/test/readme_test.rb +119 -0
- data/test/standard/converter_test.rb +88 -0
- data/test/standard/field_test.rb +156 -0
- data/test/standard/plat_example.rb +48 -0
- data/test/standard/plat_test.rb +304 -0
- data/test/standard/value_test.rb +28 -0
- data/test/test_helper.rb +15 -0
- data/test/types/boolean_test.rb +77 -0
- data/test/types/list_test.rb +71 -0
- data/test/types/plat_test.rb +38 -0
- metadata +246 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
module Standard
|
4
|
+
class PlatExample < FieldMapper::Standard::Plat
|
5
|
+
|
6
|
+
field :name, type: String
|
7
|
+
field :desc, type: String
|
8
|
+
|
9
|
+
field :score, default: 2, type: Integer do
|
10
|
+
value 1
|
11
|
+
value 2
|
12
|
+
value 3
|
13
|
+
end
|
14
|
+
|
15
|
+
field :color, type: String do
|
16
|
+
load_values File.expand_path("../assets/colors.csv", __FILE__)
|
17
|
+
end
|
18
|
+
|
19
|
+
field :camelCase, type: String
|
20
|
+
field :PascalCase, type: String
|
21
|
+
|
22
|
+
field :artist, type: String do
|
23
|
+
value "Leonardo Da Vinci"
|
24
|
+
value "Michelangelo Buonarroti"
|
25
|
+
value "Raphael Sanzio"
|
26
|
+
end
|
27
|
+
|
28
|
+
field :day, type: String do
|
29
|
+
value "Sunday"
|
30
|
+
value "Monday"
|
31
|
+
value "Tuesday"
|
32
|
+
value "Wednesday"
|
33
|
+
value "Thursday"
|
34
|
+
value "Friday"
|
35
|
+
value "Saturday"
|
36
|
+
end
|
37
|
+
|
38
|
+
field :letters, type: FieldMapper::Types::List[String], default: ["a", "b"] do
|
39
|
+
value "a"
|
40
|
+
value "b"
|
41
|
+
value "c"
|
42
|
+
end
|
43
|
+
|
44
|
+
field :parent, type: FieldMapper::Types::Plat[Standard::PlatExample]
|
45
|
+
field :children, type: FieldMapper::Types::List[Standard::PlatExample], default: []
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,304 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
require_relative "plat_example"
|
3
|
+
|
4
|
+
module Standard
|
5
|
+
class PlatTest < MicroTest::Test
|
6
|
+
|
7
|
+
before do
|
8
|
+
@class = Standard::PlatExample
|
9
|
+
@instance = Standard::PlatExample.new
|
10
|
+
end
|
11
|
+
|
12
|
+
test "values assigned" do
|
13
|
+
assert @class.fields[:score].values.length == 3
|
14
|
+
assert @class.fields[:score].values[0].value == 1
|
15
|
+
assert @class.fields[:score].values[1].value == 2
|
16
|
+
assert @class.fields[:score].values[2].value == 3
|
17
|
+
end
|
18
|
+
|
19
|
+
test "methods defined" do
|
20
|
+
assert @instance.respond_to?(:name)
|
21
|
+
assert @instance.respond_to?(:name=)
|
22
|
+
assert @instance.respond_to?(:camel_case)
|
23
|
+
assert @instance.respond_to?(:camel_case=)
|
24
|
+
assert @instance.respond_to?(:pascal_case)
|
25
|
+
assert @instance.respond_to?(:pascal_case=)
|
26
|
+
end
|
27
|
+
|
28
|
+
test "defaults asigned" do
|
29
|
+
assert @instance.score == 2
|
30
|
+
assert @instance.letters = ["a", "c"]
|
31
|
+
end
|
32
|
+
|
33
|
+
test "constructor (with params)" do
|
34
|
+
instance = @class.new(name: "bar", score: 1)
|
35
|
+
assert instance.name == "bar"
|
36
|
+
assert instance.score == 1
|
37
|
+
end
|
38
|
+
|
39
|
+
test "read/write attr using []" do
|
40
|
+
@instance[:camelCase] = "as defined"
|
41
|
+
assert @instance[:camelCase] == "as defined"
|
42
|
+
|
43
|
+
@instance[:camel_case] = "ruby variant"
|
44
|
+
assert @instance[:camel_case] == "ruby variant"
|
45
|
+
end
|
46
|
+
|
47
|
+
test "read/write attr using []" do
|
48
|
+
@instance.pascal_case = true
|
49
|
+
assert @instance.pascal_case
|
50
|
+
end
|
51
|
+
|
52
|
+
test "read/write attr using getter/setter" do
|
53
|
+
@instance.name = "foobar"
|
54
|
+
assert @instance.name == "foobar"
|
55
|
+
end
|
56
|
+
|
57
|
+
test "assing multiple values to list field" do
|
58
|
+
instance = Standard::PlatExample.new(letters: ["a", "c", 1, true, Object.new])
|
59
|
+
assert ["a", "c"] == instance.letters
|
60
|
+
end
|
61
|
+
|
62
|
+
test "to_hash with flatten" do
|
63
|
+
hash = @instance.to_hash(flatten: true)
|
64
|
+
assert hash[:letters] == "[\"a\",\"b\"]"
|
65
|
+
end
|
66
|
+
|
67
|
+
test "parent & children" do
|
68
|
+
parent = Standard::PlatExample.new
|
69
|
+
parent_id = parent.object_id
|
70
|
+
@instance.parent = parent
|
71
|
+
|
72
|
+
assert @instance.parent.children.empty?
|
73
|
+
assert @instance.parent != @instance
|
74
|
+
|
75
|
+
child1 = Standard::PlatExample.new
|
76
|
+
child1_id = child1.object_id
|
77
|
+
@instance.children << child1
|
78
|
+
|
79
|
+
child2 = Standard::PlatExample.new
|
80
|
+
child2_id = child2.object_id
|
81
|
+
@instance.children << child2
|
82
|
+
|
83
|
+
assert @instance.parent.object_id == parent_id
|
84
|
+
assert @instance.children.first.object_id == child1_id
|
85
|
+
assert @instance.children.last.object_id == child2_id
|
86
|
+
assert @instance.parent.children.empty?
|
87
|
+
end
|
88
|
+
|
89
|
+
test "to_hash" do
|
90
|
+
parent = Standard::PlatExample.new(children: [@instance])
|
91
|
+
@instance.parent = parent
|
92
|
+
|
93
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
94
|
+
@instance.children << child1
|
95
|
+
|
96
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
97
|
+
@instance.children << child2
|
98
|
+
|
99
|
+
expected = {
|
100
|
+
"_node_id" => @instance.object_id,
|
101
|
+
"_flat" => false,
|
102
|
+
"name" => nil,
|
103
|
+
"desc" => nil,
|
104
|
+
"score" => 2,
|
105
|
+
"color" => nil,
|
106
|
+
"camelCase" => nil,
|
107
|
+
"PascalCase" => nil,
|
108
|
+
"artist" => nil,
|
109
|
+
"day" => nil,
|
110
|
+
"letters" => ["a", "b"],
|
111
|
+
"parent"=>{
|
112
|
+
"_node_id" => parent.object_id,
|
113
|
+
"_flat" => false,
|
114
|
+
"name" => nil,
|
115
|
+
"desc" => nil,
|
116
|
+
"score" => 2,
|
117
|
+
"color" => nil,
|
118
|
+
"camelCase" => nil,
|
119
|
+
"PascalCase" => nil,
|
120
|
+
"artist" => nil,
|
121
|
+
"day" => nil,
|
122
|
+
"letters" => ["a", "b"],
|
123
|
+
"parent" => nil,
|
124
|
+
"children" => [@instance.object_id]
|
125
|
+
},
|
126
|
+
"children"=>[
|
127
|
+
{
|
128
|
+
"_node_id" => child1.object_id,
|
129
|
+
"_flat" => false,
|
130
|
+
"name" => nil,
|
131
|
+
"desc" => nil,
|
132
|
+
"score" => 2,
|
133
|
+
"color" => nil,
|
134
|
+
"camelCase" => nil,
|
135
|
+
"PascalCase" => nil,
|
136
|
+
"artist" => nil,
|
137
|
+
"day" => nil,
|
138
|
+
"letters" => ["a", "b"],
|
139
|
+
"parent" => @instance.object_id,
|
140
|
+
"children" => []
|
141
|
+
},
|
142
|
+
{
|
143
|
+
"_node_id" => child2.object_id,
|
144
|
+
"_flat" => false,
|
145
|
+
"name" => nil,
|
146
|
+
"desc" => nil,
|
147
|
+
"score" => 2,
|
148
|
+
"color" => nil,
|
149
|
+
"camelCase" => nil,
|
150
|
+
"PascalCase" => nil,
|
151
|
+
"artist" => nil,
|
152
|
+
"day" => nil,
|
153
|
+
"letters" => ["a", "b"],
|
154
|
+
"parent" => @instance.object_id,
|
155
|
+
"children" => []
|
156
|
+
}
|
157
|
+
]
|
158
|
+
}
|
159
|
+
|
160
|
+
actual = @instance.to_hash
|
161
|
+
assert actual == expected
|
162
|
+
end
|
163
|
+
|
164
|
+
test "to_hash flatten" do
|
165
|
+
parent = Standard::PlatExample.new(children: [@instance])
|
166
|
+
@instance.parent = parent
|
167
|
+
|
168
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
169
|
+
@instance.children << child1
|
170
|
+
|
171
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
172
|
+
@instance.children << child2
|
173
|
+
|
174
|
+
expected = {
|
175
|
+
"_node_id" => @instance.object_id,
|
176
|
+
"_flat" => true,
|
177
|
+
"name" => nil,
|
178
|
+
"desc" => nil,
|
179
|
+
"score" => 2,
|
180
|
+
"color" => nil,
|
181
|
+
"camelCase" => nil,
|
182
|
+
"PascalCase" => nil,
|
183
|
+
"artist" => nil,
|
184
|
+
"day" => nil,
|
185
|
+
"letters" => "[\"a\",\"b\"]",
|
186
|
+
"parent" => "{\"_node_id\":#{parent.object_id},\"_flat\":true,\"name\":null,\"desc\":null,\"score\":2,\"color\":null,\"camelCase\":null,\"PascalCase\":null,\"artist\":null,\"day\":null,\"letters\":\"[\\\"a\\\",\\\"b\\\"]\",\"parent\":null,\"children\":\"[#{@instance.object_id}]\"}",
|
187
|
+
"children" => "[{\"_node_id\":#{child1.object_id},\"_flat\":true,\"name\":null,\"desc\":null,\"score\":2,\"color\":null,\"camelCase\":null,\"PascalCase\":null,\"artist\":null,\"day\":null,\"letters\":\"[\\\"a\\\",\\\"b\\\"]\",\"parent\":#{@instance.object_id},\"children\":[]},{\"_node_id\":#{child2.object_id},\"_flat\":true,\"name\":null,\"desc\":null,\"score\":2,\"color\":null,\"camelCase\":null,\"PascalCase\":null,\"artist\":null,\"day\":null,\"letters\":\"[\\\"a\\\",\\\"b\\\"]\",\"parent\":#{@instance.object_id},\"children\":[]}]"
|
188
|
+
}
|
189
|
+
|
190
|
+
actual = @instance.to_hash(flatten: true)
|
191
|
+
assert actual[:_flat]
|
192
|
+
assert actual == expected
|
193
|
+
end
|
194
|
+
|
195
|
+
test "initialize from to_hash (simple)" do
|
196
|
+
@instance.name = "foobar"
|
197
|
+
@instance.score = 3
|
198
|
+
@instance.letters = ["c"]
|
199
|
+
hash = @instance.to_hash
|
200
|
+
instance = Standard::PlatExample.new(hash)
|
201
|
+
assert @instance.to_hash(include_meta: false) == instance.to_hash(include_meta: false)
|
202
|
+
end
|
203
|
+
|
204
|
+
test "initialize from to_hash (simple flat)" do
|
205
|
+
@instance.name = "foobar"
|
206
|
+
@instance.score = 3
|
207
|
+
@instance.letters = ["c"]
|
208
|
+
hash = @instance.to_hash(flatten: true)
|
209
|
+
instance = Standard::PlatExample.new(hash)
|
210
|
+
assert @instance.to_hash(include_meta: false) == instance.to_hash(include_meta: false)
|
211
|
+
end
|
212
|
+
|
213
|
+
test "initialize from to_hash (parent)" do
|
214
|
+
@instance.parent = Standard::PlatExample.new(children: [@instance])
|
215
|
+
hash = @instance.to_hash
|
216
|
+
instance = Standard::PlatExample.new(hash)
|
217
|
+
assert instance.parent.children.length == 1
|
218
|
+
assert instance.parent.children.first == instance
|
219
|
+
end
|
220
|
+
|
221
|
+
test "initialize from to_hash flattened (parent)" do
|
222
|
+
@instance.parent = Standard::PlatExample.new(children: [@instance])
|
223
|
+
hash = @instance.to_hash(flatten: true)
|
224
|
+
instance = Standard::PlatExample.new(hash)
|
225
|
+
assert instance.parent.children.length == 1
|
226
|
+
assert instance.parent.children.first == instance
|
227
|
+
end
|
228
|
+
|
229
|
+
test "initialize from to_hash (children)" do
|
230
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
231
|
+
@instance.children << child1
|
232
|
+
|
233
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
234
|
+
@instance.children << child2
|
235
|
+
|
236
|
+
hash = @instance.to_hash
|
237
|
+
instance = Standard::PlatExample.new(hash)
|
238
|
+
|
239
|
+
assert instance.children.length == 2
|
240
|
+
instance.children.each do |child|
|
241
|
+
assert child.parent == instance
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
test "initialize from to_hash flattened (children)" do
|
246
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
247
|
+
@instance.children << child1
|
248
|
+
|
249
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
250
|
+
@instance.children << child2
|
251
|
+
|
252
|
+
hash = @instance.to_hash(flatten: true)
|
253
|
+
instance = Standard::PlatExample.new(hash)
|
254
|
+
|
255
|
+
assert instance.children.length == 2
|
256
|
+
instance.children.each do |child|
|
257
|
+
assert child.parent == instance
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
test "initialize from to_hash (parent & children)" do
|
262
|
+
@instance.parent = Standard::PlatExample.new(children: [@instance])
|
263
|
+
|
264
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
265
|
+
@instance.children << child1
|
266
|
+
|
267
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
268
|
+
@instance.children << child2
|
269
|
+
|
270
|
+
hash = @instance.to_hash
|
271
|
+
instance = Standard::PlatExample.new(hash)
|
272
|
+
|
273
|
+
assert instance.parent.children.length == 1
|
274
|
+
assert instance.parent.children.first == instance
|
275
|
+
|
276
|
+
assert instance.children.length == 2
|
277
|
+
instance.children.each do |child|
|
278
|
+
assert child.parent == instance
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
test "initialize from to_hash flattened (parent & children)" do
|
283
|
+
@instance.parent = Standard::PlatExample.new(children: [@instance])
|
284
|
+
|
285
|
+
child1 = Standard::PlatExample.new(parent: @instance)
|
286
|
+
@instance.children << child1
|
287
|
+
|
288
|
+
child2 = Standard::PlatExample.new(parent: @instance)
|
289
|
+
@instance.children << child2
|
290
|
+
|
291
|
+
hash = @instance.to_hash(flatten: true)
|
292
|
+
instance = Standard::PlatExample.new(hash)
|
293
|
+
|
294
|
+
assert instance.parent.children.length == 1
|
295
|
+
assert instance.parent.children.first == instance
|
296
|
+
|
297
|
+
assert instance.children.length == 2
|
298
|
+
instance.children.each do |child|
|
299
|
+
assert child.parent == instance
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
module Standard
|
4
|
+
class ValueTest < MicroTest::Test
|
5
|
+
|
6
|
+
test "constructor requires field" do
|
7
|
+
begin
|
8
|
+
FieldMapper::Standard::Value.new("bar")
|
9
|
+
rescue ArgumentError => e
|
10
|
+
error = e
|
11
|
+
end
|
12
|
+
assert error.present?
|
13
|
+
end
|
14
|
+
|
15
|
+
test "constructor sets value" do
|
16
|
+
field = FieldMapper::Standard::Field.new(:foo, type: String)
|
17
|
+
value = FieldMapper::Standard::Value.new("bar", field: field)
|
18
|
+
assert value.value == "bar"
|
19
|
+
end
|
20
|
+
|
21
|
+
test "constructor type casts value" do
|
22
|
+
field = FieldMapper::Standard::Field.new(:foo, type: Integer)
|
23
|
+
value = FieldMapper::Standard::Value.new("100", field: field)
|
24
|
+
assert value.value == 100
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
|
3
|
+
if ENV["CI"]
|
4
|
+
require "coveralls"
|
5
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
6
|
+
end
|
7
|
+
|
8
|
+
SimpleCov.command_name "micro_test"
|
9
|
+
SimpleCov.start do
|
10
|
+
add_filter "/test/"
|
11
|
+
end
|
12
|
+
|
13
|
+
Coveralls.wear! if ENV["CI"]
|
14
|
+
|
15
|
+
require_relative "../lib/field_mapper"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
class BooleanTest < MicroTest::Test
|
4
|
+
Boolean = FieldMapper::Types::Boolean
|
5
|
+
|
6
|
+
test "parse nil" do
|
7
|
+
assert Boolean.parse(nil) == false
|
8
|
+
end
|
9
|
+
|
10
|
+
test "parse 0" do
|
11
|
+
assert Boolean.parse(0) == false
|
12
|
+
end
|
13
|
+
|
14
|
+
test "parse f" do
|
15
|
+
assert Boolean.parse("f") == false
|
16
|
+
end
|
17
|
+
|
18
|
+
test "parse F" do
|
19
|
+
assert Boolean.parse("F") == false
|
20
|
+
end
|
21
|
+
|
22
|
+
test "parse false" do
|
23
|
+
assert Boolean.parse("false") == false
|
24
|
+
end
|
25
|
+
|
26
|
+
test "parse FALSE" do
|
27
|
+
assert Boolean.parse("FALSE") == false
|
28
|
+
end
|
29
|
+
|
30
|
+
test "parse FaLsE" do
|
31
|
+
assert Boolean.parse("FaLsE") == false
|
32
|
+
end
|
33
|
+
|
34
|
+
test "parse actual false" do
|
35
|
+
assert Boolean.parse(false) == false
|
36
|
+
end
|
37
|
+
|
38
|
+
test "parse n" do
|
39
|
+
assert Boolean.parse("n") == false
|
40
|
+
end
|
41
|
+
|
42
|
+
test "parse N" do
|
43
|
+
assert Boolean.parse("N") == false
|
44
|
+
end
|
45
|
+
|
46
|
+
test "parse no" do
|
47
|
+
assert Boolean.parse("no") == false
|
48
|
+
end
|
49
|
+
|
50
|
+
test "parse NO" do
|
51
|
+
assert Boolean.parse("NO") == false
|
52
|
+
end
|
53
|
+
|
54
|
+
test "parse No" do
|
55
|
+
assert Boolean.parse("No") == false
|
56
|
+
end
|
57
|
+
|
58
|
+
test "parse t" do
|
59
|
+
assert Boolean.parse("t")
|
60
|
+
end
|
61
|
+
|
62
|
+
test "parse true" do
|
63
|
+
assert Boolean.parse("true")
|
64
|
+
end
|
65
|
+
|
66
|
+
test "parse actual true" do
|
67
|
+
assert Boolean.parse(true)
|
68
|
+
end
|
69
|
+
|
70
|
+
test "parse random string" do
|
71
|
+
assert Boolean.parse("jfdkhjwe")
|
72
|
+
end
|
73
|
+
|
74
|
+
test "parse random number" do
|
75
|
+
assert Boolean.parse(7482397)
|
76
|
+
end
|
77
|
+
end
|