class RecordCache::Set
Attributes
created_at[R]
dbhost[R]
fields_hash[R]
hostname[R]
model_class[R]
updated_at[R]
Public Class Methods
hostname()
click to toggle source
# File lib/record_cache/set.rb, line 13 def self.hostname @hostname ||= Socket.gethostname end
new(model_class, fields_hash = nil)
click to toggle source
# File lib/record_cache/set.rb, line 17 def initialize(model_class, fields_hash = nil) raise 'valid model class required' unless model_class @model_class = model_class @fields_hash = fields_hash @records_by_type = {} if self.class.source_tracking? @created_at = Time.now @hostname = self.class.hostname @dbhost = RecordCache.db(model_class).instance_variable_get(:@config)[:host] end end
source_tracking=(value)
click to toggle source
# File lib/record_cache/set.rb, line 9 def self.source_tracking=(value) @source_tracking = value end
source_tracking?()
click to toggle source
# File lib/record_cache/set.rb, line 5 def self.source_tracking? @source_tracking end
Public Instance Methods
<<(record)
click to toggle source
# File lib/record_cache/set.rb, line 53 def <<(record) mark_updated! record_type = record['type'] record['id'] = record['id'].to_i if record.has_key?('id') [record_type, model_class.to_s].uniq.each do |type| records_by_type(type) << record end end
all_fields(type = model_class, opts = {})
click to toggle source
# File lib/record_cache/set.rb, line 98 def all_fields(type = model_class, opts = {}) records(type).collect do |r| record = {} r.each do |field, value| next if field == opts[:except] record[field.to_sym] = type_cast(field, value) end record end end
delete(record)
click to toggle source
# File lib/record_cache/set.rb, line 63 def delete(record) raise 'cannot delete record without id' unless record.has_key?('id') mark_updated! record_type = record['type'] id = record['id'].to_i [record_type, model_class.to_s].uniq.each do |type| records_by_type(type).reject! {|r| r['id'] == id} end end
empty?()
click to toggle source
# File lib/record_cache/set.rb, line 86 def empty? records.empty? end
fields(field, type)
click to toggle source
# File lib/record_cache/set.rb, line 94 def fields(field, type) records(type).collect {|r| type_cast(field, r[field])} end
ids(type = model_class)
click to toggle source
# File lib/record_cache/set.rb, line 90 def ids(type = model_class) records(type).collect {|r| r['id']} end
instantiate(type = model_class, full_record = false)
click to toggle source
# File lib/record_cache/set.rb, line 119 def instantiate(type = model_class, full_record = false) if full_record records(type).collect do |record| type.send(:instantiate, record) end else type.find_all_by_id(ids(type)) end end
instantiate_first(type = model_class, full_record = false)
click to toggle source
# File lib/record_cache/set.rb, line 109 def instantiate_first(type = model_class, full_record = false) if full_record record = records(type).first type.send(:instantiate, record) if record else id = ids(type).first type.find_by_id(id) if id end end
limit!(limit)
click to toggle source
# File lib/record_cache/set.rb, line 42 def limit!(limit) all_records = records if all_records.length > limit removed_records = all_records.slice!(limit..-1) removed_records.each do |record| type = record['type'] records_by_type(type).delete(record) if type end end end
records(type = model_class)
click to toggle source
# File lib/record_cache/set.rb, line 78 def records(type = model_class) records_by_type(type) end
records_by_type(type)
click to toggle source
# File lib/record_cache/set.rb, line 74 def records_by_type(type) @records_by_type[type.to_s] ||= [] end
size()
click to toggle source
# File lib/record_cache/set.rb, line 82 def size records.size end
sort!(order_by)
click to toggle source
# File lib/record_cache/set.rb, line 30 def sort!(order_by) field, order = order_by.strip.squeeze.split descending = (order == 'DESC') @records_by_type.values.each do |records| sorted_records = records.sort_by do |record| type_cast(field, record[field]) end sorted_records.reverse! if descending records.replace(sorted_records) end end
Private Instance Methods
mark_updated!()
click to toggle source
# File lib/record_cache/set.rb, line 131 def mark_updated! @updated_at = Time.now if self.class.source_tracking? end
type_cast(field, value)
click to toggle source
# File lib/record_cache/set.rb, line 135 def type_cast(field, value) column = model_class.columns_hash[field.to_s] raise 'column not found in #{model_class} for field #{field}' unless column column.type_cast(value) end