Class: Hold::Sequel::PropertyMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/hold/sequel/property_mapper.rb

Overview

Abstract superclass.

Responsibility of a PropertyMapper is to map data for a particular property of a model class, between the instances of that model class, and the database

Defined Under Namespace

Classes: Array, Column, CreatedAt, CustomQuery, CustomQuerySingleValue, ForeignKey, Hash, Identity, ManyToMany, OneToMany, TransformedColumn, UpdatedAt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, property_name, _options = nil, &block) ⇒ PropertyMapper

If you pass a block, it will be instance_evalled, allowing you to create one-off custom property mappers by overriding bits of this implementation in the block.



18
19
20
21
22
# File 'lib/hold/sequel/property_mapper.rb', line 18

def initialize(repo, property_name, _options = nil, &block)
  @repository = repo
  @property_name = property_name
  instance_eval(&block) if block
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



13
14
15
# File 'lib/hold/sequel/property_mapper.rb', line 13

def property
  @property
end

#property_nameObject (readonly)

Returns the value of attribute property_name.



13
14
15
# File 'lib/hold/sequel/property_mapper.rb', line 13

def property_name
  @property_name
end

#repositoryObject (readonly)

Returns the value of attribute repository.



13
14
15
# File 'lib/hold/sequel/property_mapper.rb', line 13

def repository
  @repository
end

Class Method Details

.setter_dependencies_for(_options = {}) ⇒ Object



9
10
11
# File 'lib/hold/sequel/property_mapper.rb', line 9

def self.setter_dependencies_for(_options = {})
  {}
end

Instance Method Details

#build_insert_row(_entity, _table, _row, _id = nil) ⇒ Object

gets this property off the entity, and sets associated keys on a sequel row hash for insertion into the given table. May be passed an ID if an last_insert_id id value for the entity was previously obtained from an ID sequence on insertion into another table as part of the same combined entity store_new.

this is called inside the transaction which wraps the insert, so this is effectively your pre-insert hook and you can safely do other things inside it in the knowledge they’ll be rolled back in the event of a subsequent problem.



97
98
# File 'lib/hold/sequel/property_mapper.rb', line 97

def build_insert_row(_entity, _table, _row, _id = nil)
end

#build_update_row(_update_entity, _table, _row) ⇒ Object

gets this property off the update_entity, and sets associated keys on a sequel row hash for update of the given table for the given entity.

as with build_update_row, this is done inside the update transaction, it’s effectively your pre-update hook.



105
106
# File 'lib/hold/sequel/property_mapper.rb', line 105

def build_update_row(_update_entity, _table, _row)
end

#columns_aliases_and_tables_for_select(_preferred_table = nil) ⇒ Object

columns: column names to include in a SELECT in order to select this property. These should be qualified with the relevant table name but not aliased.

aliases: the above columns, aliased for use in the SELECT clause. be alias should something unique which the mapper can later use to retreive from a result row.

Any tables which need to be present in the FROM clause in order to select the columns. Relevant joins will be constructed by the parent repo.

A ‘preferred_table’ hint may be passed by the repo to indicate that it’d prefer you load the column off a particular table; at present this is only used by the IdentityMapper



39
40
41
# File 'lib/hold/sequel/property_mapper.rb', line 39

def columns_aliases_and_tables_for_select(_preferred_table = nil)
  [[], [], []]
end

#load_value(_row = nil, _id = nil, _properties = nil) ⇒ Object

Obtains the value of this property from a sequel result row and/or identity value.

where the mapper has columns_aliases_and_tables_for_select, it will get passed a result row object here which contains the sql values for these columns (amongst others potentially)

Where the identity value is available it will also be passed.

One or other of id, row must always be passed.



53
54
# File 'lib/hold/sequel/property_mapper.rb', line 53

def load_value(_row = nil, _id = nil, _properties = nil)
end

#load_values(rows = nil, ids = nil, properties = nil) ⇒ Object

like load_value, but works in a batched fashion, allowing a batched loading strategy to be used for associated objects. takes a block and yields the loaded values one at a time to it together with their index



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hold/sequel/property_mapper.rb', line 125

def load_values(rows = nil, ids = nil, properties = nil)
  if rows
    rows.each_with_index do |row, i|
      yield load_value(row, ids && ids[i], properties), i
    end
  else
    ids.each_with_index do |id, i|
      yield load_value(nil, id, properties), i
    end
  end
end

#make_filter(_value, _columns_mapped_to) ⇒ Object

used to make a sequel filter condition setting relevant columns equal to values equivalent to the given property value. May raise if mapper doesn’t support this



111
112
113
# File 'lib/hold/sequel/property_mapper.rb', line 111

def make_filter(_value, _columns_mapped_to)
  fail Hold::UnsupportedOperation
end

#make_multi_filter(_values, _columns_mapped_to) ⇒ Object

As for make_filter but takes multiple possible values and does a column IN (1,2,3,4) type thing.



117
118
119
# File 'lib/hold/sequel/property_mapper.rb', line 117

def make_multi_filter(_values, _columns_mapped_to)
  fail Hold::UnsupportedOperation
end

#post_delete(_entity) ⇒ Object

called inside the DELETE transaction for a given entity.

this is called last thing after rows are deleted for each table of the repo.



84
85
# File 'lib/hold/sequel/property_mapper.rb', line 84

def post_delete(_entity)
end

#post_insert(_entity, _rows, _last_insert_id = nil) ⇒ Object

called after rows built via build_insert_row have successfully been used in a INSERT for the entity passed. Should update the entity property, where appropriate, with any default values which were supplied by the repository (via default_for) on insert, and should do any additional work in order to save any values which are not mapped to columns on one of the repo’s own :tables

Is also passed the last_insert_id resulting from any insert, to help fill out any autoincrement primary key column.

is executed inside the same transaction as the INSERT



148
149
# File 'lib/hold/sequel/property_mapper.rb', line 148

def post_insert(_entity, _rows, _last_insert_id = nil)
end

#post_update(_entity, _update_entity, _rows, _data_from_pre_update) ⇒ Object

called after rows built via build_update_row have successfully been used in a UPDATE for the id and update_entity passed. Should update the entity property, where appropriate, with any default values which were supplied by the repository (via default_for) on update, and should do any additional work in order to save any values which are not mapped to columns on one of the repo’s own :tables

is executed inside the same transaction as the UPDATE



159
160
# File 'lib/hold/sequel/property_mapper.rb', line 159

def post_update(_entity, _update_entity, _rows, _data_from_pre_update)
end

#pre_delete(_entity) ⇒ Object

called inside the DELETE transaction for a given entity.

this is called first thing before rows are deleted for each table of the repo.



77
78
# File 'lib/hold/sequel/property_mapper.rb', line 77

def pre_delete(_entity)
end

#pre_insert(_entity) ⇒ Object

called inside the INSERT transaction for insertion of the given entity.

this is called first thing before insert rows are built (via build_insert_row) for each table of the repo.



60
61
# File 'lib/hold/sequel/property_mapper.rb', line 60

def pre_insert(_entity)
end

#pre_update(_entity, _update_entity) ⇒ Object

called inside the UPDATE transaction for insertion of the given entity.

this is called first thing before update rows are built (via build_update_row) for each table of the repo.

anything returned from pre_update will be passed to post_update’s data_from_pre_update arg if the update succeeds.



70
71
# File 'lib/hold/sequel/property_mapper.rb', line 70

def pre_update(_entity, _update_entity)
end