Class: ActiveRedis::Base
- Inherits:
-
Object
- Object
- ActiveRedis::Base
- Includes:
- ActiveModel::Dirty, ActiveModel::Naming, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/activeredis.rb
Constant Summary collapse
- QUEUED =
"QUEUED"
Class Attribute Summary collapse
-
._fields ⇒ Object
Returns the value of attribute _fields.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
RAILSISM Returns a hash of all the attributes with their names as keys and the values of the attributes as values –> means: Strings as keys! –> initialize stringifies.
-
#frozen ⇒ Object
readonly
Returns the value of attribute frozen.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .all ⇒ Object
- .connection ⇒ Object
- .count ⇒ Object
-
.create(attributes) ⇒ Object
CLASS METHODS.
- .define_field(field) ⇒ Object
- .delete_all ⇒ Object
- .delete_unused_field ⇒ Object
- .fast_find_by_param(field, value) ⇒ Object
- .fetch_new_identifier ⇒ Object
-
.fields(*fields) ⇒ Object
Run this method to declare the fields of your model.
- .find(id) ⇒ Object
- .find_all ⇒ Object
- .find_all_by_param(field, value) ⇒ Object
- .find_by_param(field, value) ⇒ Object
- .get_fields ⇒ Object
- .identifier_sequencer ⇒ Object
- .inherited(child) ⇒ Object
- .key_namespace ⇒ Object
- .method_missing(name, *args) ⇒ Object
- .redis_information ⇒ Object
Instance Method Summary collapse
- #[](field) ⇒ Object
- #[]=(field, value) ⇒ Object
- #add_attribute(name, value = nil) ⇒ Object
- #class_namespace ⇒ Object
- #connection ⇒ Object
- #destroy ⇒ Object
- #frozen? ⇒ Boolean
-
#initialize(attributes = {}, id = nil) ⇒ Base
constructor
INSTANCE METHODS.
-
#initialize_attributes(attributes) ⇒ Object
Object’s attributes’ keys are converted to strings because of railsisms.
- #key_namespace ⇒ Object
- #new_record? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #update_attributes(attributes) ⇒ Object
Constructor Details
#initialize(attributes = {}, id = nil) ⇒ Base
INSTANCE METHODS
68 69 70 71 72 73 |
# File 'lib/activeredis.rb', line 68 def initialize(attributes = {}, id = nil) @id = id if id @attributes = {} initialize_attributes(attributes) frozen = false end |
Class Attribute Details
._fields ⇒ Object
Returns the value of attribute _fields.
64 65 66 |
# File 'lib/activeredis.rb', line 64 def _fields @_fields end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
RAILSISM Returns a hash of all the attributes with their names as keys and the values of the attributes as values
--> means: Strings as keys!
--> initialize stringifies
called by to_json for_example
60 61 62 |
# File 'lib/activeredis.rb', line 60 def attributes @attributes end |
#frozen ⇒ Object (readonly)
Returns the value of attribute frozen.
62 63 64 |
# File 'lib/activeredis.rb', line 62 def frozen @frozen end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
61 62 63 |
# File 'lib/activeredis.rb', line 61 def id @id end |
Class Method Details
.all ⇒ Object
247 248 249 |
# File 'lib/activeredis.rb', line 247 def self.all find_all end |
.connection ⇒ Object
227 228 229 |
# File 'lib/activeredis.rb', line 227 def self.connection @@redis end |
.count ⇒ Object
231 232 233 |
# File 'lib/activeredis.rb', line 231 def self.count size = connection.zcard "#{key_namespace}:all" end |
.create(attributes) ⇒ Object
CLASS METHODS
177 178 179 |
# File 'lib/activeredis.rb', line 177 def self.create(attributes) self.new(attributes).save end |
.define_field(field) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/activeredis.rb', line 181 def self.define_field(field) define_method field.to_sym do if field.to_sym == :updated_at Time.parse(@attributes["#{field}"]) else @attributes["#{field}"] end end define_method "#{field}=".to_sym do |new_value| @attributes["#{field}"] = new_value.to_s end end |
.delete_all ⇒ Object
251 252 253 254 255 256 257 258 259 |
# File 'lib/activeredis.rb', line 251 def self.delete_all records = find_all records.each do |record| record.destroy end if ActiveRedis.fast_find_field != nil connection.del "#{key_namespace}:fast_find_field", "#{key_namespace}:no" end end |
.delete_unused_field ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/activeredis.rb', line 311 def self.delete_unused_field ids = connection.zrange "#{key_namespace}:all", 0, count if ids.size > 0 attributes = connection.hgetall "#{key_namespace}:#{ids[0]}:attributes" now_keys = self.get_fields array = [] now_keys.each do |key| array << key.to_s end attributes.reject! {|key| array.include? key } attributes.keys.each do |delete_key| ids.each do |id| connection.hdel "#{key_namespace}:#{id}:attributes", delete_key end end end end |
.fast_find_by_param(field, value) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/activeredis.rb', line 295 def self.fast_find_by_param(field, value) find_id = nil if ActiveRedis.fast_find_field == field find_id = connection.hget "#{key_namespace}:fast_find_field", value elsif field == :no find_id = connection.hget "#{key_namespace}:no", value end find_id end |
.fetch_new_identifier ⇒ Object
209 210 211 |
# File 'lib/activeredis.rb', line 209 def self.fetch_new_identifier self.connection.incr self.identifier_sequencer end |
.fields(*fields) ⇒ Object
Run this method to declare the fields of your model.
196 197 198 199 |
# File 'lib/activeredis.rb', line 196 def self.fields(*fields) self._fields ||= [] self._fields = fields end |
.find(id) ⇒ Object
261 262 263 264 265 266 267 268 269 |
# File 'lib/activeredis.rb', line 261 def self.find(id) return find_all if id == :all exists = connection.zscore "#{key_namespace}:all", id return nil unless exists #raise RecordNotFound.new("Couldn't find #{self.name} with ID=#{id}") unless exists attributes = connection.hgetall "#{key_namespace}:#{id}:attributes" obj = self.new attributes, id return obj end |
.find_all ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/activeredis.rb', line 235 def self.find_all() records = [] # TODO Interim fix, "QUEUED" is find(id) rescue ids = connection.zrange "#{key_namespace}:all", 0, count ids.each do |id| record = find(id) records << record if record #records << find(id) end records end |
.find_all_by_param(field, value) ⇒ Object
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/activeredis.rb', line 271 def self.find_all_by_param(field, value) finded = [] records = find_all records.each do |record| if record && record.attributes[field.to_s] == value.to_s finded << record end end return finded end |
.find_by_param(field, value) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/activeredis.rb', line 282 def self.find_by_param(field, value) find_id = self.fast_find_by_param(field, value) return find(find_id) if find_id != nil ids = connection.zrange "#{key_namespace}:all", 0, count ids.each do |id| record = find(id) if record && record.attributes[field.to_s] == value.to_s return record end end nil end |
.get_fields ⇒ Object
201 202 203 |
# File 'lib/activeredis.rb', line 201 def self.get_fields self._fields end |
.identifier_sequencer ⇒ Object
213 214 215 |
# File 'lib/activeredis.rb', line 213 def self.identifier_sequencer "#{key_namespace}:sequence" end |
.inherited(child) ⇒ Object
217 218 219 220 221 |
# File 'lib/activeredis.rb', line 217 def self.inherited(child) #puts "Redis.new(:host => #{ActiveRedis.host}, :port => #{ActiveRedis.port})" @@redis = Redis.new(:host => ActiveRedis.host, :port => ActiveRedis.port) @@class = child end |
.key_namespace ⇒ Object
205 206 207 |
# File 'lib/activeredis.rb', line 205 def self.key_namespace "#{self}" end |
.method_missing(name, *args) ⇒ Object
305 306 307 308 309 |
# File 'lib/activeredis.rb', line 305 def self.method_missing(name, *args) return find_by_param($1.to_sym, args[0]) if name.to_s =~ /^find_by_(.*)/ return find_all_by_param($1.to_sym, args[0]) if name.to_s =~ /^find_all_by_(.*)/ super end |
.redis_information ⇒ Object
223 224 225 |
# File 'lib/activeredis.rb', line 223 def self.redis_information connection.info # call_command [:info] end |
Instance Method Details
#[](field) ⇒ Object
165 166 167 |
# File 'lib/activeredis.rb', line 165 def [](field) send(field) end |
#[]=(field, value) ⇒ Object
169 170 171 |
# File 'lib/activeredis.rb', line 169 def []=(field, value) send(field+"=", value) end |
#add_attribute(name, value = nil) ⇒ Object
161 162 163 |
# File 'lib/activeredis.rb', line 161 def add_attribute(name, value=nil) initialize_attributes({name => value}) end |
#class_namespace ⇒ Object
134 135 136 |
# File 'lib/activeredis.rb', line 134 def class_namespace "#{self.class.key_namespace}" end |
#connection ⇒ Object
138 139 140 |
# File 'lib/activeredis.rb', line 138 def connection self.class.connection end |
#destroy ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/activeredis.rb', line 142 def destroy connection.multi do if ActiveRedis.fast_find_field != nil connection.hdel "#{class_namespace}:fast_find_field", @attributes[ActiveRedis.fast_find_field.to_s].to_s if self.class._fields.include?(:no) connection.hdel "#{class_namespace}:no", @attributes[:no.to_s].to_s end end connection.del "#{key_namespace}:attributes" connection.zrem "#{class_namespace}:all", @id @frozen = true end return true end |
#frozen? ⇒ Boolean
157 158 159 |
# File 'lib/activeredis.rb', line 157 def frozen? @frozen end |
#initialize_attributes(attributes) ⇒ Object
Object’s attributes’ keys are converted to strings because of railsisms. Because of activeredisism, also values are converted to strings for consistency.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/activeredis.rb', line 77 def initialize_attributes(attributes) fields = Hash[[self.class._fields, Array.new(self.class._fields.size)].transpose] fields.stringify_keys! # NEEDS to be strings for railsisms attributes.stringify_keys! # NEEDS to be strings for railsisms attributes.each_pair { |key, value| attributes[key] = value.to_s } fields.merge!(attributes) @attributes.merge!(fields) @attributes.each_pair do |key, value| self.class.define_field key end end |
#key_namespace ⇒ Object
130 131 132 |
# File 'lib/activeredis.rb', line 130 def key_namespace "#{self.class.key_namespace}:#{self.id}" end |
#new_record? ⇒ Boolean
126 127 128 |
# File 'lib/activeredis.rb', line 126 def new_record? @id == nil end |
#reload ⇒ Object
122 123 124 |
# File 'lib/activeredis.rb', line 122 def reload @attributes = connection.hgetall "#{key_namespace}:attributes" end |
#save ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/activeredis.rb', line 89 def save creation = new_record? @id = self.class.fetch_new_identifier if creation connection.multi do if @attributes.size > 0 @attributes.each_pair { |key, value| if key.to_sym == :updated_at value = Time.now.to_s end connection.hset("#{key_namespace}:attributes", key, value) } end if ActiveRedis.fast_find_field != nil connection.hset("#{class_namespace}:fast_find_field", @attributes[ActiveRedis.fast_find_field.to_s].to_s, @id) if self.class._fields.include?(:no) connection.hset("#{class_namespace}:no", @attributes[:no.to_s].to_s, @id) end end connection.zadd("#{class_namespace}:all", @id, @id) end true end |
#update_attributes(attributes) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/activeredis.rb', line 112 def update_attributes(attributes) attributes.stringify_keys! # NEEDS to be strings for railsisms attributes.each_pair { |key, value| attributes[key] = value.to_s } @attributes.merge!(attributes) attributes.each_pair do |key, value| self.class.define_field key end save end |