Skip to content

Commit

Permalink
Refactor logical scopes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingkingstudios committed Jan 18, 2024
1 parent 65b7719 commit 077e8c8
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 240 deletions.
11 changes: 2 additions & 9 deletions lib/cuprum/collections/basic/scopes/conjunction_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

require 'cuprum/collections/basic/scopes'
require 'cuprum/collections/basic/scopes/base'
require 'cuprum/collections/scopes/composition/conjunction'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/conjunction'

module Cuprum::Collections::Basic::Scopes
# Scope for filtering data matching all of the given scopes.
class ConjunctionScope < Cuprum::Collections::Basic::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Conjunction
include Cuprum::Collections::Scopes::Conjunction

# Returns true if the provided item matches all of the configured scopes.
def match?(item:)
Expand All @@ -18,10 +16,5 @@ def match?(item:)
scopes.all? { |scope| scope.match?(item: item) }
end
alias matches? match?

# (see Cuprum::Collections::Scopes::Base#type)
def type
:conjunction
end
end
end
2 changes: 0 additions & 2 deletions lib/cuprum/collections/basic/scopes/criteria_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
require 'cuprum/collections/basic/scopes'
require 'cuprum/collections/basic/scopes/base'
require 'cuprum/collections/queries'
require 'cuprum/collections/scopes/composition/criteria'
require 'cuprum/collections/scopes/criteria'

module Cuprum::Collections::Basic::Scopes
# Scope for filtering on basic collection data based on criteria.
class CriteriaScope < Cuprum::Collections::Basic::Scopes::Base
include Cuprum::Collections::Scopes::Criteria
include Cuprum::Collections::Scopes::Composition::Criteria

Operators = Cuprum::Collections::Queries::Operators
private_constant :Operators
Expand Down
11 changes: 2 additions & 9 deletions lib/cuprum/collections/basic/scopes/disjunction_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

require 'cuprum/collections/basic/scopes'
require 'cuprum/collections/basic/scopes/base'
require 'cuprum/collections/scopes/composition/disjunction'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/disjunction'

module Cuprum::Collections::Basic::Scopes
# Scope for filtering data matching any of the given scopes.
class DisjunctionScope < Cuprum::Collections::Basic::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Disjunction
include Cuprum::Collections::Scopes::Disjunction

# Returns true if the provided item matches any of the configured scopes.
def match?(item:)
Expand All @@ -18,10 +16,5 @@ def match?(item:)
scopes.any? { |scope| scope.match?(item: item) }
end
alias matches? match?

# (see Cuprum::Collections::Scopes::Base#type)
def type
:disjunction
end
end
end
11 changes: 2 additions & 9 deletions lib/cuprum/collections/basic/scopes/negation_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

require 'cuprum/collections/basic/scopes'
require 'cuprum/collections/basic/scopes/base'
require 'cuprum/collections/scopes/composition/negation'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/negation'

module Cuprum::Collections::Basic::Scopes
# Scope for filtering data not matching at least one of the given scopes.
class NegationScope < Cuprum::Collections::Basic::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Negation
include Cuprum::Collections::Scopes::Negation

# Returns true if the provided item does not match at least one scope.
def match?(item:)
Expand All @@ -18,10 +16,5 @@ def match?(item:)
scopes.any? { |scope| !scope.match?(item: item) }
end
alias matches? match?

# (see Cuprum::Collections::Scopes::Base#type)
def type
:negation
end
end
end
3 changes: 3 additions & 0 deletions lib/cuprum/collections/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ module Scopes
autoload :Builder, 'cuprum/collections/scopes/builder'
autoload :Collection, 'cuprum/collections/scopes/collection'
autoload :Composition, 'cuprum/collections/scopes/composition'
autoload :Conjunction, 'cuprum/collections/scopes/conjunction'
autoload :ConjunctionScope, 'cuprum/collections/scopes/conjunction_scope'
autoload :Criteria, 'cuprum/collections/scopes/criteria'
autoload :CriteriaScope, 'cuprum/collections/scopes/criteria_scope'
autoload :Disjunction, 'cuprum/collections/scopes/disjunction'
autoload :DisjunctionScope, 'cuprum/collections/scopes/disjunction_scope'
autoload :Negation, 'cuprum/collections/scopes/negation'
autoload :NegationScope, 'cuprum/collections/scopes/negation_scope'
end
end
5 changes: 0 additions & 5 deletions lib/cuprum/collections/scopes/composition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
module Cuprum::Collections::Scopes
# Defines a fluent interface for composing scopes.
module Composition
autoload :Conjunction, 'cuprum/collections/scopes/composition/conjunction'
autoload :Criteria, 'cuprum/collections/scopes/composition/criteria'
autoload :Disjunction, 'cuprum/collections/scopes/composition/disjunction'
autoload :Negation, 'cuprum/collections/scopes/composition/negation'

# @override and(hash = nil, &block)
# Parses the hash or block and combines using a logical AND.
#
Expand Down
27 changes: 0 additions & 27 deletions lib/cuprum/collections/scopes/composition/criteria.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require 'cuprum/collections/scopes/composition'
require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/container'

