Class: RecordCache::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/record_cache/set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (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

#dbhostObject (readonly)

Returns the value of attribute dbhost.



3
4
5
# File 'lib/record_cache/set.rb', line 3

def dbhost
  @dbhost
end

#fields_hashObject (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

#hostnameObject (readonly)

Returns the value of attribute hostname.



3
4
5
# File 'lib/record_cache/set.rb', line 3

def hostname
  @hostname
end

#model_classObject (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_atObject (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

.hostnameObject



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

Returns:

  • (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

Returns:

  • (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

#sizeObject



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