class Sprig::Reap::Model

Attributes

existing_sprig_ids[W]
klass[R]
model_input[R]

Public Class Methods

all() click to toggle source
# File lib/sprig/reap/model.rb, line 6
def self.all
  @@all ||= begin
    models = Sprig::Reap.models.map { |model_input| new(model_input) }

    tsorted_classes(models).map do |klass|
      models.find { |model| model.klass == klass }
    end
  end
end
find(klass, id) click to toggle source
# File lib/sprig/reap/model.rb, line 16
def self.find(klass, id)
  all.find { |model| model.klass == klass }.find(id)
end
new(model_input) click to toggle source
# File lib/sprig/reap/model.rb, line 23
def initialize(model_input)
  @model_input = Sprig::Reap::Inputs.Model(model_input)
  @klass       = @model_input.klass
end

Private Class Methods

tsorted_classes(models) click to toggle source
# File lib/sprig/reap/model.rb, line 89
def self.tsorted_classes(models)
  models.reduce(TsortableHash.new) do |hash, model|
    hash.merge(model.klass => model.dependencies)
  end.resolve_circular_habtm_dependencies!.tsort
end

Public Instance Methods

associations() click to toggle source
# File lib/sprig/reap/model.rb, line 36
def associations
  @associations ||= klass.reflect_on_all_associations.select(&has_dependencies?).map do |association|
    Association.new(association)
  end
end
attributes() click to toggle source
# File lib/sprig/reap/model.rb, line 28
def attributes
  klass.column_names - Sprig::Reap.ignored_attrs
end
dependencies() click to toggle source
# File lib/sprig/reap/model.rb, line 32
def dependencies
  @dependencies ||= associations.flat_map(&:dependencies)
end
existing_sprig_ids() click to toggle source
# File lib/sprig/reap/model.rb, line 42
def existing_sprig_ids
  @existing_sprig_ids ||= []
end
find(id) click to toggle source
# File lib/sprig/reap/model.rb, line 50
def find(id)
  records.find { |record| record.id == id }
end
generate_sprig_id() click to toggle source
# File lib/sprig/reap/model.rb, line 46
def generate_sprig_id
  existing_sprig_ids.select { |i| i.is_a? Integer }.sort.last + 1
end
records() click to toggle source
# File lib/sprig/reap/model.rb, line 74
def records
  @records ||= model_input.records.map { |record| Record.new(record, self) }
rescue => e
  log_error "Encountered an error when pulling the database records for #{to_s}:\r#{e.message}"
  []
end
to_s() click to toggle source
# File lib/sprig/reap/model.rb, line 54
def to_s
  klass.to_s
end
to_yaml(options = {}) click to toggle source
# File lib/sprig/reap/model.rb, line 58
def to_yaml(options = {})
  return if records.empty?

  namespace         = options[:namespace]
  formatted_records = [records.map(&:to_hash)]

  yaml = if namespace
    { namespace => formatted_records }.to_yaml
  else
    formatted_records.to_yaml
  end

  yaml.gsub!('- -', '  -') # Remove the extra array marker used to indent the sprig records
  yaml.gsub!("---\n", '')  # Remove annoying YAML separator
end

Private Instance Methods

has_dependencies?() click to toggle source
# File lib/sprig/reap/model.rb, line 83
def has_dependencies?
  proc do |association|
    [:belongs_to, :has_and_belongs_to_many].include? association.macro
  end
end