Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/key.rb

Instance Method Summary collapse

Instance Method Details

#array_to_hash(array) ⇒ Object



173
174
175
176
177
# 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

#childrenObject



94
95
96
# File 'lib/key.rb', line 94

def children
  redis.keys(self+":*")
end

#dayObject



141
# File 'lib/key.rb', line 141

def day;    /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#day?Boolean

Returns:

  • (Boolean)


134
# File 'lib/key.rb', line 134

def day?;    resolution == :day end

#exists?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/key.rb', line 46

def exists?
  redis.exists self
end

#getObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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_arrayObject



169
170
171
# File 'lib/key.rb', line 169

def get_array
  redis.zrevrange self, 0, -1, :with_scores => true
end

#has_children?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/key.rb', line 63

def has_children?
  has_persistant_children?
end

#has_parent?Boolean

deprecated

Returns:

  • (Boolean)


68
69
70
# File 'lib/key.rb', line 68

def has_parent?
  parent.exists?
end

#has_persistant_children?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/key.rb', line 59

def has_persistant_children?
  not persistant_children.empty?
end

#has_persistant_parent?Boolean

deprecated

Returns:

  • (Boolean)


85
86
87
# File 'lib/key.rb', line 85

def has_persistant_parent?
  redis.ttl(parent_key) < 0 and parent.exists?
end

#has_volatile_parent?Boolean

deprecated

Returns:

  • (Boolean)


90
91
92
# File 'lib/key.rb', line 90

def has_volatile_parent?
  redis.ttl(parent_key) >= 0
end

#hourObject



142
# File 'lib/key.rb', line 142

def hour;   /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#hour?Boolean

Returns:

  • (Boolean)


135
# File 'lib/key.rb', line 135

def hour?;   resolution == :hour end

#minuteObject



143
# File 'lib/key.rb', line 143

def minute; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#minute?Boolean

Returns:

  • (Boolean)


136
# File 'lib/key.rb', line 136

def minute?; resolution == :minute end

#monthObject



140
# File 'lib/key.rb', line 140

def month;  /[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#month?Boolean

Returns:

  • (Boolean)


133
# File 'lib/key.rb', line 133

def month?;  resolution == :month end

#parentObject



55
56
57
# File 'lib/key.rb', line 55

def parent
  self.gsub(/:[^:]*$/, '')
end

#parent_keyObject

deprecated



51
52
53
# File 'lib/key.rb', line 51

def parent_key
  parent
end

#persistant?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/key.rb', line 76

def persistant?
  redis.ttl(self)==-1 and exists?  
end

#persistant_childrenObject



98
99
100
101
102
# File 'lib/key.rb', line 98

def persistant_children
  keys = redis.keys(self+":*")
  keys.delete_if{|k| k.volatile?}
  return keys
end

#prefixObject



41
42
43
44
# File 'lib/key.rb', line 41

def prefix
  matchdata = /(^[^:]+:[^:]+:[^:]+:)/.match(self)
  return matchdata[1] unless matchdata.nil?
end

#recent?Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
# 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

#redisObject



36
37
38
39
# File 'lib/key.rb', line 36

def redis
  @redis = $redis ||= Redis.new(:path => "/tmp/redis.sock",:driver => :hiredis)
  return @redis
end

#resolutionObject



121
122
123
124
125
126
127
128
129
130
# 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

#secondObject



144
# File 'lib/key.rb', line 144

def second; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#second?Boolean

Returns:

  • (Boolean)


137
# File 'lib/key.rb', line 137

def second?; resolution == :second end

#siblings_keysObject



104
105
106
# File 'lib/key.rb', line 104

def siblings_keys
  redis.keys(self.gsub(/:[^:]*$/, ':*'))
end

#timeObject



146
147
148
149
150
151
152
153
154
155
156
# 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

#ttlObject



80
81
82
# File 'lib/key.rb', line 80

def ttl
  redis.ttl self  
end

#unionize_persistant_childrenObject



109
110
111
112
113
114
115
116
117
118
119
# 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?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/key.rb', line 72

def volatile?
  redis.ttl(self) >= 0  
end

#yearObject



139
# File 'lib/key.rb', line 139

def year;   /[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end

#year?Boolean

Returns:

  • (Boolean)


132
# File 'lib/key.rb', line 132

def year?;   resolution == :year end