lazy_mapper 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5603d5daa9ab0151fb92ff724606a6e3eb33af849136db570b2857d4977a40a0
4
- data.tar.gz: 756b53c74259b65c399a52d48776e989c7c68050a9c8b53b1a69e959b00f192f
3
+ metadata.gz: 702528c73dfd2a707526f0e792472df55077dd97bf4cf034f6187b97f885ff41
4
+ data.tar.gz: c2b4708374ca625b5e81a6645cf279854241721705bfafdfee3f258f27f2ab8a
5
5
  SHA512:
6
- metadata.gz: 69e77cb56f70ac125b01800c0c6f78fb909b58a62539032cb301daaad4959778684687c1400f1f2339694351f6888df0e6abc7d4ba49874722e687af65f3fc8c
7
- data.tar.gz: ea5f9c3e44cc05d9652882717cdba1d189f30b5556bd05e44e8c51b1b41aaf6c70321295184a0560598467d14918c32e9b643ae428deef4c6ca468f82e0ed1f6
6
+ metadata.gz: 0540ab08d1cdcaac33e99876987df7d64ed6f4a31c4bac7f03581cf2d7ec64ee45ac35c9692614c6b78b8ca85952803fc5eab7f61818f790f784490b7c804271
7
+ data.tar.gz: e22803584b494a25427b059fa32ae059091ab614b72ef2293581dd81486a05b6193372f32b4b576262f7b8f876c74d9b41e1660cce43355472fcf5f1fbcbce31
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.1
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.1'
3
+ spec.version = '0.3.2'
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']
data/lib/lazy_mapper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bigdecimal'
2
4
  require 'bigdecimal/util'
3
5
  require 'time'
@@ -53,7 +55,7 @@ class LazyMapper
53
55
  Integer => 0,
54
56
  Numeric => 0,
55
57
  Float => 0.0,
56
- BigDecimal => BigDecimal.new('0'),
58
+ BigDecimal => BigDecimal(0),
57
59
  Array => []
58
60
  }.freeze
59
61
 
@@ -154,6 +156,7 @@ class LazyMapper
154
156
  def self.from unmapped_data, mappers: {}
155
157
  return nil if unmapped_data.nil?
156
158
  fail TypeError, "#{ unmapped_data.inspect } is not a Hash" unless unmapped_data.respond_to? :to_h
159
+
157
160
  instance = new
158
161
  instance.send :unmapped_data=, unmapped_data.to_h
159
162
  instance.send :mappers=, mappers
@@ -162,7 +165,7 @@ class LazyMapper
162
165
 
163
166
  def self.from_json *args, &block # :nodoc:
164
167
  warn "#{ self }.from_json is deprecated. Use #{ self }.from instead."
165
- from *args, &block
168
+ from(*args, &block)
166
169
  end
167
170
 
168
171
  #
@@ -304,27 +307,28 @@ class LazyMapper
304
307
  end
305
308
 
306
309
  def to_h
