Class: MendixBridge::Inventory
- Inherits:
-
Object
- Object
- MendixBridge::Inventory
- Defined in:
- lib/mendix_bridge/inventory.rb
Defined Under Namespace
Classes: Element
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
- #find(qualified_name) ⇒ Object
-
#initialize(tree, details: {}) ⇒ Inventory
constructor
A new instance of Inventory.
- #modules ⇒ Object
- #of_type(type) ⇒ Object
- #search(query) ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize(tree, details: {}) ⇒ Inventory
Returns a new instance of Inventory.
38 39 40 41 42 43 44 |
# File 'lib/mendix_bridge/inventory.rb', line 38 def initialize(tree, details: {}) raise ArgumentError, "inventory root must be an array" unless tree.is_a?(Array) @tree = tree @details = details @elements = flatten(tree).freeze end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
22 23 24 |
# File 'lib/mendix_bridge/inventory.rb', line 22 def elements @elements end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
22 23 24 |
# File 'lib/mendix_bridge/inventory.rb', line 22 def tree @tree end |
Class Method Details
.from_json(json, details: {}) ⇒ Object
24 25 26 |
# File 'lib/mendix_bridge/inventory.rb', line 24 def self.from_json(json, details: {}) new(JSON.parse(json), details:) end |
.load(path, details_path: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/mendix_bridge/inventory.rb', line 28 def self.load(path, details_path: nil) details_path ||= begin directory = File.dirname(path) current = File.join(directory, "element-details.json") File.file?(current) ? current : File.join(directory, "domain-details.json") end details = File.file?(details_path) ? JSON.parse(File.read(details_path)) : {} from_json(File.read(path), details:) end |
Instance Method Details
#find(qualified_name) ⇒ Object
64 65 66 |
# File 'lib/mendix_bridge/inventory.rb', line 64 def find(qualified_name) elements.find { |element| element.qualified_name == qualified_name } end |
#modules ⇒ Object
60 61 62 |
# File 'lib/mendix_bridge/inventory.rb', line 60 def modules of_type("module") end |
#of_type(type) ⇒ Object
55 56 57 58 |
# File 'lib/mendix_bridge/inventory.rb', line 55 def of_type(type) expected = type.to_s.downcase elements.select { |element| element.type&.downcase == expected } end |
#search(query) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/mendix_bridge/inventory.rb', line 46 def search(query) needle = query.to_s.downcase elements.select do |element| [element.label, element.qualified_name, element.type] .compact .any? { |value| value.downcase.include?(needle) } end end |
#types ⇒ Object
68 69 70 71 72 |
# File 'lib/mendix_bridge/inventory.rb', line 68 def types elements.each_with_object(Hash.new(0)) do |element, counts| counts[element.type] += 1 if element.type end.sort.to_h end |