From ee8b43731f37dfec98119fab15462ba9186b570f Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Thu, 3 Dec 2015 19:34:54 +0300 Subject: [PATCH] Implement :struct options for properties. --- README.md | 8 +++----- lib/disposable/twin.rb | 8 +++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 93a7aa4..1b480f9 100644 --- a/README.md +++ b/README.md @@ -366,14 +366,12 @@ Twins can also map hash properties, e.g. from a deeply nested serialized JSON co album.permissions #=> {admin: {read: true, write: true}, user: {destroy: false}} ``` -Map that using the `Struct` module. +Map that using the `:struct` option that automatically adds `Disposable::Twin::Struct` feature. ```ruby class AlbumTwin < Disposable::Twin - property :permissions do - include Struct - property :admin do - include Struct + property :permissions, struct: true do + property :admin, struct: true do property :read property :write end diff --git a/lib/disposable/twin.rb b/lib/disposable/twin.rb index b3d9245..b6eccbc 100644 --- a/lib/disposable/twin.rb +++ b/lib/disposable/twin.rb @@ -44,7 +44,13 @@ def property(name, options={}, &block) options[:nested] = options.delete(:twin) if options[:twin] - super(name, options, &block).tap do |definition| + class_block = proc do + require 'disposable/twin/struct' + include Disposable::Twin::Struct if options[:struct] + class_eval(&block) + end if block + + super(name, options, &class_block).tap do |definition| create_accessors(name, definition) end end