Sha256: c2f361c8a527b55926361f078dac638980ec0ae255b7a5176ffe31baa258b912

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require 'gdatastore_mapper/session'
require 'gdatastore_mapper/scoping'
require 'gdatastore_mapper/associations'
require 'gdatastore_mapper/persistence'
require 'gdatastore_mapper/persistence/class_methods'
require 'gdatastore_mapper/validations'

module GdatastoreMapper
  module Base
    extend ActiveSupport::Concern
    include ActiveModel::Model
    include ActiveModel::Validations
    include ActiveModel::Validations::Callbacks
    include ActiveModel::Callbacks

    attr_accessor :id, :created_at, :updated_at

    included do
      define_model_callbacks :save, :update, :destroy
      extend Scoping
      extend Associations
      include Persistence
      extend Persistence::ClassMethods
      extend Validations
    end

    module ClassMethods

      def attr_accessor(*vars)
        @attributes ||= []
        @attributes.concat vars
        super
      end

      def attributes
        @attributes.reject do |attr|
          [:id, :created_at, :updated_at].include? attr
        end
      end

      def from_entity entity
        record = self.new
        record.id = entity.key.id
        entity.properties.to_hash.each do |name, value|
          record.send "#{name}=", value if record.respond_to? "#{name}="
        end
        record
      end

    end

    def attributes
      self.class.attributes
    end

    def to_entity
      entity_timestamp
      attributes.each do |attribute|
        @entity[attribute] = self.send attribute if self.send attribute
      end
      @entity
    end

    private

    def entity_timestamp
      @entity = Google::Cloud::Datastore::Entity.new
      @entity.key = Google::Cloud::Datastore::Key.new self.class.to_s, id
      @entity['created_at'] = id ? self.created_at : Time.zone.now
      @entity['updated_at'] = Time.zone.now
    end

    def id_ model
      (model.to_s + '_id').to_sym
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.8 lib/gdatastore_mapper/base.rb
gdatastore_mapper-0.1.7 lib/gdatastore_mapper/base.rb