lib/morph.rb in morph-0.3.6 vs lib/morph.rb in morph-0.3.7

- old
+ new

@@ -16,11 +16,11 @@ require 'activesupport' end end module Morph - VERSION = "0.3.6" unless defined? Morph::VERSION + VERSION = "0.3.7" unless defined? Morph::VERSION class << self def generate_migrations object, options={} options[:ignore] ||= [] options[:belongs_to_id] ||= '' @@ -91,10 +91,19 @@ else raise 'hash must have single key' end end + def script_generate morphed_class, options={} + name = morphed_class.name.to_s.split('::').last + name = yield name if block_given? + generator = options[:generator] || 'model' + line = ["rails destroy #{generator} #{name}; rails generate #{generator} #{name}"] + morphed_class.morph_methods.select{|m| not(m =~ /=$/) }.each {|attribute| line << " #{attribute}:string"} + line.join('') + end + def included(base) base.extend ClassMethods base.send(:include, InstanceMethods) base.send(:include, MethodMissing) end @@ -209,27 +218,16 @@ methods += superclass.morph_methods end methods end - def adding_morph_method= true_or_false - @@adding_morph_method[self] = true_or_false + def add_morph_attribute attribute, *args + @@adding_morph_method[self] = true + class_eval "attr_accessor :#{attribute}" + @@adding_morph_method[self] = false end - def class_def name, &block - class_eval { define_method name, &block } - end - - def script_generate options={} - name = self.name.to_s.split('::').last - name = yield name if block_given? - generator = options[:generator] || 'model' - line = ["rails destroy #{generator} #{name}; rails generate #{generator} #{name}"] - morph_methods.select{|m| not(m =~ /=$/) }.each {|attribute| line << " #{attribute}:string"} - line.join('') - end - protected def method_added symbol if @@adding_morph_method[self] @@morph_methods[self][symbol.to_s] = true @@ -249,11 +247,15 @@ end module MethodMissing def method_missing symbol, *args is_writer = symbol.to_s =~ /=$/ - is_writer ? morph_method_missing(symbol, *args) : super + if is_writer + Morph::InstanceMethods::Helper.morph_method_missing(self, symbol, *args) + else + super + end end end module InstanceMethods @@ -278,11 +280,11 @@ # def morph attributes_or_label, value=nil if attributes_or_label.is_a? Hash attributes_or_label.each { |a, v| morph(a, v) } else - attribute = convert_to_morph_method_name(attributes_or_label) + attribute = Helper.convert_to_morph_method_name(attributes_or_label) send("#{attribute}=".to_sym, value) end end def morph_attributes @@ -303,41 +305,34 @@ end hash end end - def morph_method_missing symbol, *args - attribute = symbol.to_s.chomp '=' - if RUBY_VERSION >= "1.9" - attribute = attribute.to_sym - end + module Helper - 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 + def self.morph_method_missing object, symbol, *args + attribute = symbol.to_s.chomp '=' + if RUBY_VERSION >= "1.9" + attribute = attribute.to_sym + end - 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) + if Object.instance_methods.include?(attribute) + raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'" + elsif Helper.argument_provided? args + base = object.class + base.add_morph_attribute attribute + object.send(symbol, *args) end - base.adding_morph_method = false end - end - private - - def argument_provided? args + def self.argument_provided? args args.size > 0 && !args[0].nil? && !(args[0].is_a?(String) && args[0].strip.size == 0) end - def convert_to_morph_method_name label + def self.convert_to_morph_method_name label name = label.to_s.downcase.tr('()\-*',' ').gsub("'",' ').gsub('/',' ').gsub('%','percentage').strip.chomp(':').strip.gsub(/\s/,'_').squeeze('_') name = '_'+name if name =~ /^\d/ name end + end end end