Module: Curator::Repository::ClassMethods
- Defined in:
- lib/curator/repository.rb
Instance Method Summary collapse
- #_build_finder_methods(field_name) ⇒ Object
- #_deserialize(id, data) ⇒ Object
- #_find_by_index(collection_name, field_name, value) ⇒ Object
- #_format_time_for_index(time) ⇒ Object
- #_indexed_fields ⇒ Object
- #_indexes(object) ⇒ Object
- #_serialize(object) ⇒ Object
- #_update_timestamps(object) ⇒ Object
- #collection_name ⇒ Object
- #data_store ⇒ Object
- #data_store=(store) ⇒ Object
- #delete(object) ⇒ Object
- #deserialize(attributes) ⇒ Object
- #find_by_created_at(start_time, end_time) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_by_updated_at(start_time, end_time) ⇒ Object
- #find_by_version(version) ⇒ Object
- #indexed_fields(*fields) ⇒ Object
- #klass ⇒ Object
- #migrator ⇒ Object
- #save(object) ⇒ Object
- #save_without_timestamps(object) ⇒ Object
- #serialize(object) ⇒ Object
Instance Method Details
#_build_finder_methods(field_name) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/curator/repository.rb', line 88 def _build_finder_methods(field_name) eigenclass = class << self; self; end eigenclass.class_eval do define_method("find_by_#{field_name}") do |value| _find_by_index(collection_name, field_name, value) end define_method("find_first_by_#{field_name}") do |value| _find_by_index(collection_name, field_name, value).first end end end |
#_deserialize(id, data) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/curator/repository.rb', line 112 def _deserialize(id, data) attributes = data.with_indifferent_access migrated_attributes = migrator.migrate(attributes) migrated_attributes[:id] = id migrated_attributes[:created_at] = Time.parse(migrated_attributes[:created_at]) if migrated_attributes[:created_at] migrated_attributes[:updated_at] = Time.parse(migrated_attributes[:updated_at]) if migrated_attributes[:updated_at] deserialize(migrated_attributes) end |
#_find_by_index(collection_name, field_name, value) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/curator/repository.rb', line 100 def _find_by_index(collection_name, field_name, value) if results = data_store.find_by_index(collection_name, field_name, value) results.map do |hash| _deserialize(hash[:key], hash[:data]) end end end |
#_format_time_for_index(time) ⇒ Object
121 122 123 |
# File 'lib/curator/repository.rb', line 121 def _format_time_for_index(time) time.to_json.gsub('"', '') end |
#_indexed_fields ⇒ Object
125 126 127 |
# File 'lib/curator/repository.rb', line 125 def _indexed_fields @indexed_fields || [] end |
#_indexes(object) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/curator/repository.rb', line 129 def _indexes(object) index_values = _indexed_fields.map { |field| [field, object.send(field)] } index_values += [ [:created_at, _format_time_for_index(object.send(:created_at))], [:updated_at, _format_time_for_index(object.send(:updated_at))], [:version, object.version] ] Hash[index_values] end |
#_serialize(object) ⇒ Object
139 140 141 |
# File 'lib/curator/repository.rb', line 139 def _serialize(object) serialize(object).reject { |key, val| val.nil? }.merge(:version => object.version) end |
#_update_timestamps(object) ⇒ Object
143 144 145 146 |
# File 'lib/curator/repository.rb', line 143 def (object) object.updated_at = Time.now.utc object.created_at ||= object.updated_at end |
#collection_name ⇒ Object
11 12 13 |
# File 'lib/curator/repository.rb', line 11 def collection_name ActiveSupport::Inflector.tableize(klass) end |
#data_store ⇒ Object
15 16 17 |
# File 'lib/curator/repository.rb', line 15 def data_store @data_store ||= Curator.data_store end |
#data_store=(store) ⇒ Object
19 20 21 |
# File 'lib/curator/repository.rb', line 19 def data_store=(store) @data_store = store end |
#delete(object) ⇒ Object
23 24 25 26 |
# File 'lib/curator/repository.rb', line 23 def delete(object) data_store.delete(collection_name, object.id) nil end |
#deserialize(attributes) ⇒ Object
108 109 110 |
# File 'lib/curator/repository.rb', line 108 def deserialize(attributes) klass.new(attributes) end |
#find_by_created_at(start_time, end_time) ⇒ Object
28 29 30 |
# File 'lib/curator/repository.rb', line 28 def find_by_created_at(start_time, end_time) _find_by_index(collection_name, :created_at, _format_time_for_index(start_time).._format_time_for_index(end_time)) end |
#find_by_id(id) ⇒ Object
40 41 42 43 44 |
# File 'lib/curator/repository.rb', line 40 def find_by_id(id) if hash = data_store.find_by_key(collection_name, id) _deserialize(hash[:key], hash[:data]) end end |
#find_by_updated_at(start_time, end_time) ⇒ Object
32 33 34 |
# File 'lib/curator/repository.rb', line 32 def find_by_updated_at(start_time, end_time) _find_by_index(collection_name, :updated_at, _format_time_for_index(start_time).._format_time_for_index(end_time)) end |
#find_by_version(version) ⇒ Object
36 37 38 |
# File 'lib/curator/repository.rb', line 36 def find_by_version(version) _find_by_index(collection_name, :version, version) end |
#indexed_fields(*fields) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/curator/repository.rb', line 46 def indexed_fields(*fields) @indexed_fields = fields @indexed_fields.each do |field_name| _build_finder_methods(field_name) end end |
#klass ⇒ Object
54 55 56 |
# File 'lib/curator/repository.rb', line 54 def klass name.to_s.gsub("Repository", "").constantize end |
#migrator ⇒ Object
58 59 60 |
# File 'lib/curator/repository.rb', line 58 def migrator @migrator ||= Curator::Migrator.new(collection_name) end |
#save(object) ⇒ Object
62 63 64 65 |
# File 'lib/curator/repository.rb', line 62 def save(object) object.touch (object) end |
#save_without_timestamps(object) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/curator/repository.rb', line 67 def (object) hash = { :collection_name => collection_name, :value => _serialize(object), :index => _indexes(object) } if object.id hash[:key] = object.id data_store.save(hash) else object.instance_variable_set("@id", data_store.save(hash)) end object end |
#serialize(object) ⇒ Object
84 85 86 |
# File 'lib/curator/repository.rb', line 84 def serialize(object) object.instance_values end |