Skip to content

Commit

Permalink
Update #start_with? to match String#start_with signature
Browse files Browse the repository at this point in the history
Faraday recently started expecting a string-like #start_with? signature,
where you can pass multiple strings to test against and it returns true
if any of them passes. We were passing an RDF Value to Faraday since the
API was mostly consistent.

It seems like the intent is to have the same API as String#start_with?,
so this replicates it.
  • Loading branch information
tpendragon authored and gkellogg committed Jul 5, 2024
1 parent 5e32ea6 commit ca8db65
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rdf/model/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,20 @@ def validate!
alias_method :validate, :validate!

##
# Returns `true` if this Value starts with the given `string`.
# Returns `true` if this Value starts with any of the given strings.
#
# @example
# RDF::URI('http://example.org/').start_with?('http') #=> true
# RDF::Node('_:foo').start_with?('_:bar') #=> false
# RDF::Litera('Apple').start_with?('Orange') #=> false
# RDF::Litera('Apple').start_with?('Orange', 'Apple') #=> true
#
# @param [String, #to_s] string
# @param [Array<#to_s>] *args Any number of strings to check against.
# @return [Boolean] `true` or `false`
# @see String#start_with?
# @since 0.3.0
def start_with?(string)
to_s.start_with?(string.to_s)
def start_with?(*args)
to_s.start_with?(*args.map(&:to_s))
end
alias_method :starts_with?, :start_with?

Expand Down
1 change: 1 addition & 0 deletions spec/model_literal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def self.literals(*selector)
it "#start_with?" do
expect(RDF::Literal('foo')).to be_start_with('foo')
expect(RDF::Literal('bar')).not_to be_start_with('foo')
expect(RDF::Literal('foo')).to be_start_with('foo', 'nope')
end

describe "#==" do
Expand Down

0 comments on commit ca8db65

Please sign in to comment.