Module: IseshimaStore::Base::ClassMethods

Defined in:
lib/iseshima_store/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/iseshima_store/base.rb', line 10

def properties
  @properties
end

Instance Method Details

#attr_properties(*args) ⇒ Object



12
13
14
# File 'lib/iseshima_store/base.rb', line 12

def attr_properties(*args)
  @properties = args
end

#from_entity(entity) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/iseshima_store/base.rb', line 16

def from_entity(entity)
  instance = self.new
  instance.id = entity.key.id
  entity.properties.to_hash.each do |name, value|
    instance.send "#{name}=", value
  end
  instance
end

#query(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iseshima_store/base.rb', line 25

def query(options = {})
  query = Gcloud::Datastore::Query.new
  query.kind(self.to_s)
  query.limit options[:limit]   if options[:limit]
  query.cursor options[:cursor] if options[:cursor]

  results = IseshimaStore::Connection.current.run(query)
  records = results.map { |entity| from_entity(entity) }

  if options[:limit] && results.size == options[:limit]
    next_cursor = results.cursor
  end

  { records: records, cursor: next_cursor }
end