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.



30
31
32
# File 'lib/iseshima_store/base.rb', line 30

def properties
  @properties
end

Instance Method Details

#attr_properties(*args) ⇒ Object



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

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

#destroy_allObject



32
33
34
# File 'lib/iseshima_store/base.rb', line 32

def destroy_all
  scoping.each(&:destroy)
end

#from_entity(entity) ⇒ Object



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

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

#scopingObject



36
37
38
# File 'lib/iseshima_store/base.rb', line 36

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