Module: NcsNavigator::Warehouse::Transformers::Database::DSL

Defined in:
lib/ncs_navigator/warehouse/transformers/database.rb

Overview

The DSL available when a class mixes in the Database module.

Instance Method Summary collapse

Instance Method Details

#bcdatabase(name_and_group = {})

This method returns an undefined value.

Defines the bcdatabase specification to use when connecting to the database for this enumeration.



213
214
215
216
217
218
219
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 213

def bcdatabase(name_and_group={})
  if name_and_group.empty?
    @bcdatabase ||= { }
  else
    @bcdatabase = (self.bcdatabase || {}).merge(name_and_group)
  end
end

#ignored_columns(*columns) ⇒ Object

Indicates a set of columns which which should not be checked for used-ness by #model_row, even if the :unused option is set to :fail.



200
201
202
203
204
205
206
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 200

def ignored_columns(*columns)
  if columns.empty?
    @ignored_columns ||= []
  else
    @ignored_columns = columns.collect(&:to_s)
  end
end

#on_unused_columns(setting = nil) ⇒ :ignore, :fail

What to do if are encountered by #model_row. Default is :ignore but may be set to :fail for all producers using this method. The value set here may be overridden per #model_row call.

Returns:

  • (:ignore, :fail)


188
189
190
191
192
193
194
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 188

def on_unused_columns(setting=nil)
  if setting
    @on_unused_columns = setting
  else
    @on_unused_columns ||= :ignore
  end
end

#produce_one_for_one(name, model_or_reference, options = {})

This method returns an undefined value.

Define a translation from the results of a query into exactly one warehouse record per result row. This method, while more restrictive than #produce_records, allows for rapidly mapping data which is already a close match for MDES records.

The mapping uses a series of heuristics to map from the columns in each query result row to at most one MDES variable from the specified model.

  • If the column appears as a key in the :column_map hash, use the associated value as the target variable name.
  • If there's a :prefix option, the column is named X, and there's a variable named prefixX, use that variable.
  • If the column is named X and there's a variable named X, use that variable.
  • If the column is named X_code and there's a variable named X, use that variable.
  • If the column is named X_code and there's a variable named X_id, use that variable.
  • If the column is named X_other and there's a property named X_oth, use that variable.

Parameters:

  • name (Symbol)

    the name of this producer; if you don't specify a :query, the default is to return every row from the application table with this name.

  • model_or_reference (Class, Symbol)

    the warehouse model to which the results of the query will be mapped. It can be expressed either as the model class itself or the unqualified name of the model. (See Configuration#model.)

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :query (String)

    the query to execute for this producer. If not specified, the query is "SELECT * FROM #{name}".

  • :prefix (String)

    a prefix to use when looking for matching property values. (See above.)

  • :column_map (Hash<Symbol, Symbol>)

    explicit mapping from column name to MDES variable name. This mapping is consulted before the heuristics are applied.

  • :on_unused (:ignore, :fail)

    what to do when there are columns in the row which are not used.

  • :ignored_columns (Array<[String,Symbol]>)

    columns to consider "used" even if the heuristic or the column map don't match them to anything.



304
305
306
307
308
309
310
311
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 304

def produce_one_for_one(name, model_or_reference, options={})
  options[:column_map] =
    (options[:column_map] || {}).inject({}) { |h, (k, v)| h[k.to_s] = v.to_s; h }
  options[:ignored_columns] = (options[:ignored_columns] || []).collect(&:to_s)

  record_producers <<
    OneForOneProducer.new(name, options.delete(:query), model_or_reference, self, options)
end

#produce_records(name, options = {}, &logic)

This method returns an undefined value.

Define a translation from the results of a query into one or more warehouse records.

The optional second parameter the logic proc/block is a hash of metadata. That metadata contains a single key:

  • :configuration. Provides the Configuration in use by the warehouse that is executing this transformer. Among other things, this allows lookup of models Configuration#model based on the in-use MDES version.

Parameters:

  • name (Symbol)

    the name of this producer; if you don't specify a :query, the default is to return every row from the application table with this name.

  • options (Hash) (defaults to: {})
  • logic (Proc)

    an expression which receives one row from the data source and returns 0 or more warehouse records. The return value may be nil (for 0), a single instance, or an Array. If the expression accepts two arguments, the second one will be a hash of metadata related to the process.

Options Hash (options):

  • :query (String)

    the query to execute for this producer. If not specified, the query is "SELECT * FROM #{name}".



253
254
255
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 253

def produce_records(name, options={}, &logic)
  record_producers << RecordProducer.new(name, options[:query], logic)
end

#record_producersArray

The record producers defined by calls to #produce_records.

Returns:

  • (Array)


225
226
227
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 225

def record_producers
  @record_producers ||= []
end

#repository(repo_name)

This method returns an undefined value.

Parameters:

  • repo_name (Symbol)

    the data mapper repository to use in / set-up for this transformer.



169
170
171
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 169

def repository(repo_name)
  @repository_name = repo_name
end

#repository_nameSymbol

Returns The name defined by a previous call to #repository, or the default. The default is derived from the name of the class into which NcsNavigator::Warehouse::Transformers::Database is mixed.

Returns:



177
178
179
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 177

def repository_name
  @repository_name ||= name.demodulize.underscore.to_sym
end