Module: DynamicModelQueryable

Defined in:
lib/dwcr/dynamic_model_queryable.rb

Overview

convenience methods for the dynamic 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



36
37
38
# File 'lib/dwcr/dynamic_model_queryable.rb', line 36

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.



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

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.



48
49
50
51
# File 'lib/dwcr/dynamic_model_queryable.rb', line 48

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.



54
55
56
57
58
# File 'lib/dwcr/dynamic_model_queryable.rb', line 54

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.



61
62
63
64
65
66
67
# File 'lib/dwcr/dynamic_model_queryable.rb', line 61

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



72
73
74
75
76
77
78
# File 'lib/dwcr/dynamic_model_queryable.rb', line 72

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.



81
82
83
# File 'lib/dwcr/dynamic_model_queryable.rb', line 81

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).



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dwcr/dynamic_model_queryable.rb', line 87

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