Class: ArchSpec::ComponentSpec
- Inherits:
-
Object
- Object
- ArchSpec::ComponentSpec
- Defined in:
- lib/archspec/component_spec.rb
Overview
How a component selects its members: by file glob, by namespace, or by explicit constant name. Created by ArchSpec::DSL::Context#component. The analyzer uses it to assign files and constants to the component.
Instance Attribute Summary collapse
-
#ancestor_names ⇒ Object
readonly
Returns the value of attribute ancestor_names.
-
#constants ⇒ Object
readonly
Returns the value of attribute constants.
-
#exclude_patterns ⇒ Object
readonly
Returns the value of attribute exclude_patterns.
-
#file_patterns ⇒ Object
readonly
Returns the value of attribute file_patterns.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Instance Method Summary collapse
-
#initialize(name, files: [], except: [], namespace: nil, constants: nil, descendants_of: nil) ⇒ ComponentSpec
constructor
A new instance of ComponentSpec.
- #matches_constant?(name) ⇒ Boolean
- #matching_ancestor(name, graph) ⇒ Object
- #merge!(other) ⇒ Object
Constructor Details
#initialize(name, files: [], except: [], namespace: nil, constants: nil, descendants_of: nil) ⇒ ComponentSpec
Returns a new instance of ComponentSpec.
10 11 12 13 14 15 16 17 |
# File 'lib/archspec/component_spec.rb', line 10 def initialize(name, files: [], except: [], namespace: nil, constants: nil, descendants_of: nil) @name = name.to_sym @file_patterns = Array(files).compact.map(&:to_s) @exclude_patterns = Array(except).compact.map(&:to_s) @namespaces = Array(namespace).compact.map { |value| normalize_constant(value) } @constants = Array(constants).compact.map { |value| normalize_constant(value) } @ancestor_names = Array(descendants_of).compact.map { |value| normalize_constant(value) } end |
Instance Attribute Details
#ancestor_names ⇒ Object (readonly)
Returns the value of attribute ancestor_names.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def ancestor_names @ancestor_names end |
#constants ⇒ Object (readonly)
Returns the value of attribute constants.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def constants @constants end |
#exclude_patterns ⇒ Object (readonly)
Returns the value of attribute exclude_patterns.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def exclude_patterns @exclude_patterns end |
#file_patterns ⇒ Object (readonly)
Returns the value of attribute file_patterns.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def file_patterns @file_patterns end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def name @name end |
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
8 9 10 |
# File 'lib/archspec/component_spec.rb', line 8 def namespaces @namespaces end |
Instance Method Details
#matches_constant?(name) ⇒ Boolean
28 29 30 31 32 33 34 35 |
# File 'lib/archspec/component_spec.rb', line 28 def matches_constant?(name) normalized = normalize_constant(name) constants.include?(normalized) || namespaces.any? do |namespace| normalized == namespace || normalized.start_with?("#{namespace}::") end end |
#matching_ancestor(name, graph) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/archspec/component_spec.rb', line 37 def matching_ancestor(name, graph) return if ancestor_names.empty? ancestors = graph.ancestor_names(name).first ancestor_names.find { |ancestor| ancestors.include?(ancestor) } end |
#merge!(other) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/archspec/component_spec.rb', line 19 def merge!(other) @file_patterns |= other.file_patterns @exclude_patterns |= other.exclude_patterns @namespaces |= other.namespaces @constants |= other.constants @ancestor_names |= other.ancestor_names self end |