module PGTrunk::Operation::RubyHelpers
@private Helpers to build ruby snippet from the operation definition
Public Instance Methods
dump(stream)
click to toggle source
@param [IO] stream
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 95 def dump(stream) to_ruby&.rstrip&.lines&.each { |line| stream.print(line.indent(2)) } end
from_ruby(*args, &block)
click to toggle source
Build the operation from arguments sent to Ruby method
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 56 def from_ruby(*args, &block) options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {} params = ruby_params.zip(args).to_h new(**params, **options, &block) end
inherited(klass)
click to toggle source
Calls superclass method
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 64 def inherited(klass) # Use params from a parent class by default (can be overloaded). klass.instance_variable_set(:@ruby_params, ruby_params) klass.instance_variable_set(:@ruby_snippet, ruby_snippet) super end
ruby_iname()
click to toggle source
The name of the inverted builder @return [Symbol]
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 18 def ruby_iname "invert_#{ruby_name}".to_sym end
ruby_name()
click to toggle source
The name of the builder @return [Symbol]
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 12 def ruby_name @ruby_name ||= name.split("::").last.underscore.to_sym end
ruby_params(*params)
click to toggle source
Get/set positional params of the ruby method
@example Provide a method ‘add_check_constraint(table = nil, **opts)`
class AddCheckConstraint < PGTrunk::Operation ruby_params :table # ... end
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 30 def ruby_params(*params) @ruby_params = params.compact.map(&:to_sym) if params.any? @ruby_params ||= [] end
ruby_snippet(&block)
click to toggle source
Gets or sets the block building a ruby snippet
@yieldparam [PGTrunk::Operation::RubyBuilder]
@example
ruby_snippet do |s| s.ruby_param(comment: comment) if comment.present? values.each { |v| s.ruby_line(:value, v) } end # will build something like do_something "foo.bar", comment: "comment" do |s| s.value "baz" s.value "qux" end
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 51 def ruby_snippet(&block) @ruby_snippet ||= block end
to_a()
click to toggle source
List of attributes assigned that are assigned via Ruby method parameters.
We can use it to announce the operation to $stdout like ‘create_foreign_key(“users”, “roles”)`.
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 86 def to_a to_h.values_at(*self.class.ruby_params) end
to_opts()
click to toggle source
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 90 def to_opts to_h.except(*self.class.ruby_params) end
to_ruby()
click to toggle source
@private Ruby snippet to dump the creator @return [String]
# File lib/pg_trunk/core/operation/ruby_helpers.rb, line 75 def to_ruby builder = RubyBuilder.new(self.class.ruby_name) instance_exec(builder, &self.class.ruby_snippet) builder.build.rstrip end