Sha256: 301b7ff9b333835732e40b3c37a515481727ec90bb87bd74b9e4138cf0078191

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require "gdatastore_mapper/relation"

module GdatastoreMapper
  module Scoping

    def where condition, &block
      return nil unless condition.is_a?(Hash)
      dataset_run(where_query(condition), &block)
    end

    def find id
      return nil if id.nil?
      query = Google::Cloud::Datastore::Key.new self.to_s, id.to_i
      entities = GdatastoreMapper::Session.dataset.lookup query
      from_entity entities.first if entities.any?
    end

    def find_by condition
      return nil unless condition.is_a?(Hash)
      where(condition)&.first
    end

    def find_or_create condition
      return nil unless condition.is_a?(Hash)
      if record = where(condition)&.first
        record
      else
        create condition
      end
    end

    def order condition, &block
      return nil unless condition.is_a?(Hash)
      dataset_run(order_query(condition), &block)
    end

    def all &block
      order(created_at: :asc, &block)
    end
    alias_method :each, :all

    def first
      all.first
    end

    def last
      all.last
    end

    def count
      all.count
    end

    private

    def where_query condition
      query = Google::Cloud::Datastore::Query.new
      query.kind self.to_s
      condition.each do |property, value|
        query.where(property.to_s, '=', value)
      end
      query
    end

    def order_query condition
      query = Google::Cloud::Datastore::Query.new
      query.kind self.to_s
      condition.each do |property, value|
        query.order(property.to_s, value)
      end
      query
    end

    def dataset_run query, &block
      entities = GdatastoreMapper::Session.dataset.run query
      result = GdatastoreMapper::Relation.new(self, nil)
      entities.each do |entity|
        record = from_entity(entity)
        block.call(record) if block_given?
        record = find(record.id)
        result << record if record
      end
      result
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.7 lib/gdatastore_mapper/scoping.rb
gdatastore_mapper-0.1.6 lib/gdatastore_mapper/scoping.rb