Skip to content

Commit

Permalink
Implement Scopes::Base#as_json.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingkingstudios committed Jan 19, 2024
1 parent 376387c commit f3c5962
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
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

0 comments on commit f3c5962

Please sign in to comment.