class PGTrunk::Operation::RubyBuilder

@private Build ruby snippet

Constants

HEREDOC

Pattern to split lines by heredocs

Public Class Methods

new(name, shortage: nil) click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 7
        def initialize(name, shortage: nil)
  @args = []
  @lines = []
  @name = name&.to_s
  @opts = []
  @shortage = shortage
end

Public Instance Methods

build() click to toggle source

Build the snippet @return [String]

# File lib/pg_trunk/core/operation/ruby_builder.rb, line 31
def build
  [header, *block].join(" ")
end
ruby_line(meth, *args, **opts) click to toggle source

Add line into a block

# File lib/pg_trunk/core/operation/ruby_builder.rb, line 22
def ruby_line(meth, *args, **opts)
  return if meth.blank?
  return if args.first.nil?

  @lines << build_line(meth, *args, **opts)
end
ruby_param(*args, **opts) click to toggle source

Add parameters to the method call

# File lib/pg_trunk/core/operation/ruby_builder.rb, line 16
def ruby_param(*args, **opts)
  @args = [*@args, *params(*args)]
  @opts = [*@opts, *params(**opts)]
end

Private Instance Methods

block() click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 108
def block
  [nil, *@lines, "end"].join("\n") if @lines.any?
end
build_line(meth, *args, **opts) { |builder| ... } click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 40
def build_line(meth, *args, **opts)
  method_name = [shortage, meth].join(".")
  method_params = params(*args, **opts)
  line = [method_name, *method_params].join(" ")
  return single_line(line).indent(2) unless block_given?

  builder = self.class.new(line, shortage: "f")
  yield(builder)
  builder.build.indent(2)
end
format(value) click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 65
def format(value)
  case value
  when Hash   then value
  when String then format_text(value)
  when Array  then format_list(value)
  else value.inspect
  end
end
format_list(list) click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 87
def format_list(list)
  case list.map(&:class).uniq
  when [::String] then "%w[#{list.join(' ')}]"
  when [::Symbol] then "%i[#{list.join(' ')}]"
  else list
  end
end
format_text(text) click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 74
def format_text(text)
  text = text.chomp
  # prevent quoting interpolations and heredocs
  return text if text[/^<<~|^%[A-Za-z][(]/]

  long_text = text.size > 50 || text.include?("\n")
  return "<<~'Q'.chomp\n#{text.indent(2)}\nQ" if long_text && text["\\"]
  return "<<~Q.chomp\n#{text.indent(2)}\nQ" if long_text
  return "%q(#{text})" if /\\|"/.match?(text)

  text.inspect
end
header() click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 101
def header
  method_params = [*@args, *@opts].join(", ").presence
  line = [@name, *method_params].join(" ")
  line << " do |#{shortage}|" if @lines.any?
  single_line(line)
end
params(*values, **options) click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 95
def params(*values, **options)
  vals = values.map { |val| format(val) }
  opts = options.compact.map { |key, val| "#{key}: #{format(val)}" }
  [*vals, *opts].join(", ").presence
end
shortage() click to toggle source
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 61
def shortage
  @shortage ||= @name.split("_").last.first
end
single_line(text) click to toggle source

Finalize line containing a heredoc args

"body <<~'SQL'.chomp\n  foo\nSQL, from: <<~'SQL'.chomp\n  bar\nSQL"
"body <<~'SQL'.chomp, from: <<~'SQL'.chomp\n  foo\nSQL\n  bar\nSQL"
# File lib/pg_trunk/core/operation/ruby_builder.rb, line 54
def single_line(text)
  parts = text.partition(HEREDOC)
  (
    parts.map { |p| p[/^.+/] } + parts.map { |p| p[/\n(\n|.)*$/] }
  ).compact.join
end