class Whiteprint::Base

Attributes

attributes[RW]
configs[RW]
model[RW]
options[RW]

Public Class Methods

applicable?(_) click to toggle source
# File lib/whiteprint/base.rb, line 6
def applicable?(_)
  true
end
load_plugins() click to toggle source
# File lib/whiteprint/base.rb, line 10
def load_plugins
  Whiteprint.config.plugins.each do |plugin|
    include Whiteprint.plugins[plugin]
  end
end
new(model, **_options) click to toggle source
# File lib/whiteprint/base.rb, line 17
def initialize(model, **_options)
  singleton_class.send :load_plugins

  self.model                 = model
  self.options               = _options
  self.attributes            = Attributes.new(nil, model: model)
  self.configs               = []
end

Public Instance Methods

changes?() click to toggle source
# File lib/whiteprint/base.rb, line 55
def changes?
  changes = persisted_attributes.diff(attributes, type: :persisted)
  changes.any? do |_, attributes|
    !attributes.to_a.reject(&:virtual).empty?
  end
end
changes_tree() click to toggle source
# File lib/whiteprint/base.rb, line 40
def changes_tree
  changes = persisted_attributes.diff(attributes.not(virtual: true), type: :persisted)

  attributes = changes.flat_map do |kind, changed_attributes|
    changed_attributes.to_a.map do |attribute|
      next if attribute.virtual
      attribute.for_persisted(kind: kind).to_h
    end.compact
  end

  table_name_without_schema = table_name.split('.').last

  { table_name: table_name_without_schema, table_exists: table_exists?, attributes: attributes }
end
clone_to(model) click to toggle source
# File lib/whiteprint/base.rb, line 62
def clone_to(model)
  clone = ::Whiteprint.new(model, **self.options)
  self.configs.each do |config|
    clone.execute(&config)
  end
  model.instance_variable_set :@_whiteprint, clone
  Whiteprint.models += [model]
end
execute(&block) click to toggle source
# File lib/whiteprint/base.rb, line 26
def execute(&block)
  self.configs << block
  instance_eval(&block)
end
explanation(index = 1, width = 100) click to toggle source
# File lib/whiteprint/base.rb, line 31
def explanation(index = 1, width = 100)
  return unless changes?
  table = Terminal::Table.new(Explanation.apply(self, index, width: width))
  table.render
  table
rescue => e
  explanation(index, width + 10)
end
method_missing(type, name, **options) click to toggle source
# File lib/whiteprint/base.rb, line 91
def method_missing(type, name, **options)
  @attributes.add name: name.to_sym, type: type, **options
end
persisted_attributes() click to toggle source
# File lib/whiteprint/base.rb, line 71
def persisted_attributes
  Whiteprint::Attributes.new
end
set_model(model) click to toggle source
# File lib/whiteprint/base.rb, line 75
def set_model(model)
  self.model = model
end
table_exists?() click to toggle source
# File lib/whiteprint/base.rb, line 83
def table_exists?
  model.table_exists?
end
table_name() click to toggle source
# File lib/whiteprint/base.rb, line 79
def table_name
  model.table_name
end
transformer() click to toggle source
# File lib/whiteprint/base.rb, line 87
def transformer
  self.class
end