Module: Iudex::DA::Filters::FactoryHelper
- Includes:
- Filter::KeyHelper
- Defined in:
- lib/iudex-da/factory_helper.rb
Overview
Mixin FilterChainFactory helper methods
Instance Method Summary collapse
-
#create_read_filter(*args) ⇒ Object
Create a ReadFilter given the provided options.
-
#create_update_filter(*args) ⇒ Object
Create an UpdateFilter given the provided options.
-
#data_source ⇒ Object
Lazy initialize DataSource.
- #field_mapper(fields) ⇒ Object
- #updater_chain(v, &block) ⇒ Object
Methods included from Filter::KeyHelper
Instance Method Details
#create_read_filter(*args) ⇒ Object
Create a ReadFilter given the provided options.
Options
- :fields
-
The Array of fields (Symbol or Key) to read from the database.
The positional parameters equivalent to ( :fields ) as defined above is also supported but deprecated.
111 112 113 114 115 116 |
# File 'lib/iudex-da/factory_helper.rb', line 111 def create_read_filter( *args ) opts = args.last.is_a?( Hash ) ? args.pop.dup : {} opts[ :fields ] ||= args.shift #deprecated ReadFilter.new( data_source, field_mapper( opts[ :fields ] ) ) end |
#create_update_filter(*args) ⇒ Object
Create an UpdateFilter given the provided options.
Options
- :fields
-
The Array of fields (Symbol or Key) to (re-)read from the database and update. The required :uhash field is included automatically.
- :max_retries
-
Maximum number of retries not including the initial attempt, in case of a database conflict (Default: 3)
- :isolation_level
-
A transaction isolation constant as defined in java.sql.Connection (Default: REPEATABLE_READ 0x04)
- :on_content
-
Filter option for current (content) UniMap.
- :on_ref_update
-
Filter option for REFERENCES that are already existing in the database
- :on_ref_new
-
Filter option for REFERENCES that are new (not found in db).
- :on_referer
-
Filter option for the REFERER to the current content.
The positional parameters equivalent to ( :fields, :on_content, :on_ref_update, :on_ref_new ) as defined above are also supported but deprecated.
Filter options
Each of the on_* filter options defined above may take a value of a Filter, Array of filters or a Symbol value. The following symbol values are special, all other Symbol values are passed as the :filters option to create_chain, and is interpreted as a method name.
- :merge
-
The default behavior of merging the in-memory state with the current database state. This is equivalent to providing a NoOpFilter.
- :ignore
-
Do not update this element. This is equivalent to providing a filter that always rejects, but faster since it need not read the value from the db.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/iudex-da/factory_helper.rb', line 81 def create_update_filter( *args ) opts = args.last.is_a?( Hash ) ? args.pop.dup : {} opts[ :fields ] ||= args.shift #deprecated opts[ :on_content ] ||= args.shift opts[ :on_ref_update ] ||= args.shift opts[ :on_ref_new ] ||= args.shift f = UpdateFilter.new( data_source, field_mapper( opts[ :fields ] ) ) updater_chain( opts[:on_ref_update]) { |c| f.update_ref_filter = c } updater_chain( opts[ :on_ref_new ] ) { |c| f.new_ref_filter = c } updater_chain( opts[ :on_content ] ) { |c| f.content_filter = c } updater_chain( opts[ :on_referer ] ) { |c| f.referer_filter = c } f.max_retries = opts[ :max_retries ] if opts[ :max_retries ] f.isolation_level = opts[:isolation_level] if opts[:isolation_level] f end |
#data_source ⇒ Object
Lazy initialize DataSource
30 31 32 |
# File 'lib/iudex-da/factory_helper.rb', line 30 def data_source @data_source ||= PoolDataSourceFactory.new.create end |
#field_mapper(fields) ⇒ Object
118 119 120 121 |
# File 'lib/iudex-da/factory_helper.rb', line 118 def field_mapper( fields ) fields = keys( ( [ :uhash ] + fields ).flatten.compact ) ContentMapper.new( fields ) end |
#updater_chain(v, &block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/iudex-da/factory_helper.rb', line 123 def updater_chain( v, &block ) if v.is_a?( Iudex::Filter::Filter ) block.call( v ) elsif v == :merge block.call( UpdateFilter::DEFAULT_MERGE ) elsif v == :ignore block.call( nil ) else create_chain( :filters => v, &block ) end end |