Class: Orthoses::CreateFileByName

Inherits:
Object
  • Object
show all
Includes:
Outputable
Defined in:
lib/orthoses/create_file_by_name.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(loader, base_dir: nil, to: nil, header: nil, if: nil, depth: nil, rmtree: false) ⇒ CreateFileByName

Returns a new instance of CreateFileByName.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/orthoses/create_file_by_name.rb', line 14

def initialize(loader, base_dir: nil, to: nil, header: nil, if: nil, depth: nil, rmtree: false)
  unless base_dir.nil?
    warn "[Orthoses::CreateFileByName] base_dir: option is deprecated. Please use to: option instead."
  end

  to = to || base_dir
  unless to
    raise ArgumentError, "should set to: option"
  end

  relative_path_from_pwd = Pathname(to).expand_path.relative_path_from(Pathname.pwd).to_s
  unless relative_path_from_pwd == "." || !relative_path_from_pwd.match?(%r{\A[/\.]})
    raise ArgumentError, "to=\"#{to}\" should be under current dir=\"#{Pathname.pwd}\"."
  end

  @loader = loader
  @to = to
  @header = header
  @depth = depth
  @rmtree = rmtree
  @if = binding.local_variable_get(:if)
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/orthoses/create_file_by_name.rb', line 39

def call
  if @rmtree
    Orthoses.logger.debug("Remove dir #{@to} since `rmtree: true`")
    Pathname(@to).rmtree rescue nil
  end

  store = @loader.call

  store.select! do |name, content|
    @if.nil? || @if.call(name, content)
  end
  grouped = store.group_by do |name, _|
    extract(name)
  end

  grouped.each do |group_name, group|
    file_path = Pathname("#{@to}/#{group_name.split('::').map(&:underscore).join('/')}.rbs")
    file_path.dirname.mkpath
    file_path.open('w+') do |out|
      if @resolve_type_names
        out.puts "# resolve-type-names: false"
        out.puts
      end
      if @header
        out.puts @header
        out.puts
      end
      group.sort!
      contents = group.map do |(name, content)|
        content.to_rbs
      end.join("\n")
      out.puts contents
      Orthoses.logger.info("Generate file to #{file_path.to_s}")
    end
  end
end

#extract(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/orthoses/create_file_by_name.rb', line 76

def extract(name)
  case @depth
  when nil
    name.to_s
  when Integer
    name.split('::')[0, @depth].join('::')
  when Hash
    _, found_index = @depth.find do |n, _|
      n == '*' || name.start_with?(n)
    end
    case found_index
    when nil
      name.to_s
    else
      name.split('::')[0, found_index].join('::')
    end
  end
end