Navigation Menu

Skip to content

Commit

Permalink
Made instance var that holds state of whether we are already in our own
Browse files Browse the repository at this point in the history
method_added() unique.
  • Loading branch information
up_the_irons committed Sep 27, 2008
1 parent 8768bbd commit 08e3a55
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/immutable.rb
Expand Up @@ -16,14 +16,14 @@ def immutable_method(*args)
alias_method "#{UNIQ}_old_#{method}", method
end

@allow_method_override = false
instance_variable_set("@#{UNIQ}_in_method_added", false)

@args = args; @opts = opts
module_eval do
def self.method_added(sym)
if @args
@args.each do |method|
if method && sym == method.to_sym && !@allow_method_override
if method && sym == method.to_sym && !in_method_added?
unless @opts[:silent]
raise CannotOverrideMethod, "Cannot override the immutable method: #{sym}"
end
Expand All @@ -50,10 +50,14 @@ def self.method_removed(sym)
end # module_eval

def self.allow_method_override
@allow_method_override = true
instance_variable_set("@#{UNIQ}_in_method_added", true)
yield
ensure
@allow_method_override = false
instance_variable_set("@#{UNIQ}_in_method_added", false)
end

def self.in_method_added?
instance_variable_get("@#{UNIQ}_in_method_added")
end
end # def immutable_method()

Expand Down

0 comments on commit 08e3a55

Please sign in to comment.