Module: Hyperion

Defined in:
lib/hyperion.rb,
lib/hyperion/key.rb,
lib/hyperion/sort.rb,
lib/hyperion/util.rb,
lib/hyperion/query.rb,
lib/hyperion/types.rb,
lib/hyperion/filter.rb,
lib/hyperion/format.rb,
lib/hyperion/memory.rb

Defined Under Namespace

Classes: FieldSpec, Filter, Format, Key, KindSpec, Memory, Query, Sort, Types, Util

Class Method Summary collapse

Class Method Details

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

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



125
126
127
# File 'lib/hyperion.rb', line 125

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

.datastoreObject

Returns the current datastore instance



41
42
43
# File 'lib/hyperion.rb', line 41

def self.datastore
  Thread.current[:datastore] || @datastore || raise('No Datastore installed')
end

.datastore=(datastore) ⇒ Object

Sets the active datastore



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

def self.datastore=(datastore)
  @datastore = datastore
end

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

Yields:

  • (kind_spec)


10
11
12
13
14
15
16
17
# File 'lib/hyperion.rb', line 10

def self.defentity(kind)
  kind = Format.format_kind(kind)
  kind_spec = KindSpec.new(kind)
  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(key) ⇒ Object

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



115
116
117
# File 'lib/hyperion.rb', line 115

def self.delete_by_key(key)
  datastore.delete_by_key(key)
end

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

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



120
121
122
# File 'lib/hyperion.rb', line 120

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

.find_by_key(key) ⇒ Object

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



83
84
85
# File 'lib/hyperion.rb', line 83

def self.find_by_key(key)
  unpack_record(datastore.find_by_key(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"


110
111
112
# File 'lib/hyperion.rb', line 110

def self.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)


78
79
80
# File 'lib/hyperion.rb', line 78

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

.new_datastore(name, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/hyperion.rb', line 52

def self.new_datastore(name, opts={})
  begin
    require "hyperion/#{name}"
  rescue LoadError
    raise "Can't find datastore implementation: #{name}"
  end
  ds_klass = Hyperion.const_get(Util.class_name(name.to_s))
  ds_klass.new(opts)
end

.pack(type, &block) ⇒ Object



19
20
21
# File 'lib/hyperion.rb', line 19

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

.packer_defined?(type) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/hyperion.rb', line 23

def self.packer_defined?(type)
  packers.has_key?(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>"}


68
69
70
# File 'lib/hyperion.rb', line 68

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

.save_many(records) ⇒ Object

Saves multiple records at once.



73
74
75
# File 'lib/hyperion.rb', line 73

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

.unpack(type, &block) ⇒ Object



27
28
29
# File 'lib/hyperion.rb', line 27

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

.unpacker_defined?(type) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hyperion.rb', line 31

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

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

Assigns the datastore within the given block



46
47
48
49
50
# File 'lib/hyperion.rb', line 46

def self.with_datastore(name, opts={})
  Util.bind(:datastore, new_datastore(name, opts)) do
    yield
  end
end