disposable 0.1.5 → 0.1.6

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: f0f85e8b618ecd45ecb2a9fcaca32ab1e28d8377
4
- data.tar.gz: fb2f9474bd6f8aeca1f2ab5bd808825a62a00ff0
3
+ metadata.gz: 53b71c61f518f6c2e11210f479cd37258c8bf1d8
4
+ data.tar.gz: 6527a70e2a954f8c490818a08c4fc2a1657f43d6
5
5
  SHA512:
6
- metadata.gz: 447222f485c8c0b950f5916ecf6a36f5337fab008e33beaedd3462c761d6c733a41c0c9bb6bfeb1aefd9bc2d74c1fa253a287a824d5234aa4eca486c6f58d2aa
7
- data.tar.gz: 3f1daef9a44efbd2946fc4013529d9d272883b7d46ebda0108685e33640a4a3585a6e337b8006663b965fb84d241262e5f8b9403383b3f6fd81a93a7c5d2307f
6
+ metadata.gz: f768a7a5e0ba34502a34e40f0a13db3c81af668ffb743afbf8fe9ad2dfdec8d72d8faa5950e2b7bca742a9a4fa2e00922954b2a9ebedfdb3994e366762328d84
7
+ data.tar.gz: b8efbe750912d51d9ef2ede971e4282db397fd4c3b82394d6257d23eed25f3c854f82b796ff3da8b3ad33442d7e54b23b3a68debdccfb5c3acbf174989ec58e0
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.6
2
+
3
+ * Added `Default`.
4
+
1
5
  # 0.1.5
2
6
 
3
7
  * Correctly merge options from constructor into `@fields`.
data/README.md CHANGED
@@ -206,6 +206,25 @@ twin.id #=> 1
206
206
 
207
207
  Again, coercion only happens in the setter.
208
208
 
209
+
210
+ ## Defaults
211
+
212
+ Default values can be set via `:default`.
213
+
214
+ ```ruby
215
+ class AlbumTwin < Disposable::Twin
216
+ feature Default
217
+
218
+ property :title, default: "The Greatest Songs Ever Written"
219
+ property :composer, default: Composer.new do
220
+ property :name
221
+ end
222
+ end
223
+ ```
224
+
225
+ Default value is applied when the model's getter returns `nil`.
226
+
227
+
209
228
  ## Collections
210
229
 
211
230
  Collections can be defined analogue to `property`. The exposed API is the `Array` API.
Binary file
@@ -12,6 +12,7 @@ require "disposable/twin/builder"
12
12
  require "disposable/twin/changed"
13
13
  require "disposable/twin/property_processor"
14
14
  require "disposable/twin/persisted"
15
+ require "disposable/twin/default"
15
16
 
16
17
  # Twin.new(model/composition hash/hash, options)
17
18
  # assign hash to @fields
@@ -0,0 +1,12 @@
1
+ # TODO: allow default: -> for hashes, etc.
2
+ module Disposable::Twin::Default
3
+ def setup_value_for(dfn, options)
4
+ value = super
5
+ return value unless value.nil?
6
+ default_for(dfn, options)
7
+ end
8
+
9
+ def default_for(dfn, options)
10
+ dfn[:default]
11
+ end
12
+ end
@@ -24,8 +24,7 @@ module Disposable
24
24
  schema.each do |dfn|
25
25
  next if dfn[:readable] == false
26
26
 
27
- name = dfn.name
28
- value = options[name.to_sym] || mapper.send(name) # model.title.
27
+ value = setup_value_for(dfn, options)
29
28
 
30
29
  setup_write!(dfn, value)
31
30
  end
@@ -34,6 +33,11 @@ module Disposable
34
33
  # from_hash(options) # assigns known properties from options.
35
34
  end
36
35
 
36
+ def setup_value_for(dfn, options)
37
+ name = dfn.name
38
+ options[name.to_sym] || mapper.send(name) # model.title.
39
+ end
40
+
37
41
  def setup_write!(dfn, value)
