Skip to content

Commit

Permalink
Add object: keyword to import object methods
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Oct 16, 2024
1 parent a91db74 commit 2692c53
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
68 changes: 38 additions & 30 deletions lib/rbs/unit_test/with_aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,62 +43,62 @@ def with(*args, &block)
args.each(&block)
end

def with_int(value = 3, &block)
return WithEnum.new to_enum(__method__ || raise, value) unless block
def with_int(value = 3, object: false, &block)
return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block
yield value
yield ToInt.new(value)
yield object_it(ToInt.new(value), object)
end

def with_float(value = 0.1)
return WithEnum.new to_enum(__method__ || raise, value) unless block_given?
def with_float(value = 0.1, object: false)
return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block_given?
yield value
yield ToF.new(value)
yield object_it(ToF.new(value), object)
end

def with_string(value = '')
return WithEnum.new to_enum(__method__ || raise, value) unless block_given?
def with_string(value = '', object: false)
return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block_given?
yield value
yield ToStr.new(value)
yield object_it(ToStr.new(value), object)
end

def with_array(*elements)
return WithEnum.new to_enum(__method__ || raise, *elements) unless block_given?
def with_array(*elements, object: false)
return WithEnum.new to_enum(__method__ || raise, *elements, object: object) unless block_given?

yield _ = elements
yield ToArray.new(*elements)
yield object_it(ToArray.new(*elements), object)
end

def with_hash(hash = {})
return WithEnum.new to_enum(__method__ || raise, hash) unless block_given?
def with_hash(hash = {}, object: false)
return WithEnum.new to_enum(__method__ || raise, hash, object: object) unless block_given?

yield _ = hash
yield ToHash.new(hash)
yield object_it(ToHash.new(hash), object)
end

def with_io(io = $stdout)
return WithEnum.new to_enum(__method__ || raise, io) unless block_given?
def with_io(io = $stdout, object: false)
return WithEnum.new to_enum(__method__ || raise, io, object: object) unless block_given?
yield io
yield ToIO.new(io)
yield object_it(ToIO.new(io), object)
end

def with_path(path = "/tmp/foo.txt", &block)
return WithEnum.new to_enum(__method__ || raise, path) unless block
def with_path(path = "/tmp/foo.txt", object: false, &block)
return WithEnum.new to_enum(__method__ || raise, path, object: object) unless block

with_string(path, &block)
block.call ToPath.new(path)
block.call object_it(ToPath.new(path), object)
end

def with_encoding(encoding = Encoding::UTF_8, &block)
return WithEnum.new to_enum(__method__ || raise, encoding) unless block
def with_encoding(encoding = Encoding::UTF_8, object: false, &block)
return WithEnum.new to_enum(__method__ || raise, encoding, object: object) unless block

block.call encoding
with_string(encoding.to_s, &block)
with_string(encoding.to_s, object: object, &block)
end

def with_interned(value = :&, &block)
return WithEnum.new to_enum(__method__ || raise, value) unless block
def with_interned(value = :&, object: false, &block)
return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block

with_string(value.to_s, &block)
with_string(value.to_s, object: object, &block)
block.call value.to_sym
end

Expand All @@ -108,10 +108,10 @@ def with_bool(&block)
yield false
end

def with_boolish(&block)
return WithEnum.new to_enum(__method__ || raise) unless block
def with_boolish(object: false, &block)
return WithEnum.new to_enum(__method__ || raise, object: object) unless block
with_bool(&block)
[nil, 1, Object.new, BlankSlate.new, "hello, world!"].each(&block)
[nil, 1, Object.new, object_it(BlankSlate.new, object), "hello, world!"].each(&block)
end

alias with_untyped with_boolish
Expand All @@ -138,6 +138,14 @@ def lower.<=>(rhs) = :not_nil unless defined? lower.<=>
end
end
end

def object_it(value, make)
if make
value.__with_object_methods
else
value
end
end
end
end
end
42 changes: 22 additions & 20 deletions sig/unit_test/with_aliases.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -70,48 +70,48 @@ module RBS

# Yields `::int` objects
#
def with_int: (?Integer value) { (int) -> void } -> void
| (?Integer value) -> WithEnum[int]
def with_int: (?Integer value, ?object: bool) { (int) -> void } -> void
| (?Integer value, ?object: bool) -> WithEnum[int]

# Yields `::float` objects
#
def with_float: (?Float value) { (float) -> void } -> void
| (?Float value) -> WithEnum[float]
def with_float: (?Float value, ?object: bool) { (float) -> void } -> void
| (?Float value, ?object: bool) -> WithEnum[float]

# Yields `::string` objects
#
def with_string: (?String value) { (string) -> void } -> void
| (?String value) -> WithEnum[string]
def with_string: (?String value, ?object: bool) { (string) -> void } -> void
| (?String value, ?object: bool) -> WithEnum[string]

# Yields `::array` objects
#
def with_array: [T] (*T elements) { (array[T]) -> void } -> void
| [T] (*T elements) -> WithEnum[array[T]]
def with_array: [T] (*T elements, ?object: bool) { (array[T]) -> void } -> void
| [T] (*T elements, ?object: bool) -> WithEnum[array[T]]

# Yields `::hash` objects
#
def with_hash: [K, V] (?Hash[K, V] hash) { (hash[K, V]) -> void } -> void
| [K, V] (?Hash[K, V] hash) -> WithEnum[hash[K, V]]
def with_hash: [K, V] (?Hash[K, V] hash, ?object: bool) { (hash[K, V]) -> void } -> void
| [K, V] (?Hash[K, V] hash, ?object: bool) -> WithEnum[hash[K, V]]

# Yields `::io` objects
#
def with_io: (?untyped io) { (io) -> void } -> void
| (?untyped io) -> WithEnum[io]
def with_io: (?untyped io, ?object: bool) { (io) -> void } -> void
| (?untyped io, ?object: bool) -> WithEnum[io]

# Yields `::path` objects
#
def with_path: (?String path) { (path) -> void } -> void
| (?String path) -> WithEnum[path]
def with_path: (?String path, ?object: bool) { (path) -> void } -> void
| (?String path, ?object: bool) -> WithEnum[path]

# Yields `::encoding` objects
#
def with_encoding: (?untyped encoding) { (encoding) -> void } -> void
| (?untyped encoding) -> WithEnum[encoding]
def with_encoding: (?untyped encoding, ?object: bool) { (encoding) -> void } -> void
| (?untyped encoding, ?object: bool) -> WithEnum[encoding]

# Yields `::interned` objects
#
def with_interned: (?Symbol value) { (interned) -> void } -> void
| (?Symbol value) -> WithEnum[interned]
def with_interned: (?Symbol value, ?object: bool) { (interned) -> void } -> void
| (?Symbol value, ?object: bool) -> WithEnum[interned]

# Yields `::bool` objects
#
Expand All @@ -120,8 +120,8 @@ module RBS

# Yields `::boolish` objects
#
def with_boolish: () { (boolish) -> void } -> void
| () -> WithEnum[boolish]
def with_boolish: (?object: bool) { (boolish) -> void } -> void
| (?object: bool) -> WithEnum[boolish]

# Yields `::untyped` objects
#
Expand All @@ -131,6 +131,8 @@ module RBS
# Yields `::range` objects
#
def with_range: (WithEnum[untyped] start, WithEnum[untyped] stop, ?bool exclude_end) { (range[untyped]) -> void } -> void

private def object_it: [T < Convertibles::BlankSlate] (T value, bool make_it_object) -> T
end
end
end

0 comments on commit 2692c53

Please sign in to comment.