Class: Meta::Scope::Composite

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/meta/scope/base.rb

Overview

组合式 Scope 实例,用于表示多个 Scope 的逻辑操作

Direct Known Subclasses

And, Or

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#and, #include_scope, #inspect, #match?, #match_scopes?, #or, #scope_name=

Constructor Details

#initialize(*scopes) ⇒ Composite

Returns a new instance of Composite.



109
110
111
112
113
# File 'lib/meta/scope/base.rb', line 109

def initialize(*scopes)
  @scopes = scopes.compact.map do |scope|
    scope.is_a?(self.class) ? scope.defined_scopes : scope
  end.flatten.freeze
end

Class Method Details

.concat(*scopes) ⇒ Object

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/meta/scope/base.rb', line 97

def self.concat(*scopes)
  composite_classes = scopes.filter { |scope| scope.is_a?(Composite) }
                            .map(&:class).uniq
  raise ArgumentError, "不能执行 concat,参数中包含了多个逻辑运算符:#{composite_classes.join(',')}" if composite_classes.length > 1

  if composite_classes.empty?
    Or.new(*scopes)
  else
    composite_classes[0].new(*scopes)
  end
end

.new(*scopes) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/meta/scope/base.rb', line 87

def self.new(*scopes)
  if scopes.length == 0
    @empty || (@empty = self.new)
  elsif scopes.length == 1
    scopes[0]
  else
    super(*scopes)
  end
end

Instance Method Details

#defined_scopesObject



115
116
117
# File 'lib/meta/scope/base.rb', line 115

def defined_scopes
  @scopes
end

#scope_nameObject



119
120
121
# File 'lib/meta/scope/base.rb', line 119

def scope_name
  @scope_name || @scopes.map(&:scope_name).sort.join('_')
end