-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement Scopes::Null. - Implement Basic::Scopes::NullScope.
- Loading branch information
1 parent
077e8c8
commit eabd88c
Showing
9 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/basic/scopes' | ||
require 'cuprum/collections/basic/scopes/base' | ||
require 'cuprum/collections/scopes/null' | ||
|
||
module Cuprum::Collections::Basic::Scopes | ||
# Scope for returning unfiltered data. | ||
class NullScope < Cuprum::Collections::Basic::Scopes::Base | ||
include Cuprum::Collections::Scopes::Null | ||
|
||
# Filters the provided data. | ||
def call(data:) | ||
raise ArgumentError, 'data must be an Array' unless data.is_a?(Array) | ||
|
||
data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/scopes' | ||
|
||
module Cuprum::Collections::Scopes | ||
# Functionality for implementing a null scope. | ||
module Null | ||
# @override and(hash = nil, &block) | ||
# Parses the hash or block and returns the parsed scope. | ||
# | ||
# @see Cuprum::Collections::Scopes::Criteria::Parser#parse. | ||
# | ||
# @override and(scope) | ||
# Returns the given scope. | ||
def and(...) | ||
builder.build(...) | ||
end | ||
alias where and | ||
|
||
# @override or(hash = nil, &block) | ||
# Parses the hash or block and returns the parsed scope. | ||
# | ||
# @see Cuprum::Collections::Scopes::Criteria::Parser#parse. | ||
# | ||
# @override or(scope) | ||
# Returns the given scope. | ||
def or(...) | ||
builder.build(...) | ||
end | ||
|
||
# @override not(hash = nil, &block) | ||
# Parses and inverts the hash or block and returns the inverted scope. | ||
# | ||
# @see Cuprum::Collections::Scopes::Criteria::Parser#parse. | ||
# | ||
# @override not(scope) | ||
# Inverts and returns the given scope. | ||
def not(...) | ||
scope = builder.build(...) | ||
|
||
builder.build_negation_scope(scopes: [scope], safe: false) | ||
end | ||
|
||
# (see Cuprum::Collections::Scopes::Base#type) | ||
def type | ||
:null | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/scopes' | ||
require 'cuprum/collections/scopes/base' | ||
require 'cuprum/collections/scopes/null' | ||
|
||
module Cuprum::Collections::Scopes | ||
# Generic scope class for defining collection-independent null scopes. | ||
class NullScope < Cuprum::Collections::Scopes::Base | ||
include Cuprum::Collections::Scopes::Null | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/rspec/contracts/scope_contracts' | ||
require 'cuprum/collections/basic/scopes/null_scope' | ||
|
||
RSpec.describe Cuprum::Collections::Basic::Scopes::NullScope do | ||
include Cuprum::Collections::RSpec::Contracts::ScopeContracts | ||
|
||
subject(:scope) { described_class.new } | ||
|
||
let(:data) { [] } | ||
|
||
def filtered_data | ||
scope.call(data: data) | ||
end | ||
|
||
describe '.new' do | ||
it 'should define the constructor' do | ||
expect(described_class) | ||
.to be_constructible | ||
.with(0).arguments | ||
.and_any_keywords | ||
end | ||
end | ||
|
||
include_contract 'should be a null scope' | ||
|
||
describe '#match' do | ||
let(:item) { {} } | ||
|
||
it 'should define the method' do | ||
expect(scope).to respond_to(:match?).with(0).arguments.and_keywords(:item) | ||
end | ||
|
||
it 'should alias the method' do | ||
expect(scope).to have_aliased_method(:match?).as(:matches?) | ||
end | ||
|
||
describe 'with nil' do | ||
let(:error_message) { 'item must be a Hash' } | ||
|
||
it 'should raise an exception' do | ||
expect { scope.match?(item: nil) } | ||
.to raise_error ArgumentError, error_message | ||
end | ||
end | ||
|
||
describe 'with an Object' do | ||
let(:error_message) { 'item must be a Hash' } | ||
|
||
it 'should raise an exception' do | ||
expect { scope.match?(item: Object.new.freeze) } | ||
.to raise_error ArgumentError, error_message | ||
end | ||
end | ||
|
||
describe 'with an item' do | ||
let(:item) do | ||
Cuprum::Collections::RSpec::Fixtures::BOOKS_FIXTURES | ||
.find { |book| book['title'] == 'The Silmarillion' } | ||
end | ||
|
||
it { expect(scope.match?(item: item)).to be true } | ||
end | ||
end | ||
end |
Oops, something went wrong.