307
- attributes.each_with_object({}) {|(key, _value), h|
310
+ attributes.each_with_object({}) { |(key, _value), h|
308
311
  h[key] = self.send key
309
312
  }
310
313
  end
311
314
 
312
315
  def inspect
313
316
  @__under_inspection__ ||= 0
314
- return "<#{ self.class.name } ... >" if @__under_inspection__ > 0
317
+ return "<#{ self.class.name } ... >" if @__under_inspection__.positive?
318
+
315
319
  @__under_inspection__ += 1
316
320
  present_attributes = attributes.keys.each_with_object({}) { |name, memo|
317
- value = self.send name
318
- memo[name] = value unless value.nil?
321
+ ivar = IVAR[name]
322
+ next unless self.instance_variable_defined? ivar
323
+
324
+ memo[name] = self.instance_variable_get ivar
319
325
  }
320
- "<#{ self.class.name } #{ present_attributes.map { |k, v| k.to_s + ': ' + v.inspect }.join(', ') } >"
326
+
321
327
  res = "<#{ self.class.name } #{ present_attributes.map { |k, v| k.to_s + ': ' + v.inspect }.join(', ') } >"
322
328
  @__under_inspection__ -= 1
323
329
  res
324
330
  end
325
331
 
326
- protected
327
-
328
332
  #
329
333
  # Defines how to map an attribute name
330
334
  # to the corresponding name in the unmapped
@@ -358,19 +362,25 @@ class LazyMapper
358
362
  end
359
363
 
360
364
  def mapped_value(name, unmapped_value, type, map: mapping_for(name, type), default: default_value(type))
361
- if unmapped_value.nil?
362
- # Duplicate to prevent accidental sharing between instances
363
- default.dup
364
- else
365
- fail ArgumentError, "missing mapper for #{ name } (#{ type }). Unmapped value: #{ unmapped_value.inspect }" if map.nil?
366
- result = map.arity > 1 ? map.call(unmapped_value, self) : map.call(unmapped_value)
367
- result
365
+ return default.dup if unmapped_value.nil? # Duplicate to prevent accidental sharing between instances
366
+
367
+ if map.nil?
368
+ fail ArgumentError, "missing mapper for #{ name } (#{ type }). "\
369
+ "Unmapped value: #{ unmapped_value.inspect }"
368
370
  end
371
+
372
+ return map.call(unmapped_value, self) if map.arity > 1
373
+
374
+ map.call(unmapped_value)
369
375
  end
370
376
 
371
377
  def check_type! value, type, allow_nil:
372
378
  permitted_types = allow_nil ? Array(type) + [ NilClass ] : Array(type)
373
- fail TypeError.new "#{ self.class.name }: #{ value.inspect } is a #{ value.class } but was supposed to be a #{ humanize_list permitted_types, conjunction: ' or ' }" unless permitted_types.any? value.method(:is_a?)
379
+ return if permitted_types.any? value.method(:is_a?)
380
+
381
+ fail TypeError.new "#{ self.class.name }: "\
382
+ "#{ value.inspect } is a #{ value.class } "\
383
+ "but was supposed to be a #{ humanize_list permitted_types, conjunction: ' or ' }"
374
384
  end
375
385
 
376
386
  # [1,2,3] -> "1, 2 and 3"
@@ -379,6 +389,7 @@ class LazyMapper
379
389
  def humanize_list list, separator: ', ', conjunction: ' and '
380
390
  *all_but_last, last = list
381
391
  return last if all_but_last.empty?
392
+
382
393
  [ all_but_last.join(separator), last ].join conjunction
383
394
  end
384
395
 
@@ -64,6 +64,12 @@ describe LazyMapper do
64
64
  expect(mapper).to have_received(:map).exactly(1).times.with('42')
65
65
  end
66
66
 
67
+ it 'only shows already mapped values, when inspected' do
68
+ stub_const 'MyModel', klass
69
+ instance.created_at
70
+ expect(instance.inspect).to eq '<MyModel created_at: #<Date: 2015-07-27 ((2457231j,0s,0n),+0s,2299161j)> >'
71
+ end
72
+
67
73
  context 'if the mapped value is nil' do
68
74
  let(:map) { -> x { mapper.map(x); nil } }
69
75
 
@@ -101,6 +107,7 @@ describe LazyMapper do
101
107
  it 'avoids infinit recursion, when inspected' do
102
108
  stub_const('Bar', klass_bar)
103
109
  stub_const('Foo', klass_foo)
110
+ foo.bar.foo
104
111
  expect(foo.inspect).to eq('<Foo bar: <Bar foo: <Foo ... > > >')
105
112
  end
106
113
  end
@@ -163,10 +170,10 @@ describe LazyMapper do
163
170
  end
164
171
 
165
172
  instance = klass.from({ 'foo' => '123 456', 'bar' => 'abc def' },
166
- mappers: {
167
- foo: ->(f) { type.new(*f.split(' ').reverse) },
168
- type => ->(t) { type.new(*t.split(' ')) }
169
- })
173
+ mappers: {
174
+ foo: ->(f) { type.new(*f.split(' ').reverse) },
175
+ type => ->(t) { type.new(*t.split(' ')) }
176
+ })
170
177
 
171
178
  expect(instance.foo).to eq type.new('456', '123')
172
179
  expect(instance.bar).to eq type.new('abc', 'def')
@@ -290,15 +297,15 @@ describe LazyMapper do
290
297
 
291
298
  it 'still includes every attribute when converted to Hash' do
292
299
  expect(instance.to_h).to eq(
293
- title: '',
294
- count: 0,
295
- rate: 0.0,
296
- tags: [],
297
- widget: nil,
298
- things: ['something'],
299
- green?: false,
300
+ title: '',
301
+ count: 0,
302
+ rate: 0.0,
303
+ tags: [],
304
+ widget: nil,
305
+ things: ['something'],
306
+ green?: false,
300
307
  flowers?: false,
301
- cars: []
308
+ cars: []
302
309
  )
303
310
  end
304
311
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
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-10-24 00:00:00.000000000 Z
11
+ date: 2019-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - ".gitignore"
48
48
  - ".rubocop.yml"
49
+ - ".ruby-version"
49
50
  - Gemfile
50
51
  - LICENCE
51
52
  - README.md
@@ -72,8 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  - !ruby/object:Gem::Version
73
74
  version: '0'
74
75
  requirements: []
75
- rubyforge_project:
76
- rubygems_version: 2.7.3
76
+ rubygems_version: 3.0.1
77
77
  signing_key:
78
78
  specification_version: 4
79
79
  summary: A lazy object mapper