Module: Unitsml::Unitsdb::Finders

Included in:
Dimensions, Prefixes, Units
Defined in:
lib/unitsml/unitsdb/finders.rb

Overview

Shared lookup helpers for Unitsml collection wrappers. Each host collection (Units, Prefixes, Dimensions) keeps its own public find_by_* API; this module owns the two scanning shapes that the host implementations previously duplicated.

Instance Method Summary collapse

Instance Method Details

#find_first_in(collection, field:, value:) ⇒ Object

Find the first item in collection where item.<field> == value.



11
12
13
# File 'lib/unitsml/unitsdb/finders.rb', line 11

def find_first_in(collection, field:, value:)
  collection.find { |item| item.public_send(field) == value }
end

#find_first_through(collection, via:, field:, value:) ⇒ Object

Find the first item in collection where item.<via> contains an element whose <field> equals value. Used for collections of items that expose a nested list of identifiers or symbols.



18
19
20
21
22
23
24
# File 'lib/unitsml/unitsdb/finders.rb', line 18

def find_first_through(collection, via:, field:, value:)
  collection.find do |item|
    item.public_send(via).any? do |inner|
      inner.public_send(field) == value
    end
  end
end