Class: Ea::Query::Builder
- Inherits:
-
Object
- Object
- Ea::Query::Builder
- Includes:
- Enumerable
- Defined in:
- lib/ea/query/builder.rb
Overview
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#call ⇒ Array
Materialize the filtered collection.
-
#classes ⇒ Builder
Filter to UML classes only.
-
#diagrams ⇒ Builder
Filter to diagrams.
- #each(&block) ⇒ Object
-
#filter_by(&block) ⇒ Builder
Add a custom filter proc.
-
#in_package(name) ⇒ Builder
Restrict to elements in the named package.
-
#initialize(model, filters: [], kind: :objects) ⇒ Builder
constructor
A new instance of Builder.
-
#interfaces ⇒ Builder
Filter to interfaces only.
-
#name_contains(substring) ⇒ Builder
Filter by name substring (case-insensitive).
-
#named(name) ⇒ Builder
Filter by name exact match.
-
#packages ⇒ Builder
Filter to packages.
-
#with_stereotype(name) ⇒ Builder
Filter by applied stereotype name.
-
#with_type(type) ⇒ Builder
Filter by EA object_type (Class, Interface, Package, ...).
Constructor Details
#initialize(model, filters: [], kind: :objects) ⇒ Builder
Returns a new instance of Builder.
15 16 17 18 19 |
# File 'lib/ea/query/builder.rb', line 15 def initialize(model, filters: [], kind: :objects) @model = model @filters = filters @kind = kind end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
13 14 15 |
# File 'lib/ea/query/builder.rb', line 13 def filters @filters end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
13 14 15 |
# File 'lib/ea/query/builder.rb', line 13 def model @model end |
Instance Method Details
#call ⇒ Array
Materialize the filtered collection.
95 96 97 98 |
# File 'lib/ea/query/builder.rb', line 95 def call records = model.collections[@kind] || [] records.select { |o| filters.all? { |f| f.call(o) } } end |
#classes ⇒ Builder
Filter to UML classes only.
23 24 25 |
# File 'lib/ea/query/builder.rb', line 23 def classes with_type("Class") end |
#diagrams ⇒ Builder
Filter to diagrams.
41 42 43 |
# File 'lib/ea/query/builder.rb', line 41 def diagrams Builder.new(model, filters: filters, kind: :diagrams) end |
#each(&block) ⇒ Object
103 104 105 |
# File 'lib/ea/query/builder.rb', line 103 def each(&block) call.each(&block) end |
#filter_by(&block) ⇒ Builder
Add a custom filter proc.
89 90 91 |
# File 'lib/ea/query/builder.rb', line 89 def filter_by(&block) Builder.new(model, filters: filters + [block], kind: @kind) end |
#in_package(name) ⇒ Builder
Restrict to elements in the named package.
54 55 56 57 58 59 |
# File 'lib/ea/query/builder.rb', line 54 def in_package(name) pkg = (model.collections[:packages] || []).find { |p| p.name == name } return self unless pkg filter_by { |o| o.package_id == pkg.package_id } end |
#interfaces ⇒ Builder
Filter to interfaces only.
29 30 31 |
# File 'lib/ea/query/builder.rb', line 29 def interfaces with_type("Interface") end |
#name_contains(substring) ⇒ Builder
Filter by name substring (case-insensitive).
81 82 83 84 |
# File 'lib/ea/query/builder.rb', line 81 def name_contains(substring) re = Regexp.new(Regexp.escape(substring), Regexp::IGNORECASE) filter_by { |o| o.name&.match?(re) } end |
#named(name) ⇒ Builder
Filter by name exact match.
74 75 76 |
# File 'lib/ea/query/builder.rb', line 74 def named(name) filter_by { |o| o.name == name } end |
#packages ⇒ Builder
Filter to packages.
35 36 37 |
# File 'lib/ea/query/builder.rb', line 35 def packages Builder.new(model, filters: filters, kind: :packages) end |
#with_stereotype(name) ⇒ Builder
Filter by applied stereotype name.
64 65 66 67 68 69 |
# File 'lib/ea/query/builder.rb', line 64 def with_stereotype(name) filter_by do |o| refs = o.respond_to?(:stereotype_refs) ? o.stereotype_refs : nil refs&.any? { |r| r == name } end end |