Class: Yuki::Store::AbstractStore
- Inherits:
-
Object
- Object
- Yuki::Store::AbstractStore
- Defined in:
- lib/store/abstract.rb
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidStore
Instance Method Summary collapse
-
#<<(val) ⇒ Object
Creates a new value.
-
#[](key) ⇒ Object
Gets an value by key.
-
#[]=(key, val) ⇒ Object
Merges changes into an value by key.
- #any? ⇒ Boolean
- #delete!(key) ⇒ Object
- #empty? ⇒ Boolean
-
#filter(conditions = {}, &blk) ⇒ Object
expects conditions = { :attr => [:operation, :value], :limit => [:offset, :max] :order => [:attr, :direction] } todo db.union( db.prepare_query{|q| q.add(att, op, val)}).
- #keys ⇒ Object
-
#union(conditions) ⇒ Object
unioned_conditions = db.union([ { :attr => [:op, :val] } { :attr => [:op, :val2] } ]).
-
#valid? ⇒ Boolean
Determines if the current state of the store is valid.
Instance Method Details
#<<(val) ⇒ Object
Creates a new value. store << { ‘foo’ => ‘bar’ } => { ‘foo’ => ‘bar’, :pk => ‘1’ }
82 83 84 85 86 87 |
# File 'lib/store/abstract.rb', line 82 def <<(val) open { |db| key = key!(db, val) (db[key] = stringify_keys(val).without('pk')).merge(:pk => key) } end |
#[](key) ⇒ Object
Gets an value by key. store => { ‘foo’ => ‘bar’, :pk => ‘1’ }
91 92 93 |
# File 'lib/store/abstract.rb', line 91 def [](key) open { |db| db[key] } end |
#[]=(key, val) ⇒ Object
Merges changes into an value by key. store = { ‘foo’ => ‘bar’, ‘baz’ => ‘boo’ } => …
97 98 99 100 101 102 103 |
# File 'lib/store/abstract.rb', line 97 def []=(key, val) open { |db| db[key] = (db[key] || {}).merge(val) }.merge({ 'pk' => key }) end |
#any? ⇒ Boolean
72 73 74 |
# File 'lib/store/abstract.rb', line 72 def any? open { |db| db.any? } end |
#delete!(key) ⇒ Object
64 65 66 |
# File 'lib/store/abstract.rb', line 64 def delete!(key) open { |db| db.delete(key) } end |
#empty? ⇒ Boolean
76 77 78 |
# File 'lib/store/abstract.rb', line 76 def empty? !any? end |
#filter(conditions = {}, &blk) ⇒ Object
expects conditions =
:attr => [:operation, :value],
:limit => [:offset, :max]
:order => [:attr, :direction]
todo db.union( db.prepare_query{|q| q.add(att, op, val)})
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/store/abstract.rb', line 29 def filter(conditions = {}, &blk) ordering = extract_ordering!(conditions) max, offset = *extract_limit!(conditions) open { |db| db.query { |q| prepare_conditions(conditions) { |attr, op, val| q.add_condition(attr, op, val) } q.order_by(ordering[0]) q.limit(max, offset) if max && offset } } end |
#keys ⇒ Object
68 69 70 |
# File 'lib/store/abstract.rb', line 68 def keys open { |db| db.keys } end |
#union(conditions) ⇒ Object
unioned_conditions = db.union([
{ :attr => [:op, :val] }
{ :attr => [:op, :val2] }
])
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/store/abstract.rb', line 47 def union(conditions) ordering = extract_ordering!(conditions) max, offset = *extract_limit!(conditions) open { |db| queries = conditions.inject([]) { |arr, cond| prepare_conditions(cond) { |attr, op, val| db.prepare_query { |q| q.add(attr, op, val) arr << q } } arr } db.union(*queries).map { |k, v| v.merge!(:pk => k) } } end |
#valid? ⇒ Boolean
Determines if the current state of the store is valid
17 |
# File 'lib/store/abstract.rb', line 17 def valid?; false; end |