Skip to content

Commit

Permalink
Merge pull request #76 from ksss/depth-hash
Browse files Browse the repository at this point in the history
Support Hash
  • Loading branch information
ksss authored Jul 3, 2024
2 parents 66db0d9 + 86bc1cc commit 892b4b3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion known_sig/orthoses/create_file_by_name.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Orthoses
class CreateFileByName
@loader: Orthoses::_Call
def initialize: (Orthoses::_Call loader, to: String, ?header: String?, ?depth: Integer?, ?rmtree: boolish) -> void
def initialize: (Orthoses::_Call loader, to: String, ?header: String?, ?depth: Integer | Hash[String, Integer] | nil, ?rmtree: boolish) -> void
def call: () -> Orthoses::store
end
end
27 changes: 25 additions & 2 deletions lib/orthoses/create_file_by_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require 'orthoses/outputable'

module Orthoses
# @param to [String, nil]
# @param header [String, nil]
# @param if [Proc, nil]
# @param depth [Integer, Hash{String => Integer}, nil]
# @param rmtree [Boolean]
class CreateFileByName
prepend Outputable

Expand Down Expand Up @@ -43,8 +48,7 @@ def call
@if.nil? || @if.call(name, content)
end
grouped = store.group_by do |name, _|
splitted = name.to_s.split('::')
(@depth ? splitted[0, @depth] : splitted).join('::')
extract(name)
end

grouped.each do |group_name, group|
Expand All @@ -64,5 +68,24 @@ def call
end
end
end

def extract(name)
case @depth
when nil
name.to_s
when Integer
name.split('::')[0, @depth].join('::')
when Hash
found_key, found_index = @depth.find do |n, _|
name.start_with?(n)
end
case found_index
when nil
name.to_s
else
name.split('::')[0, found_index].join('::')
end
end
end
end
end
78 changes: 76 additions & 2 deletions lib/orthoses/create_file_by_name_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# frozen_string_literal: true

begin
require 'test_helper'
rescue LoadError
end

module CreateFileByNameTest
module Foo
end
Expand Down Expand Up @@ -37,7 +42,7 @@ def test_create_file_by_name(t)
end
},
to: "tmp",
header: "# header"
header: "# header",
).call

foo = Pathname("tmp/create_file_by_name_test/foo.rbs")
Expand All @@ -48,7 +53,7 @@ def test_create_file_by_name(t)

[foo, bar, baz, qux, outputable_test].each do |file|
unless file.exist?
t.error("file not created `#{file}`")
t.fatal("file not created `#{file}`")
end
end

Expand Down Expand Up @@ -106,6 +111,75 @@ module OutputableTest : BasicObject
Pathname("tmp").rmtree rescue nil
end

def test_depth(t)
Orthoses::CreateFileByName.new(
-> {
Orthoses::Utils.new_store.tap do |store|
store["A"].header = "class A"
store["A::A"].header = "class A::A"
store["A::A::A"].header = "class A::A::A"
store["B"].header = "class B"
store["B::B"].header = "class B::B"
store["C"].header = "class C"
end
},
to: "tmp",
depth: {
"A" => 2,
},
).call

a = Pathname("tmp/a.rbs")
aa = Pathname("tmp/a/a.rbs")
b = Pathname("tmp/b.rbs")
bb = Pathname("tmp/b/b.rbs")
c = Pathname("tmp/c.rbs")

[a, aa, b, bb, c].each do |file|
unless file.exist?
t.fatal("file not created `#{file}`")
end
end

a_expect = <<~RBS
class A
end
RBS
aa_expect = <<~RBS
class A::A
end
class A::A::A
end
RBS
b_expect = <<~RBS
class B
end
RBS
bb_expect = <<~RBS
class B::B
end
RBS
c_expect = <<~RBS
class C
end
RBS

[
[a.read, a_expect],
[aa.read, aa_expect],
[b.read, b_expect],
[bb.read, bb_expect],
[c.read, c_expect],
].each do |actual, expect|
unless actual == expect
t.error("[CreateFileByName] expect=\n#{expect.inspect}\n but got actual=\n#{actual.inspect}\n")
end
end
ensure
Pathname("tmp").rmtree rescue nil
end

def test_rmtree(t)
Pathname("tmp").mkdir
FileUtils.touch("tmp/a.rbs")
Expand Down
8 changes: 7 additions & 1 deletion sig/orthoses/create_file_by_name.rbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# THIS IS GENERATED CODE from `$ rake sig`

# @param to [String, nil]
# @param header [String, nil]
# @param if [Proc, nil]
# @param depth [Integer, Hash{String => Integer}, nil]
# @param rmtree [Boolean]
class Orthoses::CreateFileByName
@loader: Orthoses::_Call
@to: untyped
@header: untyped
@depth: untyped
@rmtree: untyped
@if: untyped
def initialize: (Orthoses::_Call loader, to: String, ?header: String?, ?depth: Integer?, ?rmtree: boolish) -> void
def initialize: (Orthoses::_Call loader, to: String, ?header: String?, ?depth: Integer | Hash[String, Integer] | nil, ?rmtree: boolish) -> void
def call: () -> Orthoses::store
def extract: (untyped name) -> untyped
end

0 comments on commit 892b4b3

Please sign in to comment.