class Redisabel::KeyValue

Attributes

id[RW]

Public Class Methods

data_type() click to toggle source
# File lib/redisabel/key_value.rb, line 10
def self.data_type
  return String
end
database_key_name() click to toggle source
# File lib/redisabel/key_value.rb, line 14
def self.database_key_name
  return self.name.underscore
end
new(asave=false, id='', *data) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# 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) click to toggle source
# File lib/redisabel/key_value.rb, line 96
def autosave=(bool)
  @autosave = bool
  self.save if self.autosave?
end
autosave?() click to toggle source
# File lib/redisabel/key_value.rb, line 92
def autosave?
  return (!self.frozen? && @autosave)
end
destroy() click to toggle source
# File lib/redisabel/key_value.rb, line 71
def destroy
  key = self.database_key
  return Database.db.del(key) > 0
end
empty?() click to toggle source
# File lib/redisabel/key_value.rb, line 39
def empty?
  return @data.empty?
end
eql?(other) click to toggle source
# File lib/redisabel/key_value.rb, line 52
def eql?(other)
  return self == other
end
inspect() click to toggle source
# File lib/redisabel/key_value.rb, line 43
def inspect
  return "#{self.class.name}:#{self.id} #{@data.inspect}"
end
load() click to toggle source
# File lib/redisabel/key_value.rb, line 76
def load
  key = self.database_key
  data = self.class.transform(key)
  @data = data
end
save() { |key| ... } click to toggle source
# 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() click to toggle source
# File lib/redisabel/key_value.rb, line 47
def to_s
  return "#{self.class.name}:#{self.id}"
end
Also aliased as: to_str
to_str()
Alias for: to_s
value() click to toggle source
# File lib/redisabel/key_value.rb, line 101
def value
  return @data.dup
end
value=(val) click to toggle source
# 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

Protected Instance Methods

autosave() click to toggle source
# File lib/redisabel/key_value.rb, line 87
def autosave
  self.save if @autosave
end
database_key() click to toggle source
# File lib/redisabel/key_value.rb, line 82
def database_key
  return "#{self.class.database_key_name}:#{self.id}"
end
update_data(*data) click to toggle source
# File lib/redisabel/key_value.rb, line 25
def update_data(*data)
  if data.first.is_a?(self.class.data_type)
    data = data.shift
  elsif !data.is_a?(self.class.data_type)
    data = self.class.data_type.new
  end
  unless data.is_a?(self.class.data_type)
    raise ArgumentError.new("update_data expects a #{self.class.data_type}")
  end
  @data = data
  self.autosave
end