varia_model 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a78aa9f5f5258c296e31c04b6c53c651801753c
4
- data.tar.gz: 80dc767391b7a353b3aa37f73475ddfa2d620256
3
+ metadata.gz: 77dd327b0e41d985df1a161e39a5147b507e1df5
4
+ data.tar.gz: 347a99f528442382d89e7766f12492a5b1154522
5
5
  SHA512:
6
- metadata.gz: f0ffddc9eb66f04a179816ea6abb730593543b65bfa12d01689556334cab6be1fb4b03bae3755340466864dae7c14241fdf07bb5855e923cc57f0b2d5bcce25f
7
- data.tar.gz: 1b3c83784c73ccc2c878efa0087fdd80c793687185b0ad2f4fbf0cac501bb04a95c767d8a920358bc944433e3b0f0995af1f59569f5ee104c1781425ec869233
6
+ metadata.gz: b2b42df3bd92530a95735c97e6091849d93bd75a321e85027204390087364690f93be687aaa82babec153f139a94cf6ad94be123353ba8ebca33ad911c1f5cb5
7
+ data.tar.gz: 36ee952987081d8a69f9a9f58d37d5688b0d7caca1657860f8be7a1c6c5ea754c4782847b7de6c838d890864c9449f4aea9f91531af53ea43feeb7c086ff2090
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .rspec
@@ -1,3 +1,3 @@
1
1
  module VariaModel
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/varia_model.rb CHANGED
@@ -214,7 +214,7 @@ module VariaModel
214
214
  #
215
215
  # @return [Object]
216
216
  def get_attribute(key)
217
- _attributes_.dig(key.to_s)
217
+ eval_as_proc(_attributes_.dig(key.to_s))
218
218
  end
219
219
  alias_method :[], :get_attribute
220
220
 
@@ -254,10 +254,15 @@ module VariaModel
254
254
  #
255
255
  # @return [String]
256
256
  def to_json(options = {})
257
- JSON.generate(_attributes_, options)
257
+ JSON.generate(to_hash, options)
258
258
  end
259
259
  alias_method :as_json, :to_json
260
260
 
261
+ # Convert the object to a hash.
262
+ def to_hash
263
+ _attributes_.inject({}) { |h, (k,v)| h[k] = eval_as_proc(v); h }
264
+ end
265
+
261
266
  protected
262
267
 
263
268
  # @param [String] attribute
@@ -269,6 +274,14 @@ module VariaModel
269
274
 
270
275
  private
271
276
 
277
+ # Send #call to the given object if it responds to it. If it doesn't, just return the
278
+ # object.
279
+ #
280
+ # @param [#call]
281
+ def eval_as_proc(obj)
282
+ obj.respond_to?(:call) ? obj.call : obj
283
+ end
284
+
272
285
  def carefree_assign(new_attrs = {})
273
286
  _attributes_.deep_merge!(new_attrs)
274
287
  end
@@ -55,6 +55,12 @@ describe VariaModel do
55
55
 
56
56
  expect(subject.attributes.dig('attribute')).to eql('some value')
57
57
  end
58
+
59
+ it "allows an attribute that has a lambda default value" do
60
+ subject.attribute 'brooke', default: ->{ "winsor".upcase }
61
+
62
+ expect(subject.attributes.dig('brooke')).to be_a(Proc)
63
+ end
58
64
  end
59
65
 
60
66
  describe "::validations" do
@@ -468,6 +474,38 @@ describe VariaModel do
468
474
  it "returns nil if the dotted path matches no attributes" do
469
475
  expect(subject.get_attribute('brooke.costantini')).to be_nil
470
476
  end
477
+
478
+ it "returns the current value of the Proc" do
479
+ subject.brooke.winsor = ->{ "bacon".upcase }
480
+ expect(subject.get_attribute("brooke.winsor")).to eql("BACON")
481
+ end
482
+
483
+ it "returns the current value of the Proc each time" do
484
+ @magic = "ponies"
485
+ subject.brooke.winsor = -> { @magic }
486
+ expect(subject.get_attribute("brooke.winsor")).to eql("ponies")
487
+ @magic = "unicorns"
488
+ expect(subject.get_attribute("brooke.winsor")).to eql("unicorns")
489
+ end
490
+ end
491
+
492
+ describe "#[]" do
493
+ subject do
494
+ Class.new do
495
+ include VariaModel
496
+ attribute 'foo', default: ->{ String.new("Bacon").upcase }
497
+ end.new
498
+ end
499
+
500
+ it "returns the current value of the Proc" do
501
+ expect(subject['foo']).to eql("BACON")
502
+ end
503
+
504
+ it "returns the updated value of the Proc" do
505
+ subject['foo']
506
+ subject['foo'] = 'hello'
507
+ expect(subject['foo']).to eql('hello')
508
+ end
471
509
  end
472
510
 
473
511
  describe "#mass_assign" do
@@ -600,6 +638,13 @@ describe VariaModel do
600
638
 
601
639
  expect(subject.to_json).to eql(JSON.dump(first_name: "brooke", nick: "leblanc"))
602
640
  end
641
+
642
+ it "includes the most recent value for any Procs" do
643
+ subject.first_name = ->{ "seth".capitalize }
644
+ subject.nick = ->{ "name".upcase }
645
+
646
+ expect(subject.to_json).to eql(JSON.dump(first_name: "Seth", nick: "NAME"))
647
+ end
603
648
  end
604
649
 
605
650
  describe "#to_hash" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: varia_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Winsor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-16 00:00:00.000000000 Z
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie