module DynamicModelQueryable

DynamicModelQueryable contains methods for the dynamic DwCR models.

Public Class Methods

included(host_class) click to toggle source

Extends the class that DynamicModelQueryable is mixed in with DynamicModelClassQueryable

# File lib/dwcr/dynamic_model_queryable.rb, line 41
def self.included(host_class)
  host_class.extend(DynamicModelClassQueryable)
end

Public Instance Methods

core_row() click to toggle source

Returns the core row for self. Will return nil if self is the core.

# File lib/dwcr/dynamic_model_queryable.rb, line 46
def core_row
  return nil if entity.is_core
  send(entity.core.name)
end
extension_rows() click to toggle source

Returns an array of all related extension rows for self. Will return nil if self is an extension.

# File lib/dwcr/dynamic_model_queryable.rb, line 53
def extension_rows
  return nil unless entity.is_core
  entity.extensions.map { |xtn| send(xtn.table_name) }.flatten
end
row_values() click to toggle source

Returns a value hash for self without primary or foreign keys.

# File lib/dwcr/dynamic_model_queryable.rb, line 59
def row_values
  keys_to_delete = %i[id entity_id]
  keys_to_delete.push(entity.core&.foreign_key).compact
  to_hash.clone.delete_if { |key, _| keys_to_delete.include? key }
end
to_a() click to toggle source

Returns a nested array of values only in consistent order.

# File lib/dwcr/dynamic_model_queryable.rb, line 66
def to_a
  row_array = row_values.map { |_key, value| value }
  return row_array unless entity.is_core
  entity.extensions.inject(row_array) do |memo, xtn|
    memo << send(xtn.table_name).map(&:to_a)
  end
end
to_hash_with(keys = :term) click to toggle source

Returns a value hash for the row without primary or foreign keys where the keys in the hash can be the term, baseterm, or name of the attributes, depending on the argument given

# File lib/dwcr/dynamic_model_queryable.rb, line 77
def to_hash_with(keys = :term)
  return row_values if keys == :name
  row_values.transform_keys do |key|
    attribute = entity.attributes_dataset.first(name: key.to_s)
    attribute.send(keys)
  end
end
to_json() click to toggle source

Returns the full_record for self as JSON.

# File lib/dwcr/dynamic_model_queryable.rb, line 86
def to_json
  JSON.generate(to_record)
end
to_record(keys: :term) click to toggle source

Returns the full record (current row and all related rows) for self as a hash with keys (:term, :baseterm, or :name).

# File lib/dwcr/dynamic_model_queryable.rb, line 92
def to_record(keys: :term)
  record_hash = to_hash_with(keys)
  if entity.is_core
    extension_rows.each do |row|
      key = row.entity.send(keys)
      record_hash[key] ||= []
      record_hash[key] << row.to_hash_with(keys)
    end
  else
    record_hash[entity.core.send(keys)] = core_row.to_hash_with(keys)
  end
  record_hash
end