Created
December 20, 2010 19:37
-
-
Save bhh/748873 to your computer and use it in GitHub Desktop.
attr_accessor_with_default rewrite to allow alias_method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 781092971105c6f31394978fef51ac4236146ff6 Mon Sep 17 00:00:00 2001 | |
From: Benjamin Huber <[email protected]> | |
Date: Sun, 12 Dec 2010 17:51:02 +0100 | |
Subject: [PATCH] attr_accessor_with_default rewrite to allow alias_method [#6099 state:resolved tagged:patch] | |
--- | |
.../core_ext/module/attr_accessor_with_default.rb | 14 +++++++++----- | |
.../module/attr_accessor_with_default_test.rb | 8 ++++++++ | |
2 files changed, 17 insertions(+), 5 deletions(-) | |
diff --git a/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb b/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb | |
index 0b67318..93cece1 100644 | |
--- a/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb | |
+++ b/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb | |
@@ -19,12 +19,16 @@ class Module | |
# attr_accessor_with_default(:element_name) { name.underscore } | |
# | |
def attr_accessor_with_default(sym, default = Proc.new) | |
- define_method(sym, block_given? ? default : Proc.new { default }) | |
+ define_method("#{sym}_default", block_given? ? default : Proc.new { default }) | |
module_eval(<<-EVAL, __FILE__, __LINE__ + 1) | |
- def #{sym}=(value) # def age=(value) | |
- class << self; attr_accessor :#{sym} end # class << self; attr_accessor :age end | |
- @#{sym} = value # @age = value | |
- end # end | |
+ def #{sym} # def age | |
+ (@#{sym}_marked ||= false) ? @#{sym} : #{sym}_default # (@age_marked ||= false) ? @age : age_default | |
+ end # end | |
+ | |
+ def #{sym}=(value) # def age=(value) | |
+ @#{sym}_marked = true # @age_marked = true | |
+ @#{sym} = value # @age = value | |
+ end # end | |
EVAL | |
end | |
end | |
diff --git a/activesupport/test/core_ext/module/attr_accessor_with_default_test.rb b/activesupport/test/core_ext/module/attr_accessor_with_default_test.rb | |
index b9b60c4..911f9ab 100644 | |
--- a/activesupport/test/core_ext/module/attr_accessor_with_default_test.rb | |
+++ b/activesupport/test/core_ext/module/attr_accessor_with_default_test.rb | |
@@ -25,6 +25,14 @@ class AttrAccessorWithDefaultTest < Test::Unit::TestCase | |
assert_nil(@instance.foo) | |
end | |
+ def test_with_aliased_method | |
+ @target.attr_accessor_with_default :bla, false | |
+ @target.send :alias_method, :bla?, :bla | |
+ assert_equal(false, @instance.bla?) | |
+ @instance.bla = nil | |
+ assert_nil @instance.bla? | |
+ end | |
+ | |
def test_invalid_args | |
assert_raise(ArgumentError) {@target.attr_accessor_with_default :foo} | |
end | |
-- | |
1.7.3.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment