Class: KeyValue
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- KeyValue
show all
- Defined in:
- lib/key_value.rb
Defined Under Namespace
Classes: HandlerSocketError
Constant Summary
collapse
- HS_DEFAULT_CONFIG =
{:port => '9998'}
- HS_INDEX =
31234
- VERSION =
File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.cache(key) ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/key_value.rb', line 61
def self.cache(key)
value = get(key)
if value.nil?
set(key, yield)
else
value
end
end
|
.del(key) ⇒ Object
53
54
55
|
# File 'lib/key_value.rb', line 53
def self.del(key)
set(key, nil)
end
|
.get(key) ⇒ Object
Also known as:
[]
24
25
26
27
28
29
30
31
32
|
# File 'lib/key_value.rb', line 24
def self.get(key)
if handler_socket
open_key_index
result = hs_find_by_key(key)
YAML.load(result) if result
else
KeyValue.find_by_key(key).try(:value)
end
end
|
.inc(key, offset = 1) ⇒ Object
57
58
59
|
# File 'lib/key_value.rb', line 57
def self.inc(key, offset=1)
set(key, (get(key) || 0) + offset)
end
|
.set(key, value) ⇒ Object
Also known as:
[]=
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/key_value.rb', line 34
def self.set(key, value)
if value.nil?
KeyValue.delete_all(:key => key)
else
unless record = KeyValue.find_by_key(key)
record = KeyValue.new
record.key = key end
record.value = value
record.save!
value
end
end
|
Instance Method Details
#value ⇒ Object
20
21
22
|
# File 'lib/key_value.rb', line 20
def value
YAML.load(read_attribute :value)
end
|
#value=(x) ⇒ Object
serialize would treat false the same as nil
15
16
17
18
|
# File 'lib/key_value.rb', line 15
def value=(x)
x = x.to_yaml unless x.nil?
write_attribute :value, x
end
|