module SheepAst::LetHelper

Let included module. Utility functions to be used inside of Let

@api private

rubocop:disable all

Public Class Methods

new() click to toggle source
Calls superclass method SheepAst::Log::new
# File lib/sheep_ast/action/let_helper.rb, line 23
def initialize
  super()
end

Public Instance Methods

ctime_get() click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 102
def ctime_get; end
data_shaping(chunk, **options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 203
def data_shaping(chunk, **options)
  if options[:raw]
    [chunk]
  else
    chunk.slice_after("\n").to_a
  end
end
find_file(dirs, relative_path) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 43
def find_file(dirs, relative_path)
  return nil if relative_path.nil?
  return relative_path if dirs.nil?

  found_paths = []
  dirs.each do |base|
    test_path = "#{base}/#{relative_path}"
    if File.exist?(test_path)
      ldebug? and ldebug "file exist: #{test_path}"
      found_paths << File.expand_path(test_path)
    end
  end

  if found_paths.count > 1
    lfatal "Duplicated include file has been found. #{found_paths.inspect}"
    application_error
  end

  return found_paths.first
end
format_dump() { || ... } click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 212
def format_dump
  ldump ''
  ldump '--- show ---'
  yield
  ldump '--- end  ---'
  ldump ''
end
get_first_match(data) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 27
def get_first_match(data)
  missing_impl
end
get_last_match(data) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 31
def get_last_match(data)
  missing_impl
end
get_match(data, num) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 35
def get_match(data, num)
  missing_impl
end
line_enclosed(key, pair, range, **options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 161
def line_enclosed(key, pair, range, **options)
  chunk = pair[key]
  application_error 'specified key did not hit' if chunk.nil?

  chunk = T.must(chunk)[range]
  application_error 'cannot redirect exp for no Array' unless chunk.instance_of?(Array)

  chunk = T.unsafe(self).data_shaping(chunk, **options)
  return chunk
end
line_from_to(data, key, line_from, line_to, trim = nil) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 134
def line_from_to(data, key, line_from, line_to, trim = nil)
  baseline_match = get_match(data, key)
  baseline = baseline_match.start_line
  start_line = baseline + line_from
  end_line = baseline + line_to
  if start_line < 0 || end_line > data.file_info&.max_line
    lfatal "start_line = #{start_line}, end_line = #{end_line}, max_line = #{data.file_info&.max_line}"
    application_error 'start_line < 0 or end_line > max_line'
  end
  ldebug? and ldebug "redirecting whole line start from #{start_line.inspect} to #{end_line.inspect}"
  range = start_line..end_line
  return data.file_info&.tokenized&.[](range)
end
line_matched(data) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 109
def line_matched(data)
  start_match = get_first_match(data)
  end_match = get_last_match(data)
  start_line = start_match.start_line
  end_line = end_match.end_line
  ldebug? and ldebug "redirecting whole line start from #{start_line.inspect} to #{end_line.inspect}"
  range = start_line..end_line
  return data.file_info&.tokenized&.[](range)
end
namespace_separator(**options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 243
def namespace_separator(**options)
  namespace_sep = options[:namespace_separator]
  namespace_sep = '::' if namespace_sep.nil?
  return namespace_sep
end
namespace_separator_file(**options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 249
def namespace_separator_file(**options)
  namespace_sep = options[:namespace_separator_file]
  namespace_sep = '::' if namespace_sep.nil?
  return namespace_sep
end
ns_get(pair, name) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 179
def ns_get(pair, name)
  return nil if name.nil?

  if name.instance_of? Symbol
    ns_t = pair[T.cast(name, Symbol)]
    if ns_t.nil?
      lfatal "namespace symbol cannot be found in the given data => #{pair.inspect}"
      application_error
    end
  end

  ldebug? and ldebug "namespace is #{ns_t.inspect}", :blue
  return ns_t
end
ret(**options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 255
def ret(**options)
  ret = options[:break]
end
update_file(file, res, **options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 71
def update_file(file, res, **options)
  if File.exist?(file)
    ftime = File.ctime(file)
    test = ctime_get <=> ftime
    case test
    when 1
      ldebug? and ldebug "#{file} is created before application launch. Delete it first!"
      File.delete(file)
    when -1
      # lprint "#{file} is created after factory created. Nothing to do."
    else
      lfatal "Unexpected timestamp info. #{ctime_get}, "\
        "file = #{ftime}, test = #{test.inspect}"
      application_error
    end
  end

  mode = options[:mode]
  perm = options[:perm]
  mode = 'a' if mode.nil?
  if perm.nil?
    File.open(file, mode) { |f|
      f.write(res)
    }
  else
    File.open(file, mode, perm) { |f|
      f.write(res)
    }
  end
end
w_or_wo_ns(pair, **options) click to toggle source
# File lib/sheep_ast/action/let_helper.rb, line 226
def w_or_wo_ns(pair, **options)
  t_ns = ''
  namespace_separator = ''
  if options[:namespace_key] || options[:namespace_value] || options[:namespace]
    namespace_separator = T.unsafe(self).namespace_separator(**options)
    namespace = pair[:_namespace]
    namespace.reverse_each do |elem|
      t_ns = "#{elem}#{namespace_separator}#{t_ns}"
    end
  end
  ns = t_ns.dup
  (1..namespace_separator.length).each do
    ns.chop!
  end
  return ns
end