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.



22
23
24
# File 'lib/iseshima_store/base.rb', line 22

def properties
  @properties
end

Instance Method Details

#attr_properties(*args) ⇒ Object



39
40
41
42
# File 'lib/iseshima_store/base.rb', line 39

def attr_properties(*args)
  attr_accessor(*args)
  @properties = args
end

#find(_id) ⇒ Object



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

def find(_id)
  datastore = IseshimaStore::Connection.current
  key = datastore.key(self.to_s, _id.to_i)
  entity = datastore.find(key)
  if entity
    from_entity(entity)
  else
    raise EntityNotFound.new("cannot find entity with id #{_id}")
  end
end

#find_by(hash) ⇒ Object



44
45
46
# File 'lib/iseshima_store/base.rb', line 44

def find_by(hash)
  where(hash).first
end

#from_entity(entity) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/iseshima_store/base.rb', line 48

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

#scopingObject



24
25
26
# File 'lib/iseshima_store/base.rb', line 24

def scoping
  IseshimaStore::Relation.new(self)
end

#search(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/iseshima_store/base.rb', line 57

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

  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