diff --git a/lib/disposable/twin/composition.rb b/lib/disposable/twin/composition.rb index e170f3e..fbb41bc 100644 --- a/lib/disposable/twin/composition.rb +++ b/lib/disposable/twin/composition.rb @@ -28,6 +28,22 @@ module ClassMethods def expose_class @expose_class ||= Class.new(Disposable::Composition).from(representer_class) end + + def on(model_key, &block) + builder = OnBuilder.new(model_key, self) + builder.instance_eval(&block) + end + + class OnBuilder + def initialize(model_key, definition) + @model_key = model_key + @definition = definition + end + + def property(name, options={}, &block) + @definition.property(name, options.merge(on: @model_key), &block) + end + end end def self.included(base) @@ -54,4 +70,4 @@ def save_model end end end -end \ No newline at end of file +end diff --git a/test/twin/composition_test.rb b/test/twin/composition_test.rb index 2e80bc7..7b8b3dc 100644 --- a/test/twin/composition_test.rb +++ b/test/twin/composition_test.rb @@ -11,8 +11,11 @@ class Request < Disposable::Twin property :song_id, on: :song, from: :id property :name, on: :requester - property :id, on: :requester property :captcha, readable: false, writeable: false, on: :requester # TODO: allow both, virtual with and without :on. + + on :requester do + property :id + end end module Model @@ -81,4 +84,4 @@ module Model song.instance_eval { def save; false; end } request.save.must_equal false end -end \ No newline at end of file +end