module SheepAst::LetRedirect

Let included module

Public Instance Methods

redirect(pair, datastore, key = nil, range = 1..-2, **options) click to toggle source
# File lib/sheep_ast/action/let_redirect.rb, line 52
def redirect(pair, datastore, key = nil, range = 1..-2, **options)
  chunk = nil
  line_matched = options[:redirect_line_matched]
  line_from_to = T.let(options[:redirect_line_from_to], T.nilable(Range))
  line_from = line_from_to&.first
  line_to = line_from_to&.last
  data = pair[:_data]

  if line_matched
    chunk = line_matched(data)
  elsif line_from_to
    key = :_1 if key.nil?
    chunk = line_from_to(data, key, line_from, line_to, nil)
  else
    chunk = line_enclosed(T.must(key), pair, range)
  end

  ldebug? and ldebug "received expr = #{chunk.inspect}, "\
    "pair = #{pair.inspect}, key = #{key.inspect}", :blue
  ldebug? and ldebug "options = #{options.inspect}", :blue
  ns_t = ns_get(pair, options[:namespace])

  if options[:dry_run]
    format_dump {
      ldump "To be redirect : #{chunk.inspect}"
      ldump "Namespace : #{ns_t}"
    }
    return T.unsafe(self).ret(**options)
  end

  if options[:debug]
    format_dump {
      ldump "To be redirect : #{chunk.inspect}"
      ldump "Namespace : #{ns_t}"
    }
  end

  ldebug? and ldebug "To be redirect : #{chunk.inspect}"

  save_req = SaveRequest.new(
    chunk: chunk,
    ast_include: options[:ast_include],
    ast_exclude: options[:ast_exclude],
    namespace: ns_t
  )

  @data.save_request = save_req
  return T.unsafe(self).ret(**options)
end