Class: Whiteprint::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/whiteprint/base.rb

Direct Known Subclasses

Adapters::ActiveRecord, Adapters::Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, **_options) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, name, **options) ⇒ Object



91
92
93
# File 'lib/whiteprint/base.rb', line 91

def method_missing(type, name, **options)
  @attributes.add name: name.to_sym, type: type, **options
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/whiteprint/base.rb', line 3

def attributes
  @attributes
end

#configsObject

Returns the value of attribute configs.



3
4
5
# File 'lib/whiteprint/base.rb', line 3

def configs
  @configs
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/whiteprint/base.rb', line 3

def model
  @model
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/whiteprint/base.rb', line 3

def options
  @options
end

Class Method Details

.applicable?(_) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/whiteprint/base.rb', line 6

def applicable?(_)
  true
end

.load_pluginsObject



10
11
12
13
14
# File 'lib/whiteprint/base.rb', line 10

def load_plugins
  Whiteprint.config.plugins.each do |plugin|
    include Whiteprint.plugins[plugin]
  end
end

Instance Method Details

#changes?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# 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_treeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



62
63
64
65
66
67
68
69
# 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) ⇒ Object



26
27
28
29
# File 'lib/whiteprint/base.rb', line 26

def execute(&block)
  self.configs << block
  instance_eval(&block)
end

#explanation(index = 1, width = 100) ⇒ Object



31
32
33
34
35
36
37
38
# 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

#persisted_attributesObject



71
72
73
# File 'lib/whiteprint/base.rb', line 71

def persisted_attributes
  Whiteprint::Attributes.new
end

#set_model(model) ⇒ Object



75
76
77
# File 'lib/whiteprint/base.rb', line 75

def set_model(model)
  self.model = model
end

#table_exists?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/whiteprint/base.rb', line 83

def table_exists?
  model.table_exists?
end

#table_nameObject



79
80
81
# File 'lib/whiteprint/base.rb', line 79

def table_name
  model.table_name
end

#transformerObject



87
88
89
# File 'lib/whiteprint/base.rb', line 87

def transformer
  self.class
end