Module: DynamicModelQueryable

Defined in:
lib/dwcr/dynamic_model_queryable.rb

Overview

DynamicModelQueryable contains methods for the dynamic DwCR models.

Defined Under Namespace

Modules: DynamicModelClassQueryable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object

Extends the class that DynamicModelQueryable is mixed in with DynamicModelClassQueryable



41
42
43
# File 'lib/dwcr/dynamic_model_queryable.rb', line 41

def self.included(host_class)
  host_class.extend(DynamicModelClassQueryable)
end

Instance Method Details

#core_rowObject

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



46
47
48
49
# File 'lib/dwcr/dynamic_model_queryable.rb', line 46

def core_row
  return nil if entity.is_core
  send(entity.core.name)
end

#extension_rowsObject

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



53
54
55
56
# 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_valuesObject

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



59
60
61
62
63
# 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_aObject

Returns a nested array of values only in consistent order.



66
67
68
69
70
71
72
# 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) ⇒ Object

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



77
78
79
80
81
82
83
# 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_jsonObject

Returns the #full_record for self as JSON.



86
87
88
# File 'lib/dwcr/dynamic_model_queryable.rb', line 86

def to_json
  JSON.generate(to_record)
end

#to_record(keys: :term) ⇒ Object

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# 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