Class: Loom::Pattern::ExpandingReference

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/pattern/expanding_reference.rb

Defined Under Namespace

Classes: Matcher

Constant Summary collapse

RecursiveExpansionError =
Class.new Loom::LoomError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, pattern, reference_set) ⇒ ExpandingReference

Returns a new instance of ExpandingReference.

Parameters:

  • slug (String)

    : flattened colon separated slug name

  • pattern (Loom::Pattern::Pattern)

    : a pattern responding to expanded_slugs



15
16
17
18
19
20
21
22
23
24
# File 'lib/loom/pattern/expanding_reference.rb', line 15

def initialize(slug, pattern, reference_set)
  @slug = slug
  @reference_set = reference_set
  # TODO: Hmm... I tried to abstract the "weave" keyword from the
  # "ExpandingReference" concept... but it leaked through. Think the
  # `pattern.kind` based method name over.
  @reference_slugs = pattern.weave.expanded_slugs
  @desc = pattern.description
  @pattern
end

Instance Attribute Details

#descObject (readonly)

TODO: Ensure ExpandingReference and Reference stay in sync. Maybe create an inheritance hierarchy.



10
11
12
# File 'lib/loom/pattern/expanding_reference.rb', line 10

def desc
  @desc
end

#patternObject (readonly)

TODO: Ensure ExpandingReference and Reference stay in sync. Maybe create an inheritance hierarchy.



10
11
12
# File 'lib/loom/pattern/expanding_reference.rb', line 10

def pattern
  @pattern
end

#reference_slugsObject (readonly)

TODO: Ensure ExpandingReference and Reference stay in sync. Maybe create an inheritance hierarchy.



10
11
12
# File 'lib/loom/pattern/expanding_reference.rb', line 10

def reference_slugs
  @reference_slugs
end

#slugObject (readonly)

TODO: Ensure ExpandingReference and Reference stay in sync. Maybe create an inheritance hierarchy.



10
11
12
# File 'lib/loom/pattern/expanding_reference.rb', line 10

def slug
  @slug
end

#source_fileObject (readonly)

TODO: Ensure ExpandingReference and Reference stay in sync. Maybe create an inheritance hierarchy.



10
11
12
# File 'lib/loom/pattern/expanding_reference.rb', line 10

def source_file
  @source_file
end

Instance Method Details

#expand_slugsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/loom/pattern/expanding_reference.rb', line 26

def expand_slugs
  # O(MN) :(
  expanded_slugs = @reference_slugs.flat_map do |my_slug|
    matcher = Matcher.get_matcher(my_slug)
    @reference_set.slugs.select { |your_slug| matcher.match? your_slug }
  end.uniq
  Loom.log.debug3(self) { "Loom::Pattern::ExpandingReference@reference_slugs+: #{@reference_slugs.join(",")}"}
  Loom.log.debug3(self) { "Loom::Pattern::ExpandingReference+expanded_slugs+: #{expanded_slugs.join(",")}"}

  expanded_refs = expanded_slugs.map { |s| @reference_set[s] }
  expanded_refs.each do |r|
    if r.is_a? ExpandingReference
      Loom.log.error "recursive expansion for pattern[#{r.slug}] in weave[#{@slug}], i.e. only patterns are allowed in weaves"
      raise RecursiveExpansionError, @slug
    end
  end

  Loom.log.info { "expanded slug[#{@slug}] => #{expanded_slugs.join(",")}"}
  expanded_slugs
end