Sha256: 06d6044f64cd082135c5de0fde4bf9db0e5d5f03e430aeb668a04188818d852d

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require "google/cloud/datastore"
require "gdatastore_mapper/session"
require "gdatastore_mapper/scoping"
require "gdatastore_mapper/associations"
require "gdatastore_mapper/persistence"

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

    attr_accessor :id, :created_at, :updated_at

    def self.included(klass)
      klass.extend Scoping
      klass.extend Associations
      klass.include Persistence
    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

      def create params
        record = self.new params
        record.save
        record
      end

    end

    def attributes
      self.class.attributes
    end

    def to_entity
      entity = Google::Cloud::Datastore::Entity.new
      entity.key = Google::Cloud::Datastore::Key.new self.class.to_s, id

      attributes.each do |attribute|
        entity[attribute] = self.send attribute if self.send attribute
      end

      entity['created_at'] = id ? self.created_at : Time.zone.now
      entity["updated_at"] = Time.zone.now
      entity
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.1 lib/gdatastore_mapper/base.rb