Module: Cuprum::Collections::Scopes::Container

Included in:
Conjunction, Disjunction
Defined in:
lib/cuprum/collections/scopes/container.rb

Overview

Functionality for implementing a scope container.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scopesArray<Scope>

Returns the scopes wrapped by the scope.

Returns:

  • (Array<Scope>)

    the scopes wrapped by the scope.



18
19
20
# File 'lib/cuprum/collections/scopes/container.rb', line 18

def scopes
  @scopes
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the other object is a scope with matching type and child scopes; otherwise false.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (Boolean)

    true if the other object is a scope with matching type and child scopes; otherwise false.



24
25
26
27
28
# File 'lib/cuprum/collections/scopes/container.rb', line 24

def ==(other)
  return false unless super

  other.scopes == scopes
end

#as_jsonHash{String=>Object}

Returns a JSON-compatible representation of the scope.

Returns:

  • (Hash{String=>Object})

    a JSON-compatible representation of the scope.



31
32
33
# File 'lib/cuprum/collections/scopes/container.rb', line 31

def as_json
  super.merge({ 'scopes' => scopes.map(&:as_json) })
end

#debugObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cuprum/collections/scopes/container.rb', line 36

def debug
  # :nocov:
  message = "#{super} (#{scopes.count})"

  return message if empty?

  scopes.reduce("#{message}:") do |str, scope|
    str + "\n- #{scope.debug.gsub("\n", "\n  ")}"
  end
  # :nocov:
end

#empty?Boolean

Returns true if the scope has no child scopes; otherwise false.

Returns:

  • (Boolean)

    true if the scope has no child scopes; otherwise false.



49
50
51
# File 'lib/cuprum/collections/scopes/container.rb', line 49

def empty?
  @scopes.empty?
end

#initialize(scopes:, **options) ⇒ Object

Parameters:

  • scopes (Array<Scope>)

    the scopes wrapped by the scope.

  • options (Hash)

    additional options for the scope.



11
12
13
14
15
# File 'lib/cuprum/collections/scopes/container.rb', line 11

def initialize(scopes:, **)
  super(**)

  @scopes = scopes
end

#with_scopes(scopes) ⇒ Scope

Creates a copy of the scope with the given child scopes.

Parameters:

  • scopes (Array)

    the child scopes.

Returns:

  • (Scope)

    the copied scope.



58
59
60
# File 'lib/cuprum/collections/scopes/container.rb', line 58

def with_scopes(scopes)
  dup.tap { |copy| copy.scopes = scopes }
end