lib/morph.rb in morph-0.2.0 vs lib/morph.rb in morph-0.2.1

- old
+ new

@@ -1,7 +1,7 @@ module Morph - VERSION = "0.2.0" + VERSION = "0.2.1" def self.included(base) base.extend ClassMethods base.send(:include, InstanceMethods) base.send(:include, MethodMissing) @@ -109,30 +109,29 @@ end hash end end - protected + def morph_method_missing symbol, *args + attribute = symbol.to_s.chomp '=' - def morph_method_missing symbol, *args - attribute = symbol.to_s.chomp '=' + if Object.instance_methods.include?(attribute) + raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'" + elsif argument_provided? args + base = self.class + base.adding_morph_method = true - if Object.instance_methods.include?(attribute) - raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'" - elsif argument_provided? args - base = self.class - base.adding_morph_method = true - - if block_given? - yield base, attribute - else - base.class_eval "attr_accessor :#{attribute}" - send(symbol, *args) - end - base.adding_morph_method = false + if block_given? + yield base, attribute + else + # base.class_eval "attr_accessor :#{attribute}" + base.class_eval "def #{attribute}; @#{attribute}; end; def #{attribute}=(value); @#{attribute} = value; end" + send(symbol, *args) end + base.adding_morph_method = false end + end private def argument_provided? args args.size > 0 && !args[0].nil? && !(args[0].is_a?(String) && args[0].strip.size == 0) @@ -141,8 +140,7 @@ def convert_to_morph_method_name label name = label.to_s.downcase.tr('()*',' ').gsub('%','percentage').strip.chomp(':').strip.gsub(/\s/,'_').squeeze('_') name = '_'+name if name =~ /^\d/ name end - end end