From 8882de60fc137c4534fbcf80edef9c236c771b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Sun, 8 May 2016 21:15:13 +0800 Subject: [PATCH] Add :accessor for avoiding defining accessors If we have an accessor defined before declaring a property in an included module, it will get overriden. The :accessor option allows us to specify whether we want Disposable::Twin to define accessors for that property. module Titleable def title # ... end def title=(value) # ... end end class Song < Disposable::Twin include Titleable property :title, accessor: false end --- lib/disposable/twin.rb | 3 ++- test/twin/accessor_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/twin/accessor_test.rb diff --git a/lib/disposable/twin.rb b/lib/disposable/twin.rb index f9e28b5..045bef0 100644 --- a/lib/disposable/twin.rb +++ b/lib/disposable/twin.rb @@ -34,6 +34,7 @@ def default_nested_class def property(name, options={}, &block) options[:private_name] ||= options.delete(:from) || name is_inherited = options.delete(:_inherited) + define_accessor = options.delete(:accessor) if options.delete(:virtual) options[:writeable] = options[:readable] = false @@ -42,7 +43,7 @@ def property(name, options={}, &block) options[:nested] = options.delete(:twin) if options[:twin] super(name, options, &block).tap do |definition| # super is Declarative::Schema::property. - create_accessors(name, definition) unless is_inherited + create_accessors(name, definition) unless is_inherited || define_accessor == false end end diff --git a/test/twin/accessor_test.rb b/test/twin/accessor_test.rb new file mode 100644 index 0000000..baae8ca --- /dev/null +++ b/test/twin/accessor_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +class AccessorTest < MiniTest::Spec + Song = Struct.new(:title) + + class SongForm < Disposable::Twin + def title + @title + end + + def title=(value) + @title = value.reverse if value + end + + property :title, accessor: false + end + + let (:song) { Song.new } + + let (:twin) { SongForm.new(song) } + + it { + twin.title = "Remedy" + twin.title.must_equal "ydemeR" + } +end