lazy_mapper 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lazy_mapper.gemspec +1 -3
- data/lib/lazy_mapper.rb +23 -6
- data/spec/lazy_mapper_spec.rb +18 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc96addc98f7d62a27df00a602599d7ea6d20ba9deb7f673e7ac3a4f4d65035f
|
4
|
+
data.tar.gz: ecf6e0704b94222ecde509b6a12c4493e971065ed065e709d335ee35d4d33f2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 632309a3f02d26685fbd352e237f713a4e5e16faa7d0eaa16e25233a789f8d8f39ac88eefb77c009d269e12eadfd62666c05c2b25a5b9da2cd05162c66ace7c1
|
7
|
+
data.tar.gz: 39d1a63b02163c110950da5b3f399079c0f39d2e7b19aef4df97b63e173a7b6f1375fa7dcd5aa0c61f2c4ef3b8c87549e4d68bdb747979329dcbd47c440dec51
|
data/lazy_mapper.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = 'lazy_mapper'
|
3
|
-
spec.version = '0.
|
3
|
+
spec.version = '0.2.0'
|
4
4
|
spec.summary = "A lazy object mapper"
|
5
5
|
spec.description = "Wraps primitive data in a semantically rich model"
|
6
6
|
spec.authors = ["Adam Lett"]
|
@@ -12,8 +12,6 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
13
13
|
spec.require_paths = ['lib']
|
14
14
|
|
15
|
-
spec.add_runtime_dependency 'activesupport', '>= 3'
|
16
|
-
|
17
15
|
spec.add_development_dependency 'bundler'
|
18
16
|
spec.add_development_dependency 'rspec'
|
19
17
|
end
|
data/lib/lazy_mapper.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
require 'bigdecimal/util'
|
3
3
|
require 'time'
|
4
|
-
require 'active_support/core_ext/class/attribute'
|
5
4
|
|
6
5
|
##
|
7
6
|
# Wraps a JSON object and lazily maps its attributes to domain objects
|
@@ -35,6 +34,15 @@ class LazyMapper
|
|
35
34
|
URI => URI.method(:parse)
|
36
35
|
}.freeze
|
37
36
|
|
37
|
+
def self.default_value_for type, value
|
38
|
+
default_values[type] = value
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.default_values
|
42
|
+
@default_values ||= DEFAULT_VALUES
|
43
|
+
end
|
44
|
+
|
45
|
+
|
38
46
|
# Default values for primitive types
|
39
47
|
DEFAULT_VALUES = {
|
40
48
|
String => '',
|
@@ -49,10 +57,19 @@ class LazyMapper
|
|
49
57
|
mappers[type] = mapper
|
50
58
|
end
|
51
59
|
|
52
|
-
|
53
|
-
|
60
|
+
def self.mappers
|
61
|
+
@mappers ||= DEFAULT_MAPPINGS
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.inherited(klass)
|
65
|
+
klass.instance_variable_set IVAR[:mappers], self.mappers.dup
|
66
|
+
klass.instance_variable_set IVAR[:default_values], self.default_values.dup
|
67
|
+
end
|
54
68
|
|
55
|
-
|
69
|
+
|
70
|
+
def mappers
|
71
|
+
@mappers ||= self.class.mappers
|
72
|
+
end
|
56
73
|
|
57
74
|
IVAR = -> name {
|
58
75
|
name_as_str = name.to_s
|
@@ -305,11 +322,11 @@ class LazyMapper
|
|
305
322
|
attr_writer :mappers
|
306
323
|
|
307
324
|
def mapping_for(name, type)
|
308
|
-
mappers[name] || mappers[type] || self.class.mappers[type]
|
325
|
+
mappers[name] || mappers[type] || self.class.mappers[type]
|
309
326
|
end
|
310
327
|
|
311
328
|
def default_value(type)
|
312
|
-
|
329
|
+
self.class.default_values[type]
|
313
330
|
end
|
314
331
|
|
315
332
|
def mapped_value(name, unmapped_value, type, map: mapping_for(name, type), default: default_value(type))
|
data/spec/lazy_mapper_spec.rb
CHANGED
@@ -155,7 +155,7 @@ describe LazyMapper do
|
|
155
155
|
expect(instance.composite).to eq type.new('abc', 'cde')
|
156
156
|
end
|
157
157
|
|
158
|
-
it 'supports adding default mappers to derived classes' do
|
158
|
+
it 'supports adding inheritable default mappers to derived classes' do
|
159
159
|
type = Struct.new(:val1, :val2)
|
160
160
|
|
161
161
|
klass = Class.new LazyMapper do
|
@@ -163,8 +163,25 @@ describe LazyMapper do
|
|
163
163
|
one :composite, type
|
164
164
|
end
|
165
165
|
|
166
|
+
klass2 = Class.new(klass)
|
166
167
|
instance = klass.from_json 'composite' => '123 456'
|
167
168
|
expect(instance.composite).to eq type.new('123', '456')
|
169
|
+
|
170
|
+
instance2 = klass2.from_json 'composite' => '456 789'
|
171
|
+
expect(instance2.composite).to eq type.new('456', '789')
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'supports adding or overriding inheritable default values for types to derived classes' do
|
175
|
+
type = Struct.new(:val1, :val2)
|
176
|
+
|
177
|
+
klass = Class.new LazyMapper do
|
178
|
+
default_value_for type, type.new('321', '123')
|
179
|
+
one :composite, type
|
180
|
+
end
|
181
|
+
|
182
|
+
klass2 = Class.new(klass)
|
183
|
+
instance = klass2.from_json({})
|
184
|
+
expect(instance.composite).to eq type.new('321', '123')
|
168
185
|
end
|
169
186
|
|
170
187
|
it 'supports injection of customer mappers during instantiation' do
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Lett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09
|
11
|
+
date: 2018-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '3'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|