Class: Archsight::Database
- Inherits:
-
Object
- Object
- Archsight::Database
- Defined in:
- lib/archsight/database.rb
Overview
Database loads yaml files and folders to create an in-memory representation of the structure. The loading and parsing of files will raise errors if invalid data is passed.
Instance Attribute Summary collapse
-
#compute_annotations ⇒ Object
Returns the value of attribute compute_annotations.
-
#instances ⇒ Object
Returns the value of attribute instances.
-
#only_kinds ⇒ Object
Returns the value of attribute only_kinds.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#verify ⇒ Object
Returns the value of attribute verify.
Instance Method Summary collapse
-
#annotation_values(kind, annotation) ⇒ Object
Collect unique annotation values across all instances of a kind.
-
#filters_for_kind(kind) ⇒ Object
Get filterable annotations with their values for a kind (excludes empty).
-
#initialize(path, verbose: false, verify: true, compute_annotations: true, only_kinds: nil) ⇒ Database
constructor
A new instance of Database.
- #instance_by_kind(kind, instance) ⇒ Object
-
#instance_matches?(instance, query_string) ⇒ Boolean
Check if a specific instance matches a query.
- #instances_by_kind(kind) ⇒ Object
-
#query(query_string) ⇒ Object
Execute a query string and return matching instances.
- #reload! ⇒ Object
Constructor Details
#initialize(path, verbose: false, verify: true, compute_annotations: true, only_kinds: nil) ⇒ Database
Returns a new instance of Database.
48 49 50 51 52 53 54 55 |
# File 'lib/archsight/database.rb', line 48 def initialize(path, verbose: false, verify: true, compute_annotations: true, only_kinds: nil) @path = path @verbose = verbose @verify = verify @compute_annotations = compute_annotations @only_kinds = only_kinds @instances = {} end |
Instance Attribute Details
#compute_annotations ⇒ Object
Returns the value of attribute compute_annotations.
46 47 48 |
# File 'lib/archsight/database.rb', line 46 def compute_annotations @compute_annotations end |
#instances ⇒ Object
Returns the value of attribute instances.
46 47 48 |
# File 'lib/archsight/database.rb', line 46 def instances @instances end |
#only_kinds ⇒ Object
Returns the value of attribute only_kinds.
46 47 48 |
# File 'lib/archsight/database.rb', line 46 def only_kinds @only_kinds end |
#verbose ⇒ Object
Returns the value of attribute verbose.
46 47 48 |
# File 'lib/archsight/database.rb', line 46 def verbose @verbose end |
#verify ⇒ Object
Returns the value of attribute verify.
46 47 48 |
# File 'lib/archsight/database.rb', line 46 def verify @verify end |
Instance Method Details
#annotation_values(kind, annotation) ⇒ Object
Collect unique annotation values across all instances of a kind
84 85 86 87 88 |
# File 'lib/archsight/database.rb', line 84 def annotation_values(kind, annotation) instances = instances_by_kind(kind).values values = instances.flat_map { |inst| Array(annotation.value_for(inst)) } values.compact.uniq.sort end |
#filters_for_kind(kind) ⇒ Object
Get filterable annotations with their values for a kind (excludes empty)
91 92 93 94 95 96 97 98 |
# File 'lib/archsight/database.rb', line 91 def filters_for_kind(kind) klass = Archsight::Resources[kind] return [] unless klass klass.filterable_annotations .map { |a| [a, annotation_values(kind, a)] } .reject { |_, values| values.empty? } end |
#instance_by_kind(kind, instance) ⇒ Object
79 80 81 |
# File 'lib/archsight/database.rb', line 79 def instance_by_kind(kind, instance) @instances[Archsight::Resources[kind]][instance] end |
#instance_matches?(instance, query_string) ⇒ Boolean
Check if a specific instance matches a query
107 108 109 110 |
# File 'lib/archsight/database.rb', line 107 def instance_matches?(instance, query_string) q = Archsight::Query.parse(query_string) q.matches?(instance, database: self) end |
#instances_by_kind(kind) ⇒ Object
75 76 77 |
# File 'lib/archsight/database.rb', line 75 def instances_by_kind(kind) @instances[Archsight::Resources[kind]] || {} end |
#query(query_string) ⇒ Object
Execute a query string and return matching instances
101 102 103 104 |
# File 'lib/archsight/database.rb', line 101 def query(query_string) q = Archsight::Query.parse(query_string) q.filter(self) end |
#reload! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/archsight/database.rb', line 57 def reload! @instances = {} # load all resources Dir.glob(File.join(@path, "**/*.yaml")).each do |path| @current_ref = LineReference.new(path, 0) puts "parsing #{path}..." if @verbose load_file(path) end verify! if @verify compute_all_annotations! if @verify && @compute_annotations rescue Psych::SyntaxError => e # Wrap YAML syntax errors in ResourceError for consistent handling ref = LineReference.new(e.file || @current_ref&.path || "unknown", e.line || 0) Kernel.raise(ResourceError.new(e.problem || e., ref)) end |