disposable 0.0.7 → 0.0.8
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/CHANGES.md +4 -0
- data/lib/disposable/twin.rb +3 -2
- data/lib/disposable/twin/builder.rb +46 -0
- data/lib/disposable/version.rb +1 -1
- data/test/twin/twin_test.rb +52 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbd96277db19aa9fdf0cb9d0b46942cb2cbd2983
|
4
|
+
data.tar.gz: d3cc09426627c5bcc459b80d1eb90deaf66268f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df682d54394835e9d682f3a1e2815be97d849640f7c4b0a171dc31cd2ccb431e24e47df3dcbf019c1bc87a93638dc0239afabb0cb63f513feaf3892dafeb20af
|
7
|
+
data.tar.gz: f6023c52816ca6ba2636d05b1cd49fe660d68bfd4af1cff88f45142c844e1b29b6da3732ff9dfc805aa4ae1c250d64ab840aaac287215352ecb73c393b7e70e3
|
data/CHANGES.md
CHANGED
data/lib/disposable/twin.rb
CHANGED
@@ -3,8 +3,9 @@ require 'representable/decorator'
|
|
3
3
|
require 'representable/hash'
|
4
4
|
require 'disposable/twin/representer'
|
5
5
|
require 'disposable/twin/option'
|
6
|
+
require 'disposable/twin/builder'
|
6
7
|
|
7
|
-
# Twin.new(model/composition hash, options)
|
8
|
+
# Twin.new(model/composition hash/hash, options)
|
8
9
|
# assign hash to @fields
|
9
10
|
# write: write to @fields
|
10
11
|
# sync/save is the only way to write back to the model.
|
@@ -17,7 +18,7 @@ module Disposable
|
|
17
18
|
|
18
19
|
|
19
20
|
def self.property(name, options={}, &block)
|
20
|
-
options[:private_name]
|
21
|
+
options[:private_name] = options.delete(:as) || name
|
21
22
|
options[:pass_options] = true
|
22
23
|
|
23
24
|
representer_class.property(name, options, &block).tap do |definition|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Disposable
|
2
|
+
class Twin
|
3
|
+
# Allows setting a twin class for a host object (e.g. a cell, a form, or a representer) using ::twin
|
4
|
+
# and imports a method #build_twin to initialize this twin.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# class SongTwin < Disposable::Twin
|
9
|
+
# properties :id, :title
|
10
|
+
# option :is_released
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# class Cell
|
14
|
+
# include Disposable::Twin::Builder
|
15
|
+
# twin SongTwin
|
16
|
+
#
|
17
|
+
# def initialize(model, options)
|
18
|
+
# @twin = build_twin(model, options)
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# An optional block passed to ::twin will be called per property yielding the Definition instance.
|
23
|
+
module Builder
|
24
|
+
def self.included(base)
|
25
|
+
base.class_eval do
|
26
|
+
extend Uber::InheritableAttr
|
27
|
+
inheritable_attr :twin_class
|
28
|
+
extend ClassMethods
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def twin(twin_class, &block)
|
34
|
+
twin_class.representer_class.representable_attrs.each { |dfn| yield(dfn) } if block_given?
|
35
|
+
self.twin_class = twin_class
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def build_twin(*args)
|
42
|
+
self.class.twin_class.new(*args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/disposable/version.rb
CHANGED
data/test/twin/twin_test.rb
CHANGED
@@ -161,4 +161,56 @@ class TwinOptionTest < TwinTest
|
|
161
161
|
it { Song.new(song, preview?: true, highlight?: false).preview?.must_equal true }
|
162
162
|
end
|
163
163
|
|
164
|
+
|
165
|
+
class TwinBuilderTest < MiniTest::Spec
|
166
|
+
class Twin < Disposable::Twin
|
167
|
+
property :id
|
168
|
+
property :title
|
169
|
+
option :is_released
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "without property setup" do
|
173
|
+
class Host
|
174
|
+
include Disposable::Twin::Builder
|
175
|
+
|
176
|
+
twin Twin
|
177
|
+
|
178
|
+
def initialize(*args)
|
179
|
+
@model = build_twin(*args)
|
180
|
+
end
|
181
|
+
|
182
|
+
attr_reader :model
|
183
|
+
end
|
184
|
+
|
185
|
+
subject { Host.new(TwinTest::Model::Song.new(1, "Saturday Night"), is_released: true) }
|
186
|
+
|
187
|
+
# model is simply the twin.
|
188
|
+
it { subject.respond_to?(:title).must_equal false }
|
189
|
+
it { subject.model.id.must_equal 1 }
|
190
|
+
it { subject.model.title.must_equal "Saturday Night" }
|
191
|
+
it { subject.model.is_released.must_equal true }
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
describe "without property setup" do
|
196
|
+
class HostWithReaders
|
197
|
+
include Disposable::Twin::Builder
|
198
|
+
|
199
|
+
extend Forwardable
|
200
|
+
twin(Twin) { |dfn| def_delegator :@model, dfn.name }
|
201
|
+
|
202
|
+
def initialize(*args)
|
203
|
+
@model = build_twin(*args)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
subject { HostWithReaders.new(TwinTest::Model::Song.new(1, "Saturday Night"), is_released: true) }
|
208
|
+
|
209
|
+
# both twin gets created and reader method defined.
|
210
|
+
it { subject.id.must_equal 1 }
|
211
|
+
it { subject.title.must_equal "Saturday Night" }
|
212
|
+
it { subject.is_released.must_equal true }
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
164
216
|
# TODO: test coercion!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: disposable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/disposable.rb
|
111
111
|
- lib/disposable/composition.rb
|
112
112
|
- lib/disposable/twin.rb
|
113
|
+
- lib/disposable/twin/builder.rb
|
113
114
|
- lib/disposable/twin/composition.rb
|
114
115
|
- lib/disposable/twin/finders.rb
|
115
116
|
- lib/disposable/twin/new.rb
|
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
144
|
version: '0'
|
144
145
|
requirements: []
|
145
146
|
rubyforge_project:
|
146
|
-
rubygems_version: 2.2.
|
147
|
+
rubygems_version: 2.2.2
|
147
148
|
signing_key:
|
148
149
|
specification_version: 4
|
149
150
|
summary: Domain-Oriented Refactoring Framework.
|
@@ -152,4 +153,3 @@ test_files:
|
|
152
153
|
- test/test_helper.rb
|
153
154
|
- test/twin/composition_test.rb
|
154
155
|
- test/twin/twin_test.rb
|
155
|
-
has_rdoc:
|