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.
114 115 116 117 |
# File 'lib/ea/query/builder.rb', line 114 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
122 123 124 |
# File 'lib/ea/query/builder.rb', line 122 def each(&block) call.each(&block) end |
#filter_by(&block) ⇒ Builder
Add a custom filter proc.
91 92 93 |
# File 'lib/ea/query/builder.rb', line 91 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).
83 84 85 86 |
# File 'lib/ea/query/builder.rb', line 83 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.
76 77 78 |
# File 'lib/ea/query/builder.rb', line 76 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. Stereotype lookup walks t_xref for @STEREO blocks referencing the object's ea_guid.
65 66 67 68 69 70 71 |
# File 'lib/ea/query/builder.rb', line 65 def with_stereotype(name) xrefs = model.collections[:xrefs] || [] filter_by do |o| applied = stereotype_names_for(o, xrefs) applied.any? { |r| r == name } end end |