Module: RecordCache::Strategy::Util
- Defined in:
- lib/record_cache/strategy/util.rb
Defined Under Namespace
Modules: Collator
Constant Summary collapse
- CLASS_KEY =
:c
- ATTRIBUTES_KEY =
:a
Class Method Summary collapse
-
.deserialize(serialized) ⇒ Object
deserialize a cached record.
-
.filter!(records, wheres) ⇒ Object
Filter the cached records in memory only simple x = y or x IN (a,b,c) can be handled string comparison is case insensitive Example: RecordCache::Strategy::Util.filter!(Apple.all, :price => [0.49, 0.59, 0.69], :name => “Green Apple”).
-
.serialize(record) ⇒ Object
serialize one record before adding it to the cache creates a shallow clone with a version and without associations.
-
.sort!(records, *sort_orders) ⇒ Object
Sort the cached records in memory, similar to MySql sorting rules including collatiom Simply provide the Symbols of the attributes to sort in Ascending order, or use [<attribute>, false] for Descending order.
Class Method Details
.deserialize(serialized) ⇒ Object
deserialize a cached record
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/record_cache/strategy/util.rb', line 18 def deserialize(serialized) record = serialized[CLASS_KEY].constantize.allocate attributes = serialized[ATTRIBUTES_KEY] record.class.serialized_attributes.keys.each do |attribute| if attributes[attribute].respond_to?(:unserialize) if attributes[attribute].method(:unserialize).arity > 0 attributes[attribute] = attributes[attribute].unserialize(attributes[attribute].value) else attributes[attribute] = attributes[attribute].unserialize end end end record.init_with('attributes' => attributes) record end |
.filter!(records, wheres) ⇒ Object
Filter the cached records in memory only simple x = y or x IN (a,b,c) can be handled string comparison is case insensitive Example:
RecordCache::Strategy::Util.filter!(Apple.all, :price => [0.49, 0.59, 0.69], :name => "Green Apple")
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/record_cache/strategy/util.rb', line 39 def filter!(records, wheres) wheres.each_pair do |attr, value| attr = attr.to_sym if value.is_a?(Array) where_values = Set.new(value.first.respond_to?(:downcase) ? value.map(&:downcase) : value) records.to_a.select! do |record| attribute_value = record.send(attr) attribute_value = attribute_value.downcase if attribute_value.respond_to?(:downcase) where_values.include?(attribute_value) end else where_value = value.respond_to?(:downcase) ? value.downcase : value records.to_a.select! do |record| attribute_value = record.send(attr) attribute_value = attribute_value.downcase if attribute_value.respond_to?(:downcase) attribute_value == where_value end end end end |
.serialize(record) ⇒ Object
serialize one record before adding it to the cache creates a shallow clone with a version and without associations
12 13 14 15 |
# File 'lib/record_cache/strategy/util.rb', line 12 def serialize(record) {CLASS_KEY => record.class.name, ATTRIBUTES_KEY => record.instance_variable_get(:@attributes).dup} end |
.sort!(records, *sort_orders) ⇒ Object
Sort the cached records in memory, similar to MySql sorting rules including collatiom Simply provide the Symbols of the attributes to sort in Ascending order, or use
- <attribute>, false
-
for Descending order.
Example:
RecordCache::Strategy::Util.sort!(Apple.all, :name)
RecordCache::Strategy::Util.sort!(Apple.all, [:name, false])
RecordCache::Strategy::Util.sort!(Apple.all, [:price, false], :name)
RecordCache::Strategy::Util.sort!(Apple.all, [:price, false], [:name, true])
RecordCache::Strategy::Util.sort!(Apple.all, [[:price, false], [:name, true]])
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/record_cache/strategy/util.rb', line 69 def sort!(records, *sort_orders) return records if records.empty? || sort_orders.empty? if sort_orders.first.is_a?(Array) && sort_orders.first.first.is_a?(Array) sort_orders = sort_orders.first else sort_orders = sort_orders.map{ |order| order.is_a?(Array) ? order : [order, true] } unless sort_orders.all?{ |order| order.is_a?(Array) } end records.sort!(&sort_proc(records.first.class, sort_orders)) Collator.clear records end |