Module: Curator::Repository::ClassMethods
- Defined in:
- lib/curator/repository.rb
Instance Method Summary collapse
- #_build_finder_methods(attribute) ⇒ Object
- #_deserialize(id, data) ⇒ Object
- #_find_by_attribute(attribute, value) ⇒ Object
- #_format_time_for_index(time) ⇒ Object
- #_indexed_fields ⇒ Object
- #_indexes(object) ⇒ Object
- #_serialize(object) ⇒ Object
- #_update_timestamps(object) ⇒ Object
- #all ⇒ Object
- #apply_settings! ⇒ Object
- #collection(explicit_collection_name) ⇒ Object
- #collection_name ⇒ Object
- #data_store ⇒ Object
- #data_store=(store) ⇒ Object
- #default_collection_name ⇒ 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
- #settings ⇒ Object
Instance Method Details
#_build_finder_methods(attribute) ⇒ Object
| 120 121 122 123 124 125 126 127 128 129 130 | # File 'lib/curator/repository.rb', line 120 def _build_finder_methods(attribute) eigenclass = class << self; self; end eigenclass.class_eval do define_method("find_by_#{attribute}") do |value| _find_by_attribute(attribute, value) end define_method("find_first_by_#{attribute}") do |value| _find_by_attribute(attribute, value).first end end end | 
#_deserialize(id, data) ⇒ Object
| 144 145 146 147 148 149 | # File 'lib/curator/repository.rb', line 144 def _deserialize(id, data) attributes = data.with_indifferent_access migrated_attributes = migrator.migrate(attributes) migrated_attributes[:id] = id deserialize(migrated_attributes) end | 
#_find_by_attribute(attribute, value) ⇒ Object
| 132 133 134 135 136 137 138 | # File 'lib/curator/repository.rb', line 132 def _find_by_attribute(attribute, value) if results = data_store.find_by_attribute(collection_name, attribute, value) results.map do |hash| _deserialize(hash[:key], hash[:data]) if hash end.compact end end | 
#_format_time_for_index(time) ⇒ Object
| 151 152 153 | # File 'lib/curator/repository.rb', line 151 def _format_time_for_index(time) time.to_json.gsub('"', '') end | 
#_indexed_fields ⇒ Object
| 155 156 157 | # File 'lib/curator/repository.rb', line 155 def _indexed_fields @indexed_fields || [] end | 
#_indexes(object) ⇒ Object
| 159 160 161 162 163 164 165 166 167 | # File 'lib/curator/repository.rb', line 159 def _indexes(object) index_values = _indexed_fields.map { |field| [field, serialize(object)[field.to_sym]] } 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
| 169 170 171 | # File 'lib/curator/repository.rb', line 169 def _serialize(object) serialize(object).reject { |key, val| val.nil? }.merge(:version => object.version) end | 
#_update_timestamps(object) ⇒ Object
| 173 174 175 176 | # File 'lib/curator/repository.rb', line 173 def (object) object.updated_at = Time.now.utc object.created_at ||= object.updated_at end | 
#all ⇒ Object
| 20 21 22 23 24 | # File 'lib/curator/repository.rb', line 20 def all data_store.find_all(collection_name).map do |result| _deserialize(result[:key], result[:data]) end end | 
#apply_settings! ⇒ Object
| 115 116 117 118 | # File 'lib/curator/repository.rb', line 115 def apply_settings! settings.apply!(:data_store => data_store, :collection_name => collection_name) end | 
#collection(explicit_collection_name) ⇒ Object
| 26 27 28 | # File 'lib/curator/repository.rb', line 26 def collection(explicit_collection_name) @collection_name = explicit_collection_name end | 
#collection_name ⇒ Object
| 30 31 32 | # File 'lib/curator/repository.rb', line 30 def collection_name @collection_name ||= default_collection_name end | 
#data_store ⇒ Object
| 38 39 40 | # File 'lib/curator/repository.rb', line 38 def data_store @data_store ||= Curator.data_store end | 
#data_store=(store) ⇒ Object
| 42 43 44 | # File 'lib/curator/repository.rb', line 42 def data_store=(store) @data_store = store end | 
#default_collection_name ⇒ Object
| 34 35 36 | # File 'lib/curator/repository.rb', line 34 def default_collection_name ActiveSupport::Inflector.tableize(klass) end | 
#delete(object) ⇒ Object
| 46 47 48 49 | # File 'lib/curator/repository.rb', line 46 def delete(object) data_store.delete(collection_name, object.id) nil end | 
#deserialize(attributes) ⇒ Object
| 140 141 142 | # File 'lib/curator/repository.rb', line 140 def deserialize(attributes) klass.new(attributes) end | 
#find_by_created_at(start_time, end_time) ⇒ Object
| 51 52 53 | # File 'lib/curator/repository.rb', line 51 def find_by_created_at(start_time, end_time) _find_by_attribute(:created_at, _format_time_for_index(start_time).._format_time_for_index(end_time)) end | 
#find_by_id(id) ⇒ Object
| 63 64 65 66 67 | # File 'lib/curator/repository.rb', line 63 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
| 55 56 57 | # File 'lib/curator/repository.rb', line 55 def find_by_updated_at(start_time, end_time) _find_by_attribute(:updated_at, _format_time_for_index(start_time).._format_time_for_index(end_time)) end | 
#find_by_version(version) ⇒ Object
| 59 60 61 | # File 'lib/curator/repository.rb', line 59 def find_by_version(version) _find_by_attribute(:version, version) end | 
#indexed_fields(*fields) ⇒ Object
| 69 70 71 72 73 74 75 | # File 'lib/curator/repository.rb', line 69 def indexed_fields(*fields) @indexed_fields = fields @indexed_fields.each do |field_name| _build_finder_methods(field_name) end end | 
#klass ⇒ Object
| 77 78 79 | # File 'lib/curator/repository.rb', line 77 def klass name.to_s.gsub("Repository", "").constantize end | 
#migrator ⇒ Object
| 81 82 83 | # File 'lib/curator/repository.rb', line 81 def migrator @migrator ||= Curator::Migrator.new(collection_name) end | 
#save(object) ⇒ Object
| 85 86 87 88 | # File 'lib/curator/repository.rb', line 85 def save(object) object.touch (object) end | 
#save_without_timestamps(object) ⇒ Object
| 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # File 'lib/curator/repository.rb', line 90 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
| 107 108 109 | # File 'lib/curator/repository.rb', line 107 def serialize(object) HashWithIndifferentAccess.new(object.instance_values) end | 
#settings ⇒ Object
| 111 112 113 | # File 'lib/curator/repository.rb', line 111 def settings @settings ||= Settings.new(data_store.settings(collection_name)) end |