Class: JsonStore
- Inherits:
-
Object
- Object
- JsonStore
- Defined in:
- lib/json_store.rb
Instance Method Summary collapse
- #all ⇒ Object
- #all_as_json ⇒ Object
- #clear ⇒ Object
- #db_path ⇒ Object
- #get(key) ⇒ Object
- #get_as_json(key) ⇒ Object
- #get_json_opts ⇒ Object
-
#initialize(db) ⇒ JsonStore
constructor
A new instance of JsonStore.
- #merge(direction = :into_remote) ⇒ Object
- #pull ⇒ Object
- #push ⇒ Object
- #search(selector, kind = :matches) ⇒ Object
- #set(key, value) ⇒ Object
- #set_json_opts(options) ⇒ Object
Constructor Details
#initialize(db) ⇒ JsonStore
Returns a new instance of JsonStore.
7 8 9 10 11 |
# File 'lib/json_store.rb', line 7 def initialize(db) @db = db @map = {} @json_opts = Oj. end |
Instance Method Details
#all ⇒ Object
21 22 23 |
# File 'lib/json_store.rb', line 21 def all @map end |
#all_as_json ⇒ Object
37 38 39 |
# File 'lib/json_store.rb', line 37 def all_as_json Oj.dump(@map,@json_opts) end |
#clear ⇒ Object
33 34 35 |
# File 'lib/json_store.rb', line 33 def clear @map = {} end |
#db_path ⇒ Object
76 77 78 |
# File 'lib/json_store.rb', line 76 def db_path @db end |
#get(key) ⇒ Object
17 18 19 |
# File 'lib/json_store.rb', line 17 def get(key) @map[key] end |
#get_as_json(key) ⇒ Object
45 46 47 |
# File 'lib/json_store.rb', line 45 def get_as_json(key) Oj.dump(@map[key],@json_opts) end |
#get_json_opts ⇒ Object
29 30 31 |
# File 'lib/json_store.rb', line 29 def get_json_opts @json_opts end |
#merge(direction = :into_remote) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/json_store.rb', line 67 def merge(direction=:into_remote) begin @map = direction == :into_local ? @map.merge(read_data) : read_data.merge(@map) rescue LockMethod::Locked sleep 0.5 merge(direction) end end |
#pull ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/json_store.rb', line 49 def pull begin @map = read_data rescue LockMethod::Locked sleep 0.5 pull end end |
#push ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/json_store.rb', line 58 def push begin write_data rescue LockMethod::Locked sleep 0.5 push end end |
#search(selector, kind = :matches) ⇒ Object
41 42 43 |
# File 'lib/json_store.rb', line 41 def search(selector, kind=:matches) JSONSelect(selector).send(kind, @map) end |
#set(key, value) ⇒ Object
13 14 15 |
# File 'lib/json_store.rb', line 13 def set(key, value) @map[key] = value end |
#set_json_opts(options) ⇒ Object
25 26 27 |
# File 'lib/json_store.rb', line 25 def set_json_opts() @json_opts.merge!() end |