38
42
  send(dfn.setter, value)
39
43
  end
@@ -1,3 +1,3 @@
1
1
  module Disposable
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -0,0 +1,75 @@
1
+ require "test_helper"
2
+
3
+ class DefaultTest < Minitest::Spec
4
+ Song = Struct.new(:title, :composer)
5
+ Composer = Struct.new(:name)
6
+
7
+ class Twin < Disposable::Twin
8
+ feature Default
9
+
10
+ property :title, default: "Mediocore"
11
+ property :composer, default: Composer.new do
12
+ property :name, default: "NOFX"
13
+ end
14
+ end
15
+
16
+ # all given.
17
+ it do
18
+ twin = Twin.new(Song.new("Anarchy Camp", Composer.new("Nofx")))
19
+ twin.title.must_equal "Anarchy Camp"
20
+ twin.composer.name.must_equal "Nofx"
21
+ end
22
+
23
+ # defaults, please.
24
+ it do
25
+ twin = Twin.new(Song.new)
26
+ twin.title.must_equal "Mediocore"
27
+ twin.composer.name.must_equal "NOFX"
28
+ end
29
+
30
+ # false value is not defaulted.
31
+ it do
32
+ twin = Twin.new(Song.new(false))
33
+ twin.title.must_equal false
34
+ end
35
+ end
36
+
37
+
38
+ require "disposable/twin/struct"
39
+ class DefaultWithStructTest < Minitest::Spec
40
+ Song = Struct.new(:settings)
41
+
42
+ class Twin < Disposable::Twin
43
+ feature Default
44
+ feature Sync
45
+
46
+ property :settings, default: Hash.new do
47
+ include Struct
48
+
49
+ property :enabled, default: "yes"
50
+ property :roles, default: Hash.new do
51
+ include Struct
52
+ property :admin, default: "maybe"
53
+ end
54
+ end
55
+ end
56
+
57
+ # all given.
58
+ it do
59
+ twin = Twin.new(Song.new({enabled: true, roles: {admin: false}}))
60
+ twin.settings.enabled.must_equal true
61
+ twin.settings.roles.admin.must_equal false
62
+ end
63
+
64
+ # defaults, please.
65
+ it do
66
+ song = Song.new
67
+ twin = Twin.new(song)
68
+ twin.settings.enabled.must_equal "yes"
69
+ twin.settings.roles.admin.must_equal "maybe"
70
+
71
+ twin.sync
72
+
73
+ song.settings.must_equal({"enabled"=>"yes", "roles"=>{"admin"=>"maybe"}})
74
+ end
75
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disposable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-01 00:00:00.000000000 Z
11
+ date: 2015-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uber
@@ -164,6 +164,7 @@ files:
164
164
  - lib/disposable/twin/coercion.rb
165
165
  - lib/disposable/twin/collection.rb
166
166
  - lib/disposable/twin/composition.rb
167
+ - lib/disposable/twin/default.rb
167
168
  - lib/disposable/twin/option.rb
168
169
  - lib/disposable/twin/persisted.rb
169
170
  - lib/disposable/twin/property_processor.rb
@@ -188,6 +189,7 @@ files:
188
189
  - test/twin/coercion_test.rb
189
190
  - test/twin/collection_test.rb
190
191
  - test/twin/composition_test.rb
192
+ - test/twin/default_test.rb
191
193
  - test/twin/expose_test.rb
192
194
  - test/twin/feature_test.rb
193
195
  - test/twin/from_test.rb
@@ -244,6 +246,7 @@ test_files:
244
246
  - test/twin/coercion_test.rb
245
247
  - test/twin/collection_test.rb
246
248
  - test/twin/composition_test.rb
249
+ - test/twin/default_test.rb
247
250
  - test/twin/expose_test.rb
248
251
  - test/twin/feature_test.rb
249
252
  - test/twin/from_test.rb