Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/key.rb
Instance Method Summary collapse
- #array_to_hash(array) ⇒ Object
- #children ⇒ Object
- #day ⇒ Object
- #day? ⇒ Boolean
- #exists? ⇒ Boolean
- #get ⇒ Object
- #get_array ⇒ Object
- #has_children? ⇒ Boolean
-
#has_parent? ⇒ Boolean
deprecated.
- #has_persistant_children? ⇒ Boolean
-
#has_persistant_parent? ⇒ Boolean
deprecated.
-
#has_volatile_parent? ⇒ Boolean
deprecated.
- #hour ⇒ Object
- #hour? ⇒ Boolean
- #minute ⇒ Object
- #minute? ⇒ Boolean
- #month ⇒ Object
- #month? ⇒ Boolean
- #parent ⇒ Object
-
#parent_key ⇒ Object
deprecated.
- #persistant? ⇒ Boolean
- #persistant_children ⇒ Object
- #prefix ⇒ Object
- #recent? ⇒ Boolean
- #redis ⇒ Object
- #resolution ⇒ Object
- #second ⇒ Object
- #second? ⇒ Boolean
- #siblings_keys ⇒ Object
- #time ⇒ Object
- #ttl ⇒ Object
- #unionize_persistant_children ⇒ Object
- #volatile? ⇒ Boolean
- #year ⇒ Object
- #year? ⇒ Boolean
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 |
#children ⇒ Object
94 95 96 |
# File 'lib/key.rb', line 94 def children redis.keys(self+":*") end |
#day ⇒ Object
141 |
# File 'lib/key.rb', line 141 def day; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#day? ⇒ Boolean
134 |
# File 'lib/key.rb', line 134 def day?; resolution == :day end |
#exists? ⇒ Boolean
46 47 48 |
# File 'lib/key.rb', line 46 def exists? redis.exists self end |
#get ⇒ Object
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_array ⇒ Object
169 170 171 |
# File 'lib/key.rb', line 169 def get_array redis.zrevrange self, 0, -1, :with_scores => true end |
#has_children? ⇒ Boolean
63 64 65 |
# File 'lib/key.rb', line 63 def has_children? has_persistant_children? end |
#has_parent? ⇒ Boolean
deprecated
68 69 70 |
# File 'lib/key.rb', line 68 def has_parent? parent.exists? end |
#has_persistant_children? ⇒ Boolean
59 60 61 |
# File 'lib/key.rb', line 59 def has_persistant_children? not persistant_children.empty? end |
#has_persistant_parent? ⇒ Boolean
deprecated
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
90 91 92 |
# File 'lib/key.rb', line 90 def has_volatile_parent? redis.ttl(parent_key) >= 0 end |
#hour ⇒ Object
142 |
# File 'lib/key.rb', line 142 def hour; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#hour? ⇒ Boolean
135 |
# File 'lib/key.rb', line 135 def hour?; resolution == :hour end |
#minute ⇒ Object
143 |
# File 'lib/key.rb', line 143 def minute; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#minute? ⇒ Boolean
136 |
# File 'lib/key.rb', line 136 def minute?; resolution == :minute end |
#month ⇒ Object
140 |
# File 'lib/key.rb', line 140 def month; /[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#month? ⇒ Boolean
133 |
# File 'lib/key.rb', line 133 def month?; resolution == :month end |
#parent ⇒ Object
55 56 57 |
# File 'lib/key.rb', line 55 def parent self.gsub(/:[^:]*$/, '') end |
#parent_key ⇒ Object
deprecated
51 52 53 |
# File 'lib/key.rb', line 51 def parent_key parent end |
#persistant? ⇒ Boolean
76 77 78 |
# File 'lib/key.rb', line 76 def persistant? redis.ttl(self)==-1 and exists? end |
#persistant_children ⇒ Object
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 |
#prefix ⇒ Object
41 42 43 44 |
# File 'lib/key.rb', line 41 def prefix matchdata = /(^[^:]+:[^:]+:[^:]+:)/.match(self) return matchdata[1] unless matchdata.nil? end |
#recent? ⇒ 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 |
#redis ⇒ Object
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 |
#resolution ⇒ Object
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 |
#second ⇒ Object
144 |
# File 'lib/key.rb', line 144 def second; /[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#second? ⇒ Boolean
137 |
# File 'lib/key.rb', line 137 def second?; resolution == :second end |
#siblings_keys ⇒ Object
104 105 106 |
# File 'lib/key.rb', line 104 def siblings_keys redis.keys(self.gsub(/:[^:]*$/, ':*')) end |
#time ⇒ Object
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 |
#ttl ⇒ Object
80 81 82 |
# File 'lib/key.rb', line 80 def ttl redis.ttl self end |
#unionize_persistant_children ⇒ Object
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
72 73 74 |
# File 'lib/key.rb', line 72 def volatile? redis.ttl(self) >= 0 end |
#year ⇒ Object
139 |
# File 'lib/key.rb', line 139 def year; /[^:]+:[^:]+:[^:]+:([^:]+)/.match(self)[1].to_i end |
#year? ⇒ Boolean
132 |
# File 'lib/key.rb', line 132 def year?; resolution == :year end |