lib/immutable.rb in up_the_irons-immutable-0.1 vs lib/immutable.rb in up_the_irons-immutable-0.2
- old
+ new
@@ -1,31 +1,35 @@
module Immutable
+ class CannotOverrideMethod < StandardError; end
+
def self.included(mod)
mod.extend(ClassMethods)
end
module ClassMethods
def immutable_method(*args)
+ opts = args.last.is_a?(Hash) ? args.pop : {}
+
args.each do |method|
alias_method "orig_#{method}", method
end
- @args = args
+ @args = args; @opts = opts
module_eval do
def self.method_added(sym)
if @args
@args.each do |method|
- if method
- if sym == method.to_sym
- unless called_by_method_added
- self.module_eval <<-"end;"
- def #{method.to_s}(*args, &block)
- orig_#{method.to_s}(*args, &block)
- end
- end;
- end # called_by_method_added
- end # method.to_sym
- end # method
+ if method && sym == method.to_sym && !called_by_method_added
+ unless @opts[:silent]
+ raise CannotOverrideMethod, "Cannot override the immutable method: #{sym}"
+ end
+
+ self.module_eval <<-"end;"
+ def #{method.to_s}(*args, &block)
+ orig_#{method.to_s}(*args, &block)
+ end
+ end;
+ end
end # @args.each
end # @args
end # def self.method_added()
def self.method_undefined(sym)