Class: MPS::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mps/query.rb

Overview

Encapsulates filter predicates derived from CLI options. Has no dependency on Thor; fully unit-testable.

Usage:

q = Query.new(type: "task", status: "open", tag: "work")
filtered_hash = q.apply(elements_hash)

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
# File 'lib/mps/query.rb', line 11

def initialize(opts = {})
  @type_filter = opts[:type]&.downcase
  @tag_filter  = opts[:tag]
  # Collect schema-driven attribute filters from all element classes.
  @attr_filters = _collect_attr_filters(opts)
end

Instance Method Details

#apply(elements_hash) ⇒ Object

Returns a new hash containing only elements that pass all active filters.



20
21
22
# File 'lib/mps/query.rb', line 20

def apply(elements_hash)
  elements_hash.select { |_, el| !el.is_a?(::MPS::Elements::MPS) && _match?(el) }
end

#apply_for_tree(elements_hash) ⇒ Object

Applies filters while preserving the full tree structure including @mps containers. Used by the presenter’s tree renderer so group headers are correctly suppressed when all their children are filtered out.



27
28
29
30
31
32
33
34
35
36
# File 'lib/mps/query.rb', line 27

def apply_for_tree(elements_hash)
  elements_hash.select do |ref_key, el|
    if el.is_a?(::MPS::Elements::MPS)
      prefix = "#{ref_key}."
      elements_hash.any? { |k, v| k.start_with?(prefix) && !v.is_a?(::MPS::Elements::MPS) && _match?(v) }
    else
      _match?(el)
    end
  end
end

#match?(el) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mps/query.rb', line 38

def match?(el)
  _match?(el)
end