Class: Hikki::Adapters::RedisCollection
- Inherits:
-
Collection
- Object
- Collection
- Hikki::Adapters::RedisCollection
- Defined in:
- lib/hikki/adapters/redis_collection.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#uuid_generator ⇒ Object
readonly
Returns the value of attribute uuid_generator.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #find(id) ⇒ Object
- #find_by(field, value, options = {}) ⇒ Object
- #has_index?(field) ⇒ Boolean
- #id_for(data) ⇒ Object
- #index(field) ⇒ Object
- #indexes ⇒ Object
-
#initialize(collection, connection, uuid_generator) ⇒ RedisCollection
constructor
A new instance of RedisCollection.
- #remove(id) ⇒ Object
- #remove_all ⇒ Object
- #save(data) ⇒ Object
Constructor Details
#initialize(collection, connection, uuid_generator) ⇒ RedisCollection
Returns a new instance of RedisCollection.
6 7 8 9 10 |
# File 'lib/hikki/adapters/redis_collection.rb', line 6 def initialize(collection, connection, uuid_generator) super(collection) @connection = connection @uuid_generator = uuid_generator end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
4 5 6 |
# File 'lib/hikki/adapters/redis_collection.rb', line 4 def connection @connection end |
#uuid_generator ⇒ Object (readonly)
Returns the value of attribute uuid_generator.
4 5 6 |
# File 'lib/hikki/adapters/redis_collection.rb', line 4 def uuid_generator @uuid_generator end |
Instance Method Details
#all(options = {}) ⇒ Object
28 29 30 31 |
# File 'lib/hikki/adapters/redis_collection.rb', line 28 def all(={}) = () connection.hvals(collection_key)[page_range()].map { |j| JSON.parse(j) } end |
#find(id) ⇒ Object
24 25 26 |
# File 'lib/hikki/adapters/redis_collection.rb', line 24 def find(id) JSON.parse(connection.hget(collection_key, id.to_s) || '{}') end |
#find_by(field, value, options = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/hikki/adapters/redis_collection.rb', line 33 def find_by(field, value, ={}) = () return find_by_index(field, value, ) if has_index?(field) all.select { |o| o.fetch(field.to_s) == value }[page_range()] end |
#has_index?(field) ⇒ Boolean
51 52 53 |
# File 'lib/hikki/adapters/redis_collection.rb', line 51 def has_index?(field) connection.sismember collection_indexes_key, field.to_s end |
#id_for(data) ⇒ Object
59 60 61 |
# File 'lib/hikki/adapters/redis_collection.rb', line 59 def id_for(data) data.fetch('id', uuid_generator.uuid).to_s end |
#index(field) ⇒ Object
12 13 14 15 |
# File 'lib/hikki/adapters/redis_collection.rb', line 12 def index(field) connection.sadd collection_indexes_key, field.to_s true end |
#indexes ⇒ Object
55 56 57 |
# File 'lib/hikki/adapters/redis_collection.rb', line 55 def indexes connection.smembers collection_indexes_key end |
#remove(id) ⇒ Object
39 40 41 42 43 |
# File 'lib/hikki/adapters/redis_collection.rb', line 39 def remove(id) remove_from_index(find(id)) connection.hdel collection_key, id.to_s true end |
#remove_all ⇒ Object
45 46 47 48 49 |
# File 'lib/hikki/adapters/redis_collection.rb', line 45 def remove_all indexes.each { |field| connection.del collection_index_key(field) } connection.del collection_indexes_key connection.del collection_key end |
#save(data) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/hikki/adapters/redis_collection.rb', line 17 def save(data) data = normalize_data(data) connection.hset collection_key, data['id'], data.to_json add_to_index(data) data end |