class String

Public Instance Methods

array_to_hash(array) click to toggle source
# File lib/key.rb, line 173
def array_to_hash array
  hash = Hash.new
  array.each{|a| hash[a[0]] = a[1].to_f }
  return hash
end
children() click to toggle source
# File lib/key.rb, line 94
def children
  redis.keys(self+":*")
end
day() click to toggle source
# File lib/key.rb, line 141
def day;    /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
day?() click to toggle source
# File lib/key.rb, line 134
def day?;    resolution == :day end
exists?() click to toggle source
# File lib/key.rb, line 46
def exists?
  redis.exists self
end
get() click to toggle source
# File lib/key.rb, line 179
def get
  if exists?
    ;
  elsif has_children?
    unionize_persistant_children
  else
    if not year?
      parent.get
      # http://rubydoc.info/github/redis/redis-rb/Redis:zunionstore
      p = parent #TODO Test the case when the immediate parent has no persistant children so weights has infinite values
      while !p.has_children?
        p = p.parent
      end
      weights = [ 1.0 / p.persistant_children.size ]      
      redis.zunionstore self, [parent], :weights => weights
      if recent?
        redis.expire self, 60
      else
        redis.expire self, 600      
      end
    else
      return {}
    end
  end
  array_to_hash get_array
end
get_array() click to toggle source
# File lib/key.rb, line 169
def get_array
  redis.zrevrange self, 0, -1, :with_scores => true
end
has_children?() click to toggle source
# File lib/key.rb, line 63
def has_children?
  has_persistant_children?
end
has_parent?() click to toggle source

deprecated

# File lib/key.rb, line 68
def has_parent?
  parent.exists?
end
has_persistant_children?() click to toggle source
# File lib/key.rb, line 59
def has_persistant_children?
  not persistant_children.empty?
end
has_persistant_parent?() click to toggle source

deprecated

# File lib/key.rb, line 85
def has_persistant_parent?
  redis.ttl(parent_key) < 0 and parent.exists?
end
has_volatile_parent?() click to toggle source

deprecated

# File lib/key.rb, line 90
def has_volatile_parent?
  redis.ttl(parent_key) >= 0
end
hour() click to toggle source
# File lib/key.rb, line 142
def hour;   /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
hour?() click to toggle source
# File lib/key.rb, line 135
def hour?;   resolution == :hour end
minute() click to toggle source
# File lib/key.rb, line 143
def minute; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
minute?() click to toggle source
# File lib/key.rb, line 136
def minute?; resolution == :minute end
month() click to toggle source
# File lib/key.rb, line 140
def month;  /[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
month?() click to toggle source
# File lib/key.rb, line 133
def month?;  resolution == :month end
parent() click to toggle source
# File lib/key.rb, line 55
def parent
  self.gsub(/:[^:]*$/, '')
end
parent_key() click to toggle source

deprecated

# File lib/key.rb, line 51
def parent_key
  parent
end
persistant?() click to toggle source
# File lib/key.rb, line 76
  def persistant?
  redis.ttl(self)==-1 and exists?  
end
persistant_children() click to toggle source
# File lib/key.rb, line 98
def persistant_children
  keys = redis.keys(self+":*")
  keys.delete_if{|k| k.volatile?}
  return keys
end
prefix() click to toggle source
# File lib/key.rb, line 41
def prefix
  matchdata = /(^[^:]+:[^:]+:[^:]+:)/.match(self)
  return matchdata[1] unless matchdata.nil?
end
recent?() click to toggle source
# File lib/key.rb, line 158
def recent?
  case resolution
    when :year   then year == Time.now.year
    when :month  then year == Time.now.year
    when :day    then year == Time.now.year and month == Time.now.month
    when :hour   then year == Time.now.year and month == Time.now.month and day == Time.now.day
    when :minute then year == Time.now.year and month == Time.now.month and day == Time.now.day and hour == Time.now.hour
    when :second then year == Time.now.year and month == Time.now.month and day == Time.now.day and hour == Time.now.hour and minute == Time.now.min
  end  
end
redis() click to toggle source
# File lib/key.rb, line 36
def redis
  @redis = $redis ||= Redis.new(:path => "/tmp/redis.sock",:driver => :hiredis)
  return @redis
end
resolution() click to toggle source
# File lib/key.rb, line 121
def resolution
  case self.count(':')
    when 8 then :second
    when 7 then :minute
    when 6 then :hour
    when 5 then :day
    when 4 then :month
    when 3 then :year
  end   
end
second() click to toggle source
# File lib/key.rb, line 144
def second; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
second?() click to toggle source
# File lib/key.rb, line 137
def second?; resolution == :second end
siblings_keys() click to toggle source
# File lib/key.rb, line 104
def siblings_keys
  redis.keys(self.gsub(/:[^:]*$/, ':*'))
end
time() click to toggle source
# File lib/key.rb, line 146
def time
  key = self.gsub(/^[^:]*:[^:]*:[^:]*:/, '') #remove prefix
  case resolution
    when :year   then return DateTime.new(year).to_time
    when :month  then return DateTime.new(year, month).to_time
    when :day    then return DateTime.new(year, month, day).to_time
    when :hour   then return DateTime.new(year, month, day, hour, 0,      0,      '+3').to_time
    when :minute then return DateTime.new(year, month, day, hour, minute, 0,      '+3').to_time
    when :second then return DateTime.new(year, month, day, hour, minute, second, '+3').to_time
  end  
end
ttl() click to toggle source
# File lib/key.rb, line 80
def ttl
  redis.ttl self  
end
unionize_persistant_children() click to toggle source
# File lib/key.rb, line 109
def unionize_persistant_children
  children = persistant_children
  redis.zunionstore self, children
  
  if recent?
    redis.expire self, 60
  else
    redis.expire self, 600      
  end
  
end
volatile?() click to toggle source
# File lib/key.rb, line 72
def volatile?
  redis.ttl(self) >= 0  
end
year() click to toggle source
# File lib/key.rb, line 139
def year;   /[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end
year?() click to toggle source
# File lib/key.rb, line 132
def year?;   resolution == :year end