disposable 0.4.1 → 0.4.2
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 +4 -4
- data/.travis.yml +6 -2
- data/CHANGES.md +5 -0
- data/lib/disposable/twin.rb +1 -1
- data/lib/disposable/twin/definitions.rb +13 -10
- data/lib/disposable/twin/property_processor.rb +5 -5
- data/lib/disposable/version.rb +1 -1
- data/test/twin/coercion_test.rb +2 -2
- data/test/twin/property_processor_test.rb +45 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5456dc9441dfd2abd73248171b431ebc61aa999
|
4
|
+
data.tar.gz: 1dddb07f693e9d894387ee25d9127b15d4ceadcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 362fd4826566616d0c8f1129b5898f27a1f7c342e4472843ea406fdcd9dc0782d2188e2e5bf228bc225b6607f2cefbfc4619fe61f7edfaf2616ebc84bb635721
|
7
|
+
data.tar.gz: 287ed98cc543a4cee24aafa03ffc7c372fc1cb7e904c11618d8b7caa5afda2839960645db873a32edfce2d035fc23e92618f66a4e1c5bbbdb8e5c13e995532b4
|
data/.travis.yml
CHANGED
data/CHANGES.md
CHANGED
data/lib/disposable/twin.rb
CHANGED
@@ -7,22 +7,25 @@ class Disposable::Twin
|
|
7
7
|
def setter
|
8
8
|
"#{self[:name]}="
|
9
9
|
end
|
10
|
-
end
|
11
|
-
|
12
|
-
module DefinitionsEach
|
13
|
-
def each(options={})
|
14
|
-
return self unless block_given?
|
15
10
|
|
16
|
-
|
11
|
+
# :private:
|
12
|
+
Filter = ->(definitions, options) do
|
13
|
+
definitions.collect do |dfn|
|
17
14
|
next if options[:exclude] and options[:exclude].include?(dfn[:name])
|
18
15
|
next if options[:scalar] and dfn[:collection]
|
19
16
|
next if options[:collection] and ! dfn[:collection]
|
20
17
|
next if options[:twin] and ! dfn[:nested]
|
18
|
+
dfn
|
19
|
+
end.compact
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
require "delegate"
|
23
|
+
class Each < SimpleDelegator
|
24
|
+
def each(options={})
|
25
|
+
return __getobj__ unless block_given?
|
24
26
|
|
25
|
-
|
27
|
+
Definition::Filter.(__getobj__, options).each { |dfn| yield dfn }
|
28
|
+
end
|
26
29
|
end
|
27
30
|
end
|
28
|
-
end
|
31
|
+
end
|
@@ -3,9 +3,10 @@
|
|
3
3
|
#
|
4
4
|
# For a scalar property, this will be run once and yield the property's value.
|
5
5
|
# For a collection, this is run per item and yields the item.
|
6
|
+
#:private:
|
6
7
|
class Disposable::Twin::PropertyProcessor
|
7
8
|
def initialize(definition, twin, value=nil)
|
8
|
-
value
|
9
|
+
value ||= twin.send(definition.getter) # DISCUSS: should we decouple definition and value, here?
|
9
10
|
@definition = definition
|
10
11
|
@value = value
|
11
12
|
end
|
@@ -20,12 +21,11 @@ class Disposable::Twin::PropertyProcessor
|
|
20
21
|
|
21
22
|
private
|
22
23
|
def collection!
|
23
|
-
|
24
|
-
(@value || []).collect { |nested_twin| yield(nested_twin) }
|
24
|
+
(@value || []).each_with_index.collect { |nested_twin, i| yield(nested_twin, i) }
|
25
25
|
end
|
26
26
|
|
27
27
|
def property!
|
28
28
|
twin = @value or return nil
|
29
|
-
|
29
|
+
yield(twin)
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
data/lib/disposable/version.rb
CHANGED
data/test/twin/coercion_test.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class PropertyProcessorTest < Minitest::Spec
|
4
|
+
Album = Struct.new(:title, :artist, :songs)
|
5
|
+
Artist = Struct.new(:name)
|
6
|
+
Song = Struct.new(:id)
|
7
|
+
|
8
|
+
class AlbumTwin < Disposable::Twin
|
9
|
+
property :title
|
10
|
+
|
11
|
+
property :artist do
|
12
|
+
property :name
|
13
|
+
end
|
14
|
+
|
15
|
+
collection :songs do
|
16
|
+
property :id
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "collection" do
|
21
|
+
let(:twin) { AlbumTwin.new(Album.new("Live!", Artist.new, [Song.new(1), Song.new(2)])) }
|
22
|
+
it "yields twin, index" do
|
23
|
+
called = []
|
24
|
+
Disposable::Twin::PropertyProcessor.new(twin.class.definitions.get(:songs), twin).() { |v, i| called << [v.model, i] }
|
25
|
+
|
26
|
+
called.inspect.must_equal %{[[#<struct PropertyProcessorTest::Song id=1>, 0], [#<struct PropertyProcessorTest::Song id=2>, 1]]}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "yields twin" do
|
30
|
+
called = []
|
31
|
+
Disposable::Twin::PropertyProcessor.new(twin.class.definitions.get(:songs), twin).() { |v| called << [v.model] }
|
32
|
+
|
33
|
+
called.inspect.must_equal %{[[#<struct PropertyProcessorTest::Song id=1>], [#<struct PropertyProcessorTest::Song id=2>]]}
|
34
|
+
end
|
35
|
+
|
36
|
+
it "allows nil collection" do
|
37
|
+
twin = AlbumTwin.new(Album.new("Live!", Artist.new, nil))
|
38
|
+
|
39
|
+
called = []
|
40
|
+
Disposable::Twin::PropertyProcessor.new(twin.class.definitions.get(:songs), twin).() { |v, i| called << [v.model, i] }
|
41
|
+
|
42
|
+
called.inspect.must_equal %{[]}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
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.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uber
|
@@ -246,6 +246,7 @@ files:
|
|
246
246
|
- test/twin/option_test.rb
|
247
247
|
- test/twin/parent_test.rb
|
248
248
|
- test/twin/process_inline_test.rb
|
249
|
+
- test/twin/property_processor_test.rb
|
249
250
|
- test/twin/readable_test.rb
|
250
251
|
- test/twin/save_test.rb
|
251
252
|
- test/twin/setup_test.rb
|
@@ -277,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
277
278
|
version: '0'
|
278
279
|
requirements: []
|
279
280
|
rubyforge_project:
|
280
|
-
rubygems_version: 2.
|
281
|
+
rubygems_version: 2.5.1
|
281
282
|
signing_key:
|
282
283
|
specification_version: 4
|
283
284
|
summary: Decorators on top of your ORM layer with change tracking, collection semantics
|
@@ -309,6 +310,7 @@ test_files:
|
|
309
310
|
- test/twin/option_test.rb
|
310
311
|
- test/twin/parent_test.rb
|
311
312
|
- test/twin/process_inline_test.rb
|
313
|
+
- test/twin/property_processor_test.rb
|
312
314
|
- test/twin/readable_test.rb
|
313
315
|
- test/twin/save_test.rb
|
314
316
|
- test/twin/setup_test.rb
|