Sha256: 493b40b0c37c7fc2b9c4ae035ecde8f75f11765fa083d598f99713b0272aad90

Contents?: true

Size: 1.42 KB

Versions: 31

Compression:

Stored size: 1.42 KB

Contents

module MongoModel
  module AttributeMethods
    extend ActiveSupport::Concern
    
    include ActiveModel::AttributeMethods
    
    module ClassMethods
      # Generates all the attribute related methods for defined properties
      # accessors, mutators and query methods.
      def define_attribute_methods
        super(properties.keys)
      end
      
      def property(*args)
        property = super
        undefine_attribute_methods
        property
      end
    end
    
    def method_missing(method_id, *args, &block)
      # If we haven't generated any methods yet, generate them, then
      # see if we've created the method we're looking for.
      unless self.class.attribute_methods_generated?
        self.class.define_attribute_methods
        method_name = method_id.to_s
        
        guard_private_attribute_method!(method_name, args)
        
        if self.class.generated_attribute_methods.method_defined?(method_name)
          return self.send(method_id, *args, &block)
        end
      end
      
      super
    end
    
    def respond_to?(*args)
      self.class.define_attribute_methods
      super
    end
    
    def clone_attribute_value(attribute_name)
      value = read_attribute(attribute_name)
      value.duplicable? ? value.clone : value
    rescue TypeError, NoMethodError
      value
    end
    
  protected
    def attribute_method?(attr_name)
      properties.has_key?(attr_name)
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
mongomodel-0.3.3 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.3.2 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.3.1 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.3.0 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.20 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.19 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.18 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.17 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.16 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.15 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.14 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.13 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.12 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.11 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.10 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.9 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.8 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.7 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.6 lib/mongomodel/concerns/attribute_methods.rb
mongomodel-0.2.5 lib/mongomodel/concerns/attribute_methods.rb