class LittleGhost::Models::Catalog
Resolves model facts from refreshed data and the snapshot packaged with LittleGhost. Refresh is always explicit.
Constants
- SNAPSHOT_PATH
-
Path to the offline model metadata snapshot packaged with the gem.
Public Class Methods
# File lib/little_ghost/models/catalog.rb, line 18 def initialize(sources: [], snapshot_path: SNAPSHOT_PATH, clock: -> { Time.now.utc }) @sources = Array(sources).freeze @snapshot = load_snapshot(snapshot_path) @refreshed = {} @clock = clock @mutex = Mutex.new end
Builds a layered catalog from bundled and refreshed facts.
Public Instance Methods
Source
# File lib/little_ghost/models/catalog.rb, line 27 def details(target) target = Target.parse(target) records = [@snapshot[target.to_s], @mutex.synchronize { @refreshed[target.to_s] }].compact merge_details(target, records) end
Returns immutable normalized details for target.
Source
# File lib/little_ghost/models/catalog.rb, line 35 def refresh!(target: nil) requested = target && Target.parse(target) records = {} errors = [] @sources.each do |source| values = source.refresh(target: requested) normalized = normalize_records( values, source.name, attribute_merge_strategies: source.attribute_merge_strategies ) records = records.merge(normalized) { |_key, older, newer| merge_records(older, newer) } rescue => error errors << error end @mutex.synchronize { @refreshed = @refreshed.merge(records) { |_key, older, newer| merge_records(older, newer) } } unless records.empty? {updated: records.keys.freeze, errors: errors.freeze}.freeze end
Refreshes one target or all models. Failed sources leave the last good catalog untouched and are returned to the caller for reporting.