Skip to content

Commit

Permalink
feat: Pass through arguments to predicate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joelmoss committed Apr 18, 2024
1 parent 8cd3494 commit d569181
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fixtures/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _!(append = nil)
"Lesley Moss#{append}"
end

def _?
def _?(*, **)
true
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/spaced.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def #{name}!(...); #{name}.call(...); end # def user!(...); user.call(...); end

if methods.include?(:_?)
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}?; #{name}._?; end # def user?; user._?; end
def #{name}?(...); #{name}._?(...); end # def user?; user._?; end
RUBY
elsif methods.include?(:"#{name}?")
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}?; #{name}.#{name}?; end # def user?; user.user?; end
def #{name}?(...); #{name}.#{name}?(...); end # def user?; user.user?; end
RUBY
elsif methods.include?(:predicate) # DEPRECATED
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}?; #{name}.predicate; end # def user?; user.predicate; end
def #{name}?(...); #{name}.predicate(...); end # def user?; user.predicate; end
RUBY
else
define_method :"#{name}?" do
Expand Down
4 changes: 4 additions & 0 deletions test/spaced/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@
it "aliases to #_?" do
expect(user.mother?).to be == true
end

it "supports arguments" do
expect(user.mother?(:foo, bar: 1)).to be == true
end
end
end

0 comments on commit d569181

Please sign in to comment.