module Cuprum::Collections::Scopes::Composition
# Defines composition behavior for conjunction scopes.
module Cuprum::Collections::Scopes
# Functionality for implementing a logical AND scope.
module Conjunction
include Cuprum::Collections::Scopes::Composition
include Cuprum::Collections::Scopes::Container

# (see Cuprum::Collections::Scopes::Composition#and)
def and(*args, &block)
Expand All @@ -25,6 +26,11 @@ def not(*args, &block)
with_scopes([*scopes, inverted])
end

# (see Cuprum::Collections::Scopes::Base#type)
def type
:conjunction
end

private

def and_conjunction_scope(scope)
Expand Down
11 changes: 2 additions & 9 deletions lib/cuprum/collections/scopes/conjunction_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/base'
require 'cuprum/collections/scopes/composition/conjunction'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/conjunction'

module Cuprum::Collections::Scopes
# Generic scope class for defining collection-independent logical AND scopes.
class ConjunctionScope < Cuprum::Collections::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Conjunction

# (see Cuprum::Collections::Scopes::Base#type)
def type
:conjunction
end
include Cuprum::Collections::Scopes::Conjunction
end
end
16 changes: 16 additions & 0 deletions lib/cuprum/collections/scopes/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def initialize(criteria:, **options)
# @return [Array] the criteria used for filtering query data.
attr_reader :criteria

# (see Cuprum::Collections::Scopes::Composition#and)
def and(*args, &block)
return and_criteria_scope(args.first) if criteria_scope?(args.first)

return super if scope?(args.first)

with_criteria([*criteria, *self.class.parse(*args, &block)])
end
alias where and

# (see Cuprum::Collections::Scopes::Base#type)
def type
:criteria
Expand All @@ -258,5 +268,11 @@ def with_criteria(criteria)
protected

attr_writer :criteria

private

def and_criteria_scope(scope)
with_criteria([*criteria, *scope.criteria])
end
end
end
2 changes: 0 additions & 2 deletions lib/cuprum/collections/scopes/criteria_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/base'
require 'cuprum/collections/scopes/composition/criteria'
require 'cuprum/collections/scopes/criteria'

module Cuprum::Collections::Scopes
class CriteriaScope < Cuprum::Collections::Scopes::Base
include Cuprum::Collections::Scopes::Criteria
include Cuprum::Collections::Scopes::Composition::Criteria
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
# frozen_string_literal: true

require 'cuprum/collections/scopes/composition'
require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/container'

module Cuprum::Collections::Scopes::Composition
# Defines composition behavior for disjunction scopes.
module Cuprum::Collections::Scopes
# Functionality for implementing a logical OR scope.
module Disjunction
include Cuprum::Collections::Scopes::Container

# (see Cuprum::Collections::Scopes::Composition#or)
def or(*args, &block)
return or_disjuncton_scope(args.first) if disjunction_scope?(args.first)

with_scopes([*scopes, builder.build(*args, &block)])
end

# (see Cuprum::Collections::Scopes::Base#type)
def type
:disjunction
end

private

def or_disjuncton_scope(scope)
Expand Down
11 changes: 2 additions & 9 deletions lib/cuprum/collections/scopes/disjunction_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/base'
require 'cuprum/collections/scopes/composition/disjunction'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/disjunction'

module Cuprum::Collections::Scopes
# Generic scope class for defining collection-independent logical OR scopes.
class DisjunctionScope < Cuprum::Collections::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Disjunction

# (see Cuprum::Collections::Scopes::Base#type)
def type
:disjunction
end
include Cuprum::Collections::Scopes::Disjunction
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require 'cuprum/collections/scopes/composition'
require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/container'

module Cuprum::Collections::Scopes::Composition
# Defines composition behavior for negation scopes.
module Cuprum::Collections::Scopes
# Functionality for implementing a logical NAND scope.
module Negation
include Cuprum::Collections::Scopes::Composition
include Cuprum::Collections::Scopes::Container

# (see Cuprum::Collections::Scopes::Composition#and)
def and(*args, &block)
Expand All @@ -31,5 +32,10 @@ def not(*args, &block)

builder.build_conjunction_scope(scopes: scopes)
end

# (see Cuprum::Collections::Scopes::Base#type)
def type
:negation
end
end
end
11 changes: 2 additions & 9 deletions lib/cuprum/collections/scopes/negation_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

require 'cuprum/collections/scopes'
require 'cuprum/collections/scopes/base'
require 'cuprum/collections/scopes/composition/negation'
require 'cuprum/collections/scopes/container'
require 'cuprum/collections/scopes/negation'

module Cuprum::Collections::Scopes
# Generic scope class for defining collection-independent logical NAND scopes.
class NegationScope < Cuprum::Collections::Scopes::Base
include Cuprum::Collections::Scopes::Container
include Cuprum::Collections::Scopes::Composition::Negation

# (see Cuprum::Collections::Scopes::Base#type)
def type
:negation
end
include Cuprum::Collections::Scopes::Negation
end
end
36 changes: 0 additions & 36 deletions spec/cuprum/collections/scopes/composition/conjunction_spec.rb

This file was deleted.

Loading

0 comments on commit 077e8c8

Please sign in to comment.