Class: RubyCrystalCodemod::DotFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_crystal_codemod/dot_file.rb

Instance Method Summary collapse

Constructor Details

#initializeDotFile

Returns a new instance of DotFile.



4
5
6
# File 'lib/ruby_crystal_codemod/dot_file.rb', line 4

def initialize
  @cache = {}
end

Instance Method Details

#find_in(dir) ⇒ Object



15
16
17
18
19
# File 'lib/ruby_crystal_codemod/dot_file.rb', line 15

def find_in(dir)
  @cache.fetch(dir) do
    @cache[dir] = internal_find_in(dir)
  end
end

#get_config_in(dir) ⇒ Object



8
9
10
11
12
13
# File 'lib/ruby_crystal_codemod/dot_file.rb', line 8

def get_config_in(dir)
  dot_ruby_crystal_codemod = find_in(dir)
  if dot_ruby_crystal_codemod
    return parse(dot_ruby_crystal_codemod)
  end
end

#internal_find_in(dir) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_crystal_codemod/dot_file.rb', line 40

def internal_find_in(dir)
  dir = File.expand_path(dir)
  file = File.join(dir, ".ruby_crystal_codemod")
  if File.exist?(file)
    return File.read(file)
  end

  parent_dir = File.dirname(dir)
  return if parent_dir == dir

  find_in(parent_dir)
end

#parse(file_contents) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_crystal_codemod/dot_file.rb', line 21

def parse(file_contents)
  file_contents.lines
               .map { |s| s.strip.split(/\s+/, 2) }
               .each_with_object({}) do |(name, value), acc|
    value ||= ""
    if value.start_with?(":")
      value = value[1..-1].to_sym
    elsif value == "true"
      value = true
    elsif value == "false"
      value = false
    else
      $stderr.puts "Unknown config value=#{value.inspect} for #{name.inspect}"
      next
    end
    acc[name.to_sym] = value
  end
end