Class: Redisabel::KeyValue

Inherits:
Object
  • Object
show all
Extended by:
Finders, Transformations
Defined in:
lib/redisabel/key_value.rb

Direct Known Subclasses

KeyArray, KeyHash, KeyOrderedSet, KeySet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformations

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

#idObject

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_typeObject



10
11
12
# File 'lib/redisabel/key_value.rb', line 10

def self.data_type
  return String
end

.database_key_nameObject



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

Returns:

  • (Boolean)


92
93
94
# File 'lib/redisabel/key_value.rb', line 92

def autosave?
  return (!self.frozen? && @autosave)
end

#destroyObject



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

Returns:

  • (Boolean)


39
40
41
# File 'lib/redisabel/key_value.rb', line 39

def empty?
  return @data.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/redisabel/key_value.rb', line 52

def eql?(other)
  return self == other
end

#inspectObject



43
44
45
# File 'lib/redisabel/key_value.rb', line 43

def inspect
  return "#{self.class.name}:#{self.id} #{@data.inspect}"
end

#loadObject



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

#saveObject



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_sObject 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

#valueObject



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