From f3c5962bee5ba3c984ae41bb913c5cf64cdf2ff5 Mon Sep 17 00:00:00 2001 From: Rob Smith Date: Fri, 19 Jan 2024 18:42:11 -0500 Subject: [PATCH] Implement Scopes::Base#as_json. --- .../rspec/contracts/scope_contracts.rb | 24 ++++++++++ .../contracts/scopes/criteria_contracts.rb | 15 +++++++ .../contracts/scopes/logical_contracts.rb | 45 +++++++++++++++++++ lib/cuprum/collections/scopes/base.rb | 6 +++ lib/cuprum/collections/scopes/container.rb | 5 +++ lib/cuprum/collections/scopes/criteria.rb | 5 +++ 6 files changed, 100 insertions(+) diff --git a/lib/cuprum/collections/rspec/contracts/scope_contracts.rb b/lib/cuprum/collections/rspec/contracts/scope_contracts.rb index 8678574..c0b3f79 100644 --- a/lib/cuprum/collections/rspec/contracts/scope_contracts.rb +++ b/lib/cuprum/collections/rspec/contracts/scope_contracts.rb @@ -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 @@ -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 } @@ -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 diff --git a/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb b/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb index 8b9ccab..ea26181 100644 --- a/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb +++ b/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb @@ -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 diff --git a/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb b/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb index 73c46ae..a3edc75 100644 --- a/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb +++ b/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/cuprum/collections/scopes/base.rb b/lib/cuprum/collections/scopes/base.rb index 0f68d73..e5f450e 100644 --- a/lib/cuprum/collections/scopes/base.rb +++ b/lib/cuprum/collections/scopes/base.rb @@ -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 diff --git a/lib/cuprum/collections/scopes/container.rb b/lib/cuprum/collections/scopes/container.rb index 378b753..4b87a77 100644 --- a/lib/cuprum/collections/scopes/container.rb +++ b/lib/cuprum/collections/scopes/container.rb @@ -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})" diff --git a/lib/cuprum/collections/scopes/criteria.rb b/lib/cuprum/collections/scopes/criteria.rb index 8a3af1c..984735c 100644 --- a/lib/cuprum/collections/scopes/criteria.rb +++ b/lib/cuprum/collections/scopes/criteria.rb @@ -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})"