Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/Implement Scopes::Base#as_json. #68

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/cuprum/collections/rspec/contracts/scope_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ module ShouldBeAScopeContract
end
end

describe '#as_json' do
it { expect(subject).to respond_to(:as_json).with(0).arguments }

it { expect(subject.as_json).to be_a Hash }

it { expect(subject.as_json['type']).to be subject.type }
end

describe '#empty?' do
include_examples 'should define predicate', :empty?, -> { be_boolean }
end
Expand Down Expand Up @@ -181,6 +189,16 @@ module ShouldBeAContainerScopeContract
end
end

describe '#as_json' do
it { expect(subject.as_json['scopes']).to be == [] }

wrap_context 'with scopes' do
let(:expected) { subject.scopes.map(&:as_json) }

it { expect(subject.as_json['scopes']).to be == expected }
end
end

describe '#empty?' do
it { expect(subject.empty?).to be true }

Expand Down Expand Up @@ -389,6 +407,12 @@ module ShouldBeANullScopeContract
end
end

describe '#as_json' do
let(:expected) { { 'type' => subject.type } }

it { expect(subject.as_json).to be == expected }
end

describe '#call' do
shared_context 'with data' do
let(:data) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ def parse_criteria(*args, &block)
end
end

describe '#as_json' do
let(:expected) do
{
'criteria' => subject.criteria,
'type' => subject.type
}
end

it { expect(subject.as_json).to be == expected }

wrap_context 'with criteria' do
it { expect(subject.as_json).to be == expected }
end
end

describe '#call' do
next if abstract

Expand Down
45 changes: 45 additions & 0 deletions lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ module ShouldBeAConjunctionScopeContract

include_contract 'should compose scopes for conjunction'

describe '#as_json' do
let(:expected) do
{
'scopes' => subject.scopes.map(&:as_json),
'type' => subject.type
}
end

it { expect(subject.as_json).to be == expected }

wrap_context 'with scopes' do
it { expect(subject.as_json).to be == expected }
end
end

describe '#call' do
next if abstract

Expand Down Expand Up @@ -55,6 +70,21 @@ module ShouldBeADisjunctionScopeContract

include_contract 'should compose scopes for disjunction'

describe '#as_json' do
let(:expected) do
{
'scopes' => subject.scopes.map(&:as_json),
'type' => subject.type
}
end

it { expect(subject.as_json).to be == expected }

wrap_context 'with scopes' do
it { expect(subject.as_json).to be == expected }
end
end

describe '#call' do
next if abstract

Expand Down Expand Up @@ -83,6 +113,21 @@ module ShouldBeANegationScopeContract

include_contract 'should compose scopes for negation'

describe '#as_json' do
let(:expected) do
{
'scopes' => subject.scopes.map(&:as_json),
'type' => subject.type
}
end

it { expect(subject.as_json).to be == expected }

wrap_context 'with scopes' do
it { expect(subject.as_json).to be == expected }
end
end

describe '#call' do
next if abstract

Expand Down
6 changes: 6 additions & 0 deletions lib/cuprum/collections/scopes/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def ==(other)
other.type == type
end

# @return [Hash{String=>Object}] a JSON-compatible representation of the
# scope.
def as_json
{ 'type' => type }
end

# :nocov:

# @private
Expand Down
5 changes: 5 additions & 0 deletions lib/cuprum/collections/scopes/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def ==(other)
other.scopes == scopes
end

# (see Cuprum::Colletions::Scopes::Base#as_json)
def as_json
super().merge({ 'scopes' => scopes.map(&:as_json) })
end

# @private
def debug
message = "#{super} (#{scopes.count})"
Expand Down
5 changes: 5 additions & 0 deletions lib/cuprum/collections/scopes/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ def and(*args, &block)
end
alias where and

# (see Cuprum::Colletions::Scopes::Base#as_json)
def as_json
super().merge({ 'criteria' => criteria })
end

# @private
def debug
message = "#{super} (#{criteria.count})"
Expand Down
Loading