Class: Multisync::Selector
- Inherits:
-
Object
- Object
- Multisync::Selector
- Defined in:
- lib/multisync/selector.rb
Instance Attribute Summary collapse
-
#catalog ⇒ Object
readonly
Given catalog.
-
#queries ⇒ Object
readonly
Given queries.
-
#results ⇒ Object
readonly
Selected tasks.
Instance Method Summary collapse
-
#initialize(catalog, queries) ⇒ Selector
constructor
A new instance of Selector.
- #selected ⇒ Object
- #selected?(subject) ⇒ Boolean
- #selected_fullnames ⇒ Object
- #selected_or_parent_of_selected?(subject) ⇒ Boolean
- #selected_with_parents ⇒ Object
- #tasks(parents: false) ⇒ Object
- #visit(subject) ⇒ Object
Constructor Details
#initialize(catalog, queries) ⇒ Selector
Returns a new instance of Selector.
11 12 13 14 15 16 17 |
# File 'lib/multisync/selector.rb', line 11 def initialize catalog, queries @catalog = catalog @queries = queries @results = [] @all_subjects = [] @subjects_by_name = [] end |
Instance Attribute Details
#catalog ⇒ Object (readonly)
Given catalog
3 4 5 |
# File 'lib/multisync/selector.rb', line 3 def catalog @catalog end |
#queries ⇒ Object (readonly)
Given queries
6 7 8 |
# File 'lib/multisync/selector.rb', line 6 def queries @queries end |
#results ⇒ Object (readonly)
Selected tasks
9 10 11 |
# File 'lib/multisync/selector.rb', line 9 def results @results end |
Instance Method Details
#selected ⇒ Object
41 42 43 |
# File 'lib/multisync/selector.rb', line 41 def selected @selected ||= results.select { selected? _1 } end |
#selected?(subject) ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/multisync/selector.rb', line 45 def selected? subject # return only subjects with a fullname return false if subject.fullname.empty? # no queries defined, but subject is in the default set return true if queries.empty? && subject.default? # only return the leaves of the definition tree # return false unless subject.members.any? # subject matches any of the given queries queries.any? { /\b#{_1}\b/.match subject.fullname } end |
#selected_fullnames ⇒ Object
37 38 39 |
# File 'lib/multisync/selector.rb', line 37 def selected_fullnames @selected_fullnames ||= selected.map(&:fullname) end |
#selected_or_parent_of_selected?(subject) ⇒ Boolean
32 33 34 35 |
# File 'lib/multisync/selector.rb', line 32 def selected_or_parent_of_selected? subject !subject.fullname.empty? && selected_fullnames.any? { %r{^#{subject.fullname}(?:/|$)}.match _1 } end |
#selected_with_parents ⇒ Object
28 29 30 |
# File 'lib/multisync/selector.rb', line 28 def selected_with_parents @selected_with_parents ||= results.select { selected_or_parent_of_selected? _1 } end |
#tasks(parents: false) ⇒ Object
19 20 21 22 |
# File 'lib/multisync/selector.rb', line 19 def tasks parents: false catalog.traverse self parents ? selected_with_parents : selected end |
#visit(subject) ⇒ Object
24 25 26 |
# File 'lib/multisync/selector.rb', line 24 def visit subject results << subject end |