Class: RecordCache::Set
- Inherits:
-
Object
- Object
- RecordCache::Set
- Defined in:
- lib/record_cache/set.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#dbhost ⇒ Object
readonly
Returns the value of attribute dbhost.
-
#fields_hash ⇒ Object
readonly
Returns the value of attribute fields_hash.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(record) ⇒ Object
- #all_fields(type = model_class, opts = {}) ⇒ Object
- #delete(record) ⇒ Object
- #empty? ⇒ Boolean
- #fields(field, type) ⇒ Object
- #ids(type = model_class) ⇒ Object
-
#initialize(model_class, fields_hash = nil) ⇒ Set
constructor
A new instance of Set.
- #instantiate(type = model_class, full_record = false) ⇒ Object
- #instantiate_first(type = model_class, full_record = false) ⇒ Object
- #limit!(limit) ⇒ Object
- #records(type = model_class) ⇒ Object
- #records_by_type(type) ⇒ Object
- #size ⇒ Object
- #sort!(order_by) ⇒ Object
Constructor Details
#initialize(model_class, fields_hash = nil) ⇒ Set
Returns a new instance of Set.
17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def created_at @created_at end |
#dbhost ⇒ Object (readonly)
Returns the value of attribute dbhost.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def dbhost @dbhost end |
#fields_hash ⇒ Object (readonly)
Returns the value of attribute fields_hash.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def fields_hash @fields_hash end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def hostname @hostname end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def model_class @model_class end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
3 4 5 |
# File 'lib/record_cache/set.rb', line 3 def updated_at @updated_at end |
Class Method Details
.hostname ⇒ Object
13 14 15 |
# File 'lib/record_cache/set.rb', line 13 def self.hostname @hostname ||= Socket.gethostname end |
.source_tracking=(value) ⇒ Object
9 10 11 |
# File 'lib/record_cache/set.rb', line 9 def self.source_tracking=(value) @source_tracking = value end |
.source_tracking? ⇒ Boolean
5 6 7 |
# File 'lib/record_cache/set.rb', line 5 def self.source_tracking? @source_tracking end |
Instance Method Details
#<<(record) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# 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 = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# 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? ⇒ Boolean
86 87 88 |
# File 'lib/record_cache/set.rb', line 86 def empty? records.empty? end |
#fields(field, type) ⇒ Object
94 95 96 |
# 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) ⇒ Object
90 91 92 |
# 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) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# 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) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# 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) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
78 79 80 |
# File 'lib/record_cache/set.rb', line 78 def records(type = model_class) records_by_type(type) end |
#records_by_type(type) ⇒ Object
74 75 76 |
# File 'lib/record_cache/set.rb', line 74 def records_by_type(type) @records_by_type[type.to_s] ||= [] end |
#size ⇒ Object
82 83 84 |
# File 'lib/record_cache/set.rb', line 82 def size records.size end |
#sort!(order_by) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# 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 |