Class: Unitsdb::Database::UniquenessValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/unitsdb/database/uniqueness_validator.rb

Overview

Validates that short names and identifier ids are unique within each collection. Encodes the policy of which collections participate in each check via two constants so the intent is explicit.

UniquenessValidator.new(db).validate
# => #<struct short={...}, id={...}>

Defined Under Namespace

Classes: Result

Constant Summary collapse

SHORT_COLLECTIONS =

Collections that carry a short attribute and must be unique-by-short within each collection.

%i[units dimensions unit_systems].freeze
IDENTIFIER_COLLECTIONS =

Collections whose identifier id values must be unique.

Database::COLLECTIONS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ UniquenessValidator

Returns a new instance of UniquenessValidator.



26
27
28
# File 'lib/unitsdb/database/uniqueness_validator.rb', line 26

def initialize(database)
  @database = database
end

Class Method Details

.validate(database) ⇒ Object

Convenience entry-point used by Database#validate_uniqueness.



38
39
40
41
42
43
44
# File 'lib/unitsdb/database/uniqueness_validator.rb', line 38

def self.validate(database)
  result = new(database).validate
  {
    short: result.short,
    id: result.id,
  }
end

Instance Method Details

#validateObject



30
31
32
33
34
35
# File 'lib/unitsdb/database/uniqueness_validator.rb', line 30

def validate
  Result.new(
    short: scan_shorts,
    id: scan_identifiers,
  )
end