Module: Redrecord::Model

Defined in:
lib/redrecord.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/redrecord.rb', line 62

def self.included(mod)
  mod.extend(ClassMethods)
  mod.send(:after_save,     :redrecord_update_queue_save)
  mod.send(:after_destroy,  :redrecord_update_queue_destroy)
  mod.send(:after_commit,   :redrecord_update_queue_commit)
  mod.send(:after_rollback, :redrecord_update_queue_rollback)
end

Instance Method Details

#add_to_cache!Object



114
115
116
117
118
119
120
121
122
# File 'lib/redrecord.rb', line 114

def add_to_cache!
  Redrecord.redis_op(:hmset, redrecord_key,
    *(self.class.redrecord_cached_fields.map {|f|
      aliased_target, punctuation = f.to_s.sub(/([?!=])$/, ''), $1
      val = send("#{aliased_target}_without_cache#{punctuation}")
      [f.to_s, String===val && !Redrecord.is_marshalled?(val) ? val : Marshal.dump(val)]
    }.flatten)
  )
end

#attribs_with_cached_fieldsObject



143
144
145
# File 'lib/redrecord.rb', line 143

def attribs_with_cached_fields
  attributes.merge(cached_fields)
end

#cached_fieldsObject



147
148
149
# File 'lib/redrecord.rb', line 147

def cached_fields
  self.class.redrecord_cached_fields.inject({}) {|hsh,field| hsh[field] = send(field) ; hsh }
end

#cached_method(method_name) ⇒ Object



124
125
126
# File 'lib/redrecord.rb', line 124

def cached_method(method_name)
  redrecord_cached_attrib_hash[method_name.to_sym]
end

#invalidations_for_redrecord_update_queueObject



75
76
77
78
79
80
81
82
83
# File 'lib/redrecord.rb', line 75

def invalidations_for_redrecord_update_queue
  self.class.redrecord_invalidation_fields.each do |f|
    if((field_value = send(f)).kind_of?(Array))
      field_value.each {|item|  Redrecord.update_queue << [:save, item] } 
    else
      Redrecord.update_queue << [:save, field_value] if field_value
    end
  end
end

#redrecord_cached_attrib_hashObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/redrecord.rb', line 132

def redrecord_cached_attrib_hash
  @redrecord_cached_attrib_hash ||= Hash.new do |h,k|
    h[k.to_sym] = if(cached = (redrecord_redis_cache && redrecord_redis_cache[k.to_s] unless new_record?))
      Redrecord.is_marshalled?(cached) ? Marshal.load(cached) : cached
    else
      aliased_target, punctuation = k.to_s.sub(/([?!=])$/, ''), $1
      send("#{aliased_target}_without_cache#{punctuation}")
    end
  end
end

#redrecord_keyObject



106
107
108
# File 'lib/redrecord.rb', line 106

def redrecord_key
  "#{self.class.table_name}:#{self.id}"
end

#redrecord_redis_cacheObject



128
129
130
# File 'lib/redrecord.rb', line 128

def redrecord_redis_cache
  @redrecord_redis_cache ||= Redrecord.redis_op(:hgetall, redrecord_key) unless Redrecord.write_only
end

#redrecord_update_queue_commitObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/redrecord.rb', line 94

def redrecord_update_queue_commit
  Redrecord.update_queue.each do |command, record|
    if command == :destroy
      record.remove_from_cache!
    elsif command == :save
      record.add_to_cache!
      # possible todo: cascade invalidation (but avoid loops)
    end
  end
  Redrecord.update_queue.clear
end

#redrecord_update_queue_destroyObject



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

def redrecord_update_queue_destroy
  Redrecord.update_queue << [:destroy, self] unless self.class.redrecord_cached_fields.empty?
  invalidations_for_redrecord_update_queue
end

#redrecord_update_queue_rollbackObject



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

def redrecord_update_queue_rollback
  Redrecord.update_queue.clear
end

#redrecord_update_queue_saveObject



70
71
72
73
# File 'lib/redrecord.rb', line 70

def redrecord_update_queue_save
  Redrecord.update_queue << [:save, self] unless self.class.redrecord_cached_fields.empty?
  invalidations_for_redrecord_update_queue
end

#remove_from_cache!Object



110
111
112
# File 'lib/redrecord.rb', line 110

def remove_from_cache!
  Redrecord.redis_op :del, redrecord_key
end