Class: Landrush::Store
- Inherits:
-
Object
- Object
- Landrush::Store
- Defined in:
- lib/landrush/store.rb
Instance Attribute Summary collapse
-
#backing_file ⇒ Object
Returns the value of attribute backing_file.
Class Method Summary collapse
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(key) ⇒ Object
- #each(*args, &block) ⇒ Object
- #find(search) ⇒ Object
- #get(key) ⇒ Object
- #has?(key, value = nil) ⇒ Boolean
-
#initialize(backing_file) ⇒ Store
constructor
A new instance of Store.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(backing_file) ⇒ Store
Returns a new instance of Store.
25 26 27 |
# File 'lib/landrush/store.rb', line 25 def initialize(backing_file) @backing_file = Pathname(backing_file) end |
Instance Attribute Details
#backing_file ⇒ Object
Returns the value of attribute backing_file.
23 24 25 |
# File 'lib/landrush/store.rb', line 23 def backing_file @backing_file end |
Class Method Details
.config ⇒ Object
14 15 16 |
# File 'lib/landrush/store.rb', line 14 def self.config @config ||= new(Server.working_dir.join('config.json')) end |
.hosts ⇒ Object
10 11 12 |
# File 'lib/landrush/store.rb', line 10 def self.hosts @hosts ||= new(Server.working_dir.join('hosts.json')) end |
.reset ⇒ Object
18 19 20 21 |
# File 'lib/landrush/store.rb', line 18 def self.reset @config = nil @hosts = nil end |
Instance Method Details
#clear! ⇒ Object
79 80 81 82 83 |
# File 'lib/landrush/store.rb', line 79 def clear! with_file_lock do |file| write({}, file) end end |
#delete(key) ⇒ Object
42 43 44 45 46 |
# File 'lib/landrush/store.rb', line 42 def delete(key) with_file_lock do |file| write(current_config(file).reject { |k, v| k == key || v == key }, file) end end |
#each(*args, &block) ⇒ Object
36 37 38 39 40 |
# File 'lib/landrush/store.rb', line 36 def each(*args, &block) with_file_lock do |file| current_config(file).each(*args, &block) end end |
#find(search) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/landrush/store.rb', line 58 def find(search) with_file_lock do |file| search = IPAddr.new(search).reverse if begin IPAddr.new(search) rescue StandardError nil end current_config(file).keys.detect do |key| key.casecmp(search) == 0 || search =~ /#{key}$/i || key =~ /^#{search}\./i end end end |
#get(key) ⇒ Object
73 74 75 76 77 |
# File 'lib/landrush/store.rb', line 73 def get(key) with_file_lock do |file| current_config(file)[key] end end |
#has?(key, value = nil) ⇒ Boolean
48 49 50 51 52 53 54 55 56 |
# File 'lib/landrush/store.rb', line 48 def has?(key, value = nil) with_file_lock do |file| if value.nil? current_config(file).key? key else current_config(file)[key] == value end end end |
#set(key, value) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/landrush/store.rb', line 29 def set(key, value) with_file_lock do |file| config = current_config(file).merge(key => value) write(config, file) end end |