Class: Archsight::Database

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_annotationsObject

Returns the value of attribute compute_annotations.



46
47
48
# File 'lib/archsight/database.rb', line 46

def compute_annotations
  @compute_annotations
end

#instancesObject

Returns the value of attribute instances.



46
47
48
# File 'lib/archsight/database.rb', line 46

def instances
  @instances
end

#only_kindsObject

Returns the value of attribute only_kinds.



46
47
48
# File 'lib/archsight/database.rb', line 46

def only_kinds
  @only_kinds
end

#verboseObject

Returns the value of attribute verbose.



46
47
48
# File 'lib/archsight/database.rb', line 46

def verbose
  @verbose
end

#verifyObject

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

Returns:

  • (Boolean)


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.message, ref))
end