Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making it possible to use nesting to define composition properties #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/disposable/twin/composition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -54,4 +70,4 @@ def save_model
end
end
end
end
end
7 changes: 5 additions & 2 deletions test/twin/composition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,4 +84,4 @@ module Model
song.instance_eval { def save; false; end }
request.save.must_equal false
end
end
end