disposable 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3ede9bea8a77d3c3ca49a1d8d30e4d85174064b
4
- data.tar.gz: bc8db041e0b26ce6520db6c0d647ad04f3fdffb4
3
+ metadata.gz: f0f85e8b618ecd45ecb2a9fcaca32ab1e28d8377
4
+ data.tar.gz: fb2f9474bd6f8aeca1f2ab5bd808825a62a00ff0
5
5
  SHA512:
6
- metadata.gz: 241f8a98b1db05b79d6df1a725081648317c9eb913fca3bef48d487c8b8214d3089913285a1b6a0c581a8c7f778528924cf60ca8cc675143a3130603c959b13e
7
- data.tar.gz: bb406f4d2550786497c0d3dc80b77ec69b97643f68adcba6ad2a9c78ef54121206a882edcf8363357ee5f325b0f8b60d70310efce9f829433d12de85a8f1c2b5
6
+ metadata.gz: 447222f485c8c0b950f5916ecf6a36f5337fab008e33beaedd3462c761d6c733a41c0c9bb6bfeb1aefd9bc2d74c1fa253a287a824d5234aa4eca486c6f58d2aa
7
+ data.tar.gz: 3f1daef9a44efbd2946fc4013529d9d272883b7d46ebda0108685e33640a4a3585a6e337b8006663b965fb84d241262e5f8b9403383b3f6fd81a93a7c5d2307f
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.1.5
2
+
3
+ * Correctly merge options from constructor into `@fields`.
4
+ * Add `:virtual` which is an alias for `readable: false, writeable: false`.
5
+ * Do not use getters with `SkipGetter` in `#sync{}`.
6
+
1
7
  # 0.1.4
2
8
 
3
9
  * Add `Twin::Coercion`.
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in disposable.gemspec
4
4
  gemspec
5
5
 
6
- # gem 'representable', path: '../representable'
6
+ # gem 'representable', path: '../representable'
data/README.md CHANGED
@@ -539,6 +539,14 @@ collection :songs do
539
539
  end
