Module: ReeDao::DatasetExtensions::InstanceMethods
- Defined in:
- lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb
Constant Summary collapse
- PERSISTENCE_STATE_VARIABLE =
:@persistence_state
- IMPORT_BATCH_SIZE =
1000
Instance Method Summary collapse
- #all(mapper: nil) ⇒ Object
- #delete(hash_or_entity = nil) ⇒ Object
- #delete_all ⇒ Object
-
#find(id, mapper: nil) ⇒ Object
override methods.
- #first(mapper: nil) ⇒ Object
- #ids(ids_list) ⇒ Object
- #import_all(entities, batch_size: IMPORT_BATCH_SIZE) ⇒ Object
- #last(mapper: nil) ⇒ Object
- #naked_first ⇒ Object
- #naked_last ⇒ Object
- #persist(*args, set: nil) ⇒ Object
- #put(entity) ⇒ Object
- #put_or_update(entity, set: nil) ⇒ Object
- #put_with_conflict(entity, conflict_opts = {}) ⇒ Object
- #update(hash_or_entity) ⇒ Object
- #with_lock ⇒ Object
- #with_mapper(mapper) ⇒ Object
Instance Method Details
#all(mapper: nil) ⇒ Object
38 39 40 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 38 def all(mapper: nil) with_mapper(mapper).__original_all end |
#delete(hash_or_entity = nil) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 173 def delete(hash_or_entity = nil) return __original_delete if hash_or_entity.nil? return where(hash_or_entity).__original_delete if hash_or_entity.is_a?(Hash) key_condition = prepare_key_condition_from_entity(hash_or_entity) where(key_condition).__original_delete end |
#delete_all ⇒ Object
42 43 44 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 42 def delete_all where({}).__original_delete end |
#find(id, mapper: nil) ⇒ Object
override methods
26 27 28 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 26 def find(id, mapper: nil) where(primary_key => id).first(mapper: mapper) end |
#first(mapper: nil) ⇒ Object
30 31 32 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 30 def first(mapper: nil) with_mapper(mapper).__original_first end |
#ids(ids_list) ⇒ Object
139 140 141 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 139 def ids(ids_list) where(id: ids_list) end |
#import_all(entities, batch_size: IMPORT_BATCH_SIZE) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 108 def import_all(entities, batch_size: IMPORT_BATCH_SIZE) return if entities.empty? columns = self.columns raw = {} columns.delete(:id) columns.delete(:row) data = entities.map do |entity| hash = dump_entity(entity) raw[entity] = hash columns.map { hash[_1] } end ids = import( columns, data, slice: batch_size, return: :primary_key ) ids.each_with_index do |id, index| entity = entities[index] raw_data = raw[entity] set_entity_primary_key(entity, raw_data, id) set_persistence_state(entity, raw_data) end nil end |
#last(mapper: nil) ⇒ Object
34 35 36 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 34 def last(mapper: nil) with_mapper(mapper).reverse_order(:id).first end |
#naked_first ⇒ Object
165 166 167 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 165 def naked_first __original_first end |
#naked_last ⇒ Object
169 170 171 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 169 def naked_last __original_last end |
#persist(*args, set: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 47 def persist(*args, set: nil) args.each do |arg| if arg.is_a?(ReeDto::DSL) put_or_update(arg, set: set) elsif arg.is_a?(ReeDto::DtoCollection) || arg.is_a?(Array) || arg.is_a?(Enumerable) arg.each { put_or_update(_1, set: set) } end end end |
#put(entity) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 71 def put(entity) raw = dump_entity(entity) remove_null_primary_key(raw) key = insert(raw) set_entity_primary_key(entity, raw, key) set_persistence_state(entity, raw) entity end |
#put_or_update(entity, set: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 57 def put_or_update(entity, set: nil) if set set.each do |key, val| entity.send("#{key}=", val) end end if entity.id update(entity) else put(entity) end end |
#put_with_conflict(entity, conflict_opts = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 83 def put_with_conflict(entity, conflict_opts = {}) raw = opts[:schema_mapper].db_dump(entity) remove_null_primary_key(raw) if conflict_opts.key?(:update) && conflict_opts[:update].empty? conflict_opts.delete(:update) elsif conflict_opts[:update].is_a?(Array) update = {} conflict_opts[:update].each do |column| update[column] = raw[column] end conflict_opts[:update] = update end key = insert_conflict(conflict_opts).insert(raw) return if key.nil? set_entity_primary_key(entity, raw, key) set_persistence_state(entity, raw) where(primary_key => key).first end |
#update(hash_or_entity) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 143 def update(hash_or_entity) if !opts[:schema_mapper] || hash_or_entity.is_a?(Hash) return __original_update(hash_or_entity) end dump = dump_entity(hash_or_entity) raw = extract_changes(hash_or_entity, dump) if !raw.empty? key_condition = prepare_key_condition_from_entity(hash_or_entity) where(key_condition).__original_update(raw) if is_ree_dto?(hash_or_entity) hash_or_entity.reset_changes else update_persistence_state(hash_or_entity, raw) end end hash_or_entity end |
#with_lock ⇒ Object
181 182 183 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 181 def with_lock for_update end |
#with_mapper(mapper) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb', line 185 def with_mapper(mapper) clone( schema_mapper: mapper || opts[:schema_mapper], ).with_row_proc( Proc.new { |hash| m = mapper || opts[:schema_mapper] if m entity = m.db_load(hash) self.set_persistence_state(entity, hash) entity else hash end } ) end |