Class: Redisabel::KeyValue
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
detect_type, transform, transform_hash, transform_list, transform_set, transform_string, transform_zset
Methods included from Finders
filter, find
Constructor Details
#initialize(asave = false, id = '', *data) ⇒ KeyValue
Returns a new instance of KeyValue.
18
19
20
21
22
23
|
# File 'lib/redisabel/key_value.rb', line 18
def initialize(asave=false, id='', *data)
@autosave = asave
@data = self.class.data_type.new
self.id = id || ''
self.update_data(*data)
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
8
9
10
|
# File 'lib/redisabel/key_value.rb', line 8
def id
@id
end
|
Class Method Details
.data_type ⇒ Object
10
11
12
|
# File 'lib/redisabel/key_value.rb', line 10
def self.data_type
return String
end
|
.database_key_name ⇒ Object
14
15
16
|
# File 'lib/redisabel/key_value.rb', line 14
def self.database_key_name
return self.name.underscore
end
|
Instance Method Details
#==(other) ⇒ Object
56
57
58
59
|
# File 'lib/redisabel/key_value.rb', line 56
def ==(other)
return (other.is_a?(KeyValue) && self.id == other.id &&
@data == other.value)
end
|
#autosave=(bool) ⇒ Object
96
97
98
99
|
# File 'lib/redisabel/key_value.rb', line 96
def autosave=(bool)
@autosave = bool
self.save if self.autosave?
end
|
#autosave? ⇒ Boolean
92
93
94
|
# File 'lib/redisabel/key_value.rb', line 92
def autosave?
return (!self.frozen? && @autosave)
end
|
#destroy ⇒ Object
71
72
73
74
|
# File 'lib/redisabel/key_value.rb', line 71
def destroy
key = self.database_key
return Database.db.del(key) > 0
end
|
#empty? ⇒ Boolean
39
40
41
|
# File 'lib/redisabel/key_value.rb', line 39
def empty?
return @data.empty?
end
|
#eql?(other) ⇒ Boolean
52
53
54
|
# File 'lib/redisabel/key_value.rb', line 52
def eql?(other)
return self == other
end
|
#inspect ⇒ Object
43
44
45
|
# File 'lib/redisabel/key_value.rb', line 43
def inspect
return "#{self.class.name}:#{self.id} #{@data.inspect}"
end
|
#load ⇒ Object
76
77
78
79
80
|
# File 'lib/redisabel/key_value.rb', line 76
def load
key = self.database_key
data = self.class.transform(key)
@data = data
end
|
#save ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/redisabel/key_value.rb', line 61
def save
return false if self.id.to_s.empty? || self.frozen?
key = self.database_key
if block_given?
return yield(key)
else
return Database.db.set(key, @data) == Database::ok
end
end
|
#to_s ⇒ Object
Also known as:
to_str
47
48
49
|
# File 'lib/redisabel/key_value.rb', line 47
def to_s
return "#{self.class.name}:#{self.id}"
end
|
#value ⇒ Object
101
102
103
|
# File 'lib/redisabel/key_value.rb', line 101
def value
return @data.dup
end
|
#value=(val) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/redisabel/key_value.rb', line 105
def value=(val)
unless val.is_a?(self.class.data_type)
val = self.class.data_type.new
end
@data = val
end
|