540
540
  ```
541
541
 
542
+ ## Readable, Writeable, Virtual
543
+
544
+ Properties can have various access settings.
545
+
546
+ * `readable: false` won't read from the model in `Setup`.
547
+ * `writeable: false` won't write to model in `Sync`.
548
+ * `virtual: true` is both settings above combined.
549
+
542
550
  ## Builders
543
551
 
544
552
  ## Used In
data/database.sqlite3 CHANGED
Binary file
@@ -47,6 +47,10 @@ module Disposable
47
47
  options[:private_name] = options.delete(:from) || name
48
48
  options[:pass_options] = true
49
49
 
50
+ if options.delete(:virtual)
51
+ options[:writeable] = options[:readable] = false
52
+ end
53
+
50
54
  representer_class.property(name, options, &block).tap do |definition|
51
55
  mod = Module.new do
52
56
  define_method(name) { @fields[name.to_s] }
@@ -30,7 +30,7 @@ module Disposable
30
30
  setup_write!(dfn, value)
31
31
  end
32
32
 
33
- @fields.merge!(options) # FIXME: hash/string. # FIXME: call writer!!!!!!!!!!
33
+ merge_options!(options) # FIXME: call writer!!!!!!!!!!
34
34
  # from_hash(options) # assigns known properties from options.
35
35
  end
36
36
 
@@ -38,6 +38,11 @@ module Disposable
38
38
  send(dfn.setter, value)
39
39
  end
40
40
 
41
+ def merge_options!(options)
42
+ # TODO: ask charliesome if that is faster with return unless options.size.
43
+ options.each { |k, v| @fields[k.to_s] = v } # TODO: this merges every option, not only defined ones.
44
+ end
45
+
41
46
 
42
47
  # Including this will _not_ use the property's setter in Setup and allow you to override it.
43
48
  module SkipSetter
@@ -48,7 +48,11 @@ class Disposable::Twin
48
48
 
49
49
  module ToNestedHash
50
50
  def to_nested_hash(*)
51
- self.class.nested_hash_representer.new(self).to_hash
51
+ self.class.nested_hash_representer.new(nested_hash_source).to_hash
52
+ end
53
+
54
+ def nested_hash_source
55
+ self
52
56
  end
53
57
 
54
58
  module ClassMethods
@@ -58,8 +62,10 @@ class Disposable::Twin
58
62
  include Representable::Hash
59
63
 
60
64
  representable_attrs.each do |dfn|
61
- dfn.merge!(readable: true) # the nested hash contains all fields.
62
- dfn.merge!(as: dfn[:private_name]) # nested hash keys by model property names.
65
+ dfn.merge!(
66
+ readable: true, # the nested hash contains all fields.
67
+ as: dfn[:private_name] # nested hash keys by model property names.
68
+ )
63
69
 
64
70
  dfn.merge!(
65
71
  prepare: lambda { |model, *| model }, # TODO: why do we need that here?
@@ -122,6 +128,10 @@ class Disposable::Twin
122
128
  def sync_read(dfn)
123
129
  @fields[dfn.name]
124
130
  end
131
+
132
+ def nested_hash_source
133
+ OpenStruct.new(@fields)
134
+ end
125
135
  end
126
136
  end
127
137
  end
@@ -1,3 +1,3 @@
1
1
  module Disposable
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -33,6 +33,13 @@ class SkipGetterTest < MiniTest::Spec
33
33
 
34
34
  album.title.must_equal "Wild Frontier"
35
35
  album.artist.name.must_equal "Gary Moore"
36
+
37
+ # nested hash.
38
+ nested_hash = nil
39
+ twin.sync do |hash|
40
+ nested_hash = hash
41
+ end
42
+ nested_hash.must_equal({"title"=>"Wild Frontier", "artist"=>{"name"=>"Gary Moore"}})
36
43
  end
37
44
  end
38
45
 
@@ -10,7 +10,6 @@ class ChangedWithSetupTest < MiniTest::Spec
10
10
 
11
11
  module Twin
12
12
  class Album < Disposable::Twin
13
- feature Setup
14
13
  feature Changed
15
14
  # include Coercion
16
15
  property :name
@@ -25,7 +24,6 @@ class ChangedWithSetupTest < MiniTest::Spec
25
24
  end
26
25
 
27
26
  property :artist do
28
- include Setup
29
27
  property :name
30
28
  end
31
29
  end
@@ -105,4 +103,35 @@ class ChangedWithSetupTest < MiniTest::Spec
105
103
  twin.name = "The Rest Is Silence"
106
104
  twin.changed?.must_equal false
107
105
  end
106
+ end
107
+
108
+
109
+ require "disposable/twin/coercion"
110
+ class ChangedWithCoercionTest < MiniTest::Spec
111
+ Song = Struct.new(:released)
112
+
113
+ class SongTwin < Disposable::Twin
114
+ include Changed
115
+ include Coercion
116
+
117
+ property :released, type: Virtus::Attribute::Boolean
118
+ end
119
+
120
+ it do
121
+ twin = SongTwin.new(Song.new)
122
+ twin.changed?(:released).must_equal false
123
+ twin.released = 1
124
+ twin.released.must_equal true
125
+ twin.changed?(:released).must_equal true
126
+ end
127
+
128
+ it do
129
+ twin = SongTwin.new(Song.new(true))
130
+ twin.released = true
131
+ twin.changed?(:released).must_equal false
132
+ twin.released = 1 # it coerces, then assigns, then compares, which makes this NOT changed.
133
+ twin.changed?(:released).must_equal false
134
+ twin.released = false
135
+ twin.changed?(:released).must_equal true
136
+ end
108
137
  end
@@ -136,4 +136,20 @@ class TwinSetupWithInlineTwinsTest < MiniTest::Spec
136
136
  twin.songs[1].composer.must_be_kind_of Disposable::Twin
137
137
  twin.songs[1].composer.id.must_equal 2
138
138
  end
139
+ end
140
+
141
+ # Twin.new(model, is_online: true)
142
+ class TwinWithVirtualSetupTest < MiniTest::Spec
143
+ Song = Struct.new(:id)
144
+
145
+ class AlbumTwin < Disposable::Twin
146
+ property :id
147
+ property :is_online, readable: false, writeable: false
148
+ end
149
+
150
+ it do
151
+ twin = AlbumTwin.new(Song.new(1), is_online: true)
152
+ twin.id.must_equal 1
153
+ twin.is_online.must_equal true
154
+ end
139
155
  end
@@ -46,9 +46,9 @@ class SkipUnchangedTest < MiniTest::Spec
46
46
  twin.songs[0].title = "Resist Stance"
47
47
 
48
48
  # raises exceptions when setters are called.
49
- album.instance_eval { def name=; end }
50
- artist.instance_eval { def name=; end }
51
- song_with_composer.instance_eval { def title=; end }
49
+ album.instance_eval { def name=; raise; end }
50
+ artist.instance_eval { def name=; raise; end }
51
+ song_with_composer.instance_eval { def title=; raise; end }
52
52
 
53
53
  twin.sync
54
54
 
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class VirtualTest < MiniTest::Spec
4
+ class CreditCardTwin < Disposable::Twin
5
+ include Sync
6
+ property :credit_card_number, virtual: true # no read, no write, it's virtual.
7
+ end
8
+
9
+ let (:twin) { CreditCardTwin.new(Object.new) }
10
+
11
+ it {
12
+ twin.credit_card_number = "123"
13
+
14
+ twin.credit_card_number.must_equal "123" # this is still readable in the UI.
15
+
16
+ twin.sync
17
+
18
+ hash = {}
19
+ twin.sync do |nested|
20
+ hash = nested
21
+ end
22
+
23
+ hash.must_equal("credit_card_number"=> "123")
24
+ }
25
+ 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.4
4
+ version: 0.1.5
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-06-29 00:00:00.000000000 Z
11
+ date: 2015-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uber
@@ -202,6 +202,7 @@ files:
202
202
  - test/twin/sync_option_test.rb
203
203
  - test/twin/sync_test.rb
204
204
  - test/twin/twin_test.rb
205
+ - test/twin/virtual_test.rb
205
206
  - test/twin/writeable_test.rb
206
207
  homepage: https://github.com/apotonick/disposable
207
208
  licenses:
@@ -257,4 +258,5 @@ test_files:
257
258
  - test/twin/sync_option_test.rb
258
259
  - test/twin/sync_test.rb
259
260
  - test/twin/twin_test.rb
261
+ - test/twin/virtual_test.rb
260
262
  - test/twin/writeable_test.rb