Class: MarkdownComposer::SelectionResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/selection_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, options: {}, diagnostics: Diagnostics.new, path: nil) ⇒ SelectionResolver

Returns a new instance of SelectionResolver.



7
8
9
10
11
12
# File 'lib/markdown_composer/selection_resolver.rb', line 7

def initialize(index:, options: {}, diagnostics: Diagnostics.new, path: nil)
  @index = index
  @options = options
  @diagnostics = diagnostics
  @path = path
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



5
6
7
# File 'lib/markdown_composer/selection_resolver.rb', line 5

def diagnostics
  @diagnostics
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/markdown_composer/selection_resolver.rb', line 5

def index
  @index
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/markdown_composer/selection_resolver.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/markdown_composer/selection_resolver.rb', line 5

def path
  @path
end

Instance Method Details

#include_units(unit, include_config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/markdown_composer/selection_resolver.rb', line 33

def include_units(unit, include_config)
  included = []
  excluded = []

  Array(include_config).each do |item|
    if item["exclude"]
      excluded.concat(resolve_nested(unit, item["exclude"]))
    else
      included.concat(resolve_nested(unit, item))
    end
  end

  (included - excluded).uniq(&:id).sort_by(&:source_position)
end

#resolve(selector, within: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/markdown_composer/selection_resolver.rb', line 14

def resolve(selector, within: nil)
  selector ||= { "type" => "all" }
  matches = if selector["types"]
    selector["types"].flat_map { |type| matches_for(type, within: within) }.uniq(&:id)
  else
    matches_for(selector["type"] || "all", within: within)
  end
  matches = apply_where(matches, selector["where"]) if selector["where"]
  matches = Take.apply(matches, selector["take"], diagnostics: diagnostics, path: path, seed: options.fetch(:seed, 0)) if selector["take"]
  diagnostics.warn("selection.empty", "Selection matched no content", path: path) if matches.empty?
  matches
end

#resolve_with_includes(selector, include_config) ⇒ Object



27
28
29
30
31
# File 'lib/markdown_composer/selection_resolver.rb', line 27

def resolve_with_includes(selector, include_config)
  resolve(selector).flat_map do |unit|
    include_units(unit, include_config || [ { "type" => "all" } ])
  end.uniq(&:id).sort_by(&:source_position)
end