Class: Endymion::API

Inherits:
Object
  • Object
show all
Defined in:
lib/endymion/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datastore) ⇒ API

Returns a new instance of API.


13
14
15
# File 'lib/endymion/api.rb', line 13

def initialize(datastore)
  @datastore = datastore
end

Instance Attribute Details

#datastoreObject (readonly)

Returns the value of attribute datastore.


11
12
13
# File 'lib/endymion/api.rb', line 11

def datastore
  @datastore
end

Instance Method Details

#count_by_kind(kind, args = {}) ⇒ Object

Counts records of the specified kind that match the filters provided.


129
130
131
# File 'lib/endymion/api.rb', line 129

def count_by_kind(kind, args={})
  datastore.count(build_query(kind, args))
end

#create(record) ⇒ Object

Creates a record. While save delegates to a create or update based on the result of new?, this always creates. This allows you to set the key explicitly


72
73
74
# File 'lib/endymion/api.rb', line 72

def create(record)
  create_many([record]).first
end

#create_many(records) ⇒ Object

Create many records at once


77
78
79
# File 'lib/endymion/api.rb', line 77

def create_many(records)
  unpack_records(datastore.create(pack_records(records)))
end

#defentity(kind) {|kind_spec| ... } ⇒ Object

Yields:

  • (kind_spec)

17
18
19
20
21
22
23
24
# File 'lib/endymion/api.rb', line 17

def defentity(kind)
  kind = Format.format_kind(kind)
  kind_spec = KindSpec.new(kind, self)
  yield(kind_spec)
  save_kind_spec(kind_spec)
  pack(kind.to_sym) {|value| pack_record((value || {}).merge(:kind => kind))}
  unpack(kind.to_sym) {|value| unpack_record((value || {}).merge(:kind => kind))}
end

#delete_by_key(kind, key) ⇒ Object

Removes the record stored with the given key. Returns nil no matter what.


119
120
121
# File 'lib/endymion/api.rb', line 119

def delete_by_key(kind, key)
  datastore.delete_by_key(kind, key)
end

#delete_by_kind(kind, args = {}) ⇒ Object

Deletes all records of the specified kind that match the filters provided.


124
125
126
# File 'lib/endymion/api.rb', line 124

def delete_by_kind(kind, args={})
  datastore.delete(build_query(kind, args))
end

#find_by_key(kind, key) ⇒ Object

Retrieves the value associated with the given key from the datastore. nil if it doesn’t exist.


87
88
89
# File 'lib/endymion/api.rb', line 87

def find_by_key(kind, key)
  unpack_record(datastore.find_by_key(kind, key))
end

#find_by_kind(kind, args = {}) ⇒ Object

Returns all records of the specified kind that match the filters provided.

find_by_kind(:dog) # returns all records with :kind of \"dog\"
find_by_kind(:dog, :filters => [[:name, '=', "Fido"]]) # returns all dogs whos name is Fido
find_by_kind(:dog, :filters => [[:age, '>', 2], [:age, '<', 5]]) # returns all dogs between the age of 2 and 5 (exclusive)
find_by_kind(:dog, :sorts => [[:name, :asc]]) # returns all dogs in alphebetical order of their name
find_by_kind(:dog, :sorts => [[:age, :desc], [:name, :asc]]) # returns all dogs ordered from oldest to youngest, and gos of the same age ordered by name
find_by_kind(:dog, :limit => 10) # returns upto 10 dogs in undefined order
find_by_kind(:dog, :sorts => [[:name, :asc]], :limit => 10) # returns upto the first 10 dogs in alphebetical order of their name
find_by_kind(:dog, :sorts => [[:name, :asc]], :limit => 10, :offset => 10) # returns the second set of 10 dogs in alphebetical order of their name

Filter operations and acceptable syntax:

"=" "eq"
"<" "lt"
"<=" "lte"
">" "gt"
">=" "gte"
"!=" "not"
"contains?" "contains" "in?" "in"

Sort orders and acceptable syntax:

:asc "asc" :ascending "ascending"
:desc "desc" :descending "descending"

114
115
116
# File 'lib/endymion/api.rb', line 114

def find_by_kind(kind, args={})
  unpack_records(datastore.find(build_query(kind, args)))
end

#new?(record) ⇒ Boolean

Returns true if the record is new (not saved/doesn’t have a :key), false otherwise.

Returns:

  • (Boolean)

82
83
84
# File 'lib/endymion/api.rb', line 82

def new?(record)
  !record.has_key?(:key)
end

#pack(type, &block) ⇒ Object


26
27
28
# File 'lib/endymion/api.rb', line 26

def pack(type, &block)
  packers[type] = block
end

#packer_defined?(type) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
# File 'lib/endymion/api.rb', line 30

def packer_defined?(type)
  packers.has_key?(type)
end

#packer_for(type) ⇒ Object

NOTE: these methods were marked as private in Hyperion, but were actually accessed from outside, in the FieldSpec class. Temporarily made public until things are cleand up


138
139
140
# File 'lib/endymion/api.rb', line 138

def packer_for(type)
  @packers[type]
end

#save(record, attrs = {}) ⇒ Object

Saves a record. Any additional parameters will get merged onto the record before it is saved.

Hyperion.save({:kind => :foo})
=> {:kind=>"foo", :key=>"<generated key>"}
Hyperion.save({:kind => :foo}, :value => :bar)
=> {:kind=>"foo", :value=>:bar, :key=>"<generated key>"}

61
62
63
# File 'lib/endymion/api.rb', line 61

def save(record, attrs={})
  save_many([record.merge(attrs || {})]).first
end

#save_many(records) ⇒ Object

Saves multiple records at once.


66
67
68
# File 'lib/endymion/api.rb', line 66

def save_many(records)
  unpack_records(datastore.save(pack_records(records)))
end

#unpack(type, &block) ⇒ Object


34
35
36
# File 'lib/endymion/api.rb', line 34

def unpack(type, &block)
  unpackers[type] = block
end

#unpacker_defined?(type) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/endymion/api.rb', line 38

def unpacker_defined?(type)
  unpackers.has_key?(type)
end

#unpacker_for(type) ⇒ Object


142
143
144
# File 'lib/endymion/api.rb', line 142

def unpacker_for(type)
  @unpackers[type]
end

#with_datastore(name, opts = {}) ⇒ Object

Assigns the datastore within the given block


43
44
45
46
47
48
49
50
51
52
# File 'lib/endymion/api.rb', line 43

def with_datastore(name, opts={})
  old_datastore = @datastore
  @datastore = Endymion.new_datastore(name, opts)
  begin
    yield
  rescue
    @datastore = old_datastore
    raise
  end
end