Skip to content

Commit

Permalink
Merge pull request #868 from knu/scopes_and_array
Browse files Browse the repository at this point in the history
Make Scopes#+ and #& work against a non-Scopes object
  • Loading branch information
nbulaj authored Jan 29, 2018
2 parents 08a5476 + f239d14 commit 60bfed6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ User-visible changes worth mentioning.

## master

- [#868] `Scopes#&` and `Scopes#+` now take an array or any other enumerable
object.
- [#1019] Remove translation not in use: `invalid_resource_owner`.
- Use Ruby 2 hash style syntax (min required Ruby version = 2.1)
- [#948] Make Scopes.<=> work with any "other" value.
Expand Down
20 changes: 13 additions & 7 deletions lib/doorkeeper/oauth/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ def has_scopes?(scopes)
end

def +(other)
if other.is_a? Scopes
self.class.from_array(all + other.all)
else
super(other)
end
self.class.from_array(all + to_array(other))
end

def <=>(other)
Expand All @@ -61,8 +57,18 @@ def <=>(other)
end

def &(other)
other_array = other.present? ? other.all : []
self.class.from_array(all & other_array)
self.class.from_array(all & to_array(other))
end

private

def to_array(other)
case other
when Scopes
other.all
else
other.to_a
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/oauth/scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,36 @@ module Doorkeeper::OAuth
expect(origin.to_s).to eq('public')
end

it 'can add an array to a scope object' do
scopes = Scopes.from_string('public') + ['admin']
expect(scopes.all).to eq(%w(public admin))
end

it 'raises an error if cannot handle addition' do
expect do
Scopes.from_string('public') + 'admin'
end.to raise_error(NoMethodError)
end
end

describe '#&' do
it 'can get intersection with another scope object' do
scopes = Scopes.from_string('public admin') & Scopes.from_string('write admin')
expect(scopes.all).to eq(%w(admin))
end

it 'does not change the existing object' do
origin = Scopes.from_string('public admin')
origin & Scopes.from_string('write admin')
expect(origin.to_s).to eq('public admin')
end

it 'can get intersection with an array' do
scopes = Scopes.from_string('public admin') & %w(write admin)
expect(scopes.all).to eq(%w(admin))
end
end

describe '#==' do
it 'is equal to another set of scopes' do
expect(Scopes.from_string('public')).to eq(Scopes.from_string('public'))
Expand Down

0 comments on commit 60bfed6

Please sign in to comment.