Class: JsonStore

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

Instance Method Summary collapse

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.default_options
end

Instance Method Details

#allObject



21
22
23
# File 'lib/json_store.rb', line 21

def all
  @map
end

#all_as_jsonObject



37
38
39
# File 'lib/json_store.rb', line 37

def all_as_json
  Oj.dump(@map,@json_opts)
end

#clearObject



33
34
35
# File 'lib/json_store.rb', line 33

def clear
  @map = {}
end

#db_pathObject



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_optsObject



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

#pullObject



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

#pushObject



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(options)
 @json_opts.merge!(options)
end