-
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 generic Cuprum::Collections::Scope.
- Loading branch information
1 parent
4ecca86
commit 37e33ae
Showing
8 changed files
with
104 additions
and
11 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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections' | ||
require 'cuprum/collections/scopes/criteria_scope' | ||
|
||
module Cuprum::Collections | ||
# Generic scope class for defining collection-independent criteria scopes. | ||
class Scope < Cuprum::Collections::Scopes::CriteriaScope | ||
# @override build(value = nil, &block) | ||
# (see Cuprum::Collections::Scopes::Criteria::ClassMethods.build) | ||
def self.build(...) | ||
new(...) | ||
end | ||
|
||
# @override initialize(value = nil, &block) | ||
# @param value [Hash, nil] the keys and values to parse. | ||
# | ||
# @return [Array] the generated criteria. | ||
# | ||
# @yield the query block. | ||
# | ||
# @yieldreturn [Hash] a Hash with String keys. | ||
def initialize(...) | ||
criteria = self.class.parse(...) | ||
|
||
super(criteria: criteria) | ||
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/scopes' | ||
require 'cuprum/collections/scopes/base' | ||
require 'cuprum/collections/scopes/criteria' | ||
|
||
module Cuprum::Collections::Scopes | ||
class CriteriaScope < Cuprum::Collections::Scopes::Base | ||
include Cuprum::Collections::Scopes::Criteria | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/scope' | ||
require 'cuprum/collections/rspec/contracts/scopes/criteria_contracts' | ||
|
||
RSpec.describe Cuprum::Collections::Scope do | ||
include Cuprum::Collections::RSpec::Contracts::Scopes::CriteriaContracts | ||
|
||
subject(:scope) do | ||
described_class.new(*constructor_args, &constructor_block) | ||
end | ||
|
||
let(:constructor_args) do | ||
criteria | ||
.to_h { |(attribute, _, value)| [attribute, value] } | ||
.then { |hsh| [hsh] } | ||
end | ||
let(:constructor_block) { nil } | ||
|
||
describe '.new' do | ||
def parse_criteria(...) | ||
described_class.new(...).criteria | ||
end | ||
|
||
it 'should define the constructor' do | ||
expect(described_class) | ||
.to be_constructible | ||
.with(0..1).arguments | ||
.and_a_block | ||
end | ||
|
||
include_contract 'should parse criteria' | ||
end | ||
|
||
include_contract 'should be a criteria scope', | ||
abstract: true, | ||
constructor: false | ||
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cuprum/collections/scopes/criteria_scope' | ||
require 'cuprum/collections/rspec/contracts/scopes/criteria_contracts' | ||
|
||
RSpec.describe Cuprum::Collections::Scopes::CriteriaScope do | ||
include Cuprum::Collections::RSpec::Contracts::Scopes::CriteriaContracts | ||
|
||
subject(:scope) { described_class.new(criteria: criteria) } | ||
|
||
let(:criteria) { [] } | ||
|
||
include_contract 'should be a criteria scope', abstract: true | ||
end |