Sha256: 006af39bb155448c46546390166e2731fd3d18778692f5110a89db4223c70102

Contents?: true

Size: 1.84 KB

Versions: 26

Compression:

Stored size: 1.84 KB

Contents

require 'active_support/core_ext/module/aliasing'

module MongoModel
  module Attributes
    extend ActiveSupport::Concern
    
    def initialize(attrs={})
      self.attributes = (attrs || {})
      yield self if block_given?
    end
    
    def attributes
      @attributes ||= Attributes::Store.new(self)
    end
    
    def attributes=(attrs)
      attrs.each do |attr, value|
        if respond_to?("#{attr}=")
          send("#{attr}=", value)
        else
          write_attribute(attr, value)
        end
      end
    end
    
    def freeze
      attributes.freeze; self
    end
    
    def frozen?
      attributes.frozen?
    end
    
    # Returns duplicated record with unfreezed attributes.
    def dup
      obj = super
      obj.instance_variable_set('@attributes', instance_variable_get('@attributes').dup)
      obj
    end
    
    def to_mongo
      attributes.to_mongo
    end
    
    def embedded_documents
      docs = []
      
      docs.concat attributes.values.select { |attr| attr.is_a?(EmbeddedDocument) }
      
      attributes.values.select { |attr| attr.is_a?(Collection) }.each do |collection|
        docs.concat collection.embedded_documents
      end
      
      attributes.values.select { |attr| attr.is_a?(Map) && attr.to <= EmbeddedDocument }.each do |map|
        docs.concat map.values
      end
      
      docs
    end
    
    module ClassMethods
      def from_mongo(hash)
        if hash
          doc = class_for_type(hash['_type']).new
          doc.attributes.load!(hash)
          doc
        end
      end
    
    private
      def class_for_type(type)
        klass = type.constantize
        
        if klass.ancestors.include?(self)
          klass
        else
          raise DocumentNotFound, "Document not of the correct type (got #{type})"
        end
      rescue NameError
        self
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
mongomodel-0.4.1 lib/mongomodel/concerns/attributes.rb
mongomodel-0.4.0 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.6 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.5 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.4 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.3 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.2 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.1 lib/mongomodel/concerns/attributes.rb
mongomodel-0.3.0 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.20 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.19 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.18 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.17 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.16 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.15 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.14 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.13 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.12 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.11 lib/mongomodel/concerns/attributes.rb
mongomodel-0.2.10 lib/mongomodel/concerns/attributes.rb