J
James Golick
So, I had this really cool idea for a syntax.
class Options
attr_accessor :something, :something_else
end
class Post
@@options ||= Options.new
def self.options(&block)
@@options.instance_eval &block if block_given?
@@options
end
end
Post.options.something = "something"
assert_equal "something", Post.options.something
Post.options do
something = "something"
something_else = "something_else
end
assert_equal "something", Post.options.something
assert_equal "something_else", Post.options.something_else
That's the idea anyway. A shorthand for scoping setters. If you want
to set one option, you call the whole method chain. If you want to set
multiple options, you use a block.
Unfortunately, the setters don't seem to work in the block without an
explicit self.
Post.options do
self.something = "something"
self.something_else = "something_else
end
...which defeats the entire purpose of the shorthand.
Anybody have any ideas?
class Options
attr_accessor :something, :something_else
end
class Post
@@options ||= Options.new
def self.options(&block)
@@options.instance_eval &block if block_given?
@@options
end
end
Post.options.something = "something"
assert_equal "something", Post.options.something
Post.options do
something = "something"
something_else = "something_else
end
assert_equal "something", Post.options.something
assert_equal "something_else", Post.options.something_else
That's the idea anyway. A shorthand for scoping setters. If you want
to set one option, you call the whole method chain. If you want to set
multiple options, you use a block.
Unfortunately, the setters don't seem to work in the block without an
explicit self.
Post.options do
self.something = "something"
self.something_else = "something_else
end
...which defeats the entire purpose of the shorthand.
Anybody have any ideas?