Sha256: b4b5c8cc33be819742ecfc1e281e07d17156398e1523cf042051ff4b267e0403

Contents?: true

Size: 902 Bytes

Versions: 1

Compression:

Stored size: 902 Bytes

Contents

# encoding: utf-8
module Dynamoid #:nodoc:

  # This is the base module for all domain objects that need to be persisted to
  # the database as documents.
  module Document
    extend ActiveSupport::Concern
    include Dynamoid::Components

    attr_accessor :new_record
    alias :new_record? :new_record
    
    def initialize(attrs = {})
      @new_record = true
      @attributes ||= {}
      self.class.attributes.each {|att| write_attribute(att, attrs[att])}
    end
    
    def persisted?
      !new_record?
    end
    
    def ==(other)
      other.respond_to?(:id) && other.id == self.id
    end
    
    module ClassMethods
      def create(attrs = {})
        obj = self.new(attrs)
        obj.run_callbacks(:create) do
          obj.save && obj.new_record = false
        end
        obj
      end
      
      def build(attrs = {})
        self.new(attrs)
      end
    end
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dynamoid-0.1.1 lib/dynamoid/document.rb