unitsdb 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +3 -0
  3. data/.rspec +2 -1
  4. data/.rubocop_todo.yml +88 -12
  5. data/Gemfile +2 -2
  6. data/README.adoc +697 -1
  7. data/exe/unitsdb +7 -0
  8. data/lib/unitsdb/cli.rb +81 -0
  9. data/lib/unitsdb/commands/_modify.rb +22 -0
  10. data/lib/unitsdb/commands/base.rb +52 -0
  11. data/lib/unitsdb/commands/check_si.rb +124 -0
  12. data/lib/unitsdb/commands/get.rb +133 -0
  13. data/lib/unitsdb/commands/normalize.rb +81 -0
  14. data/lib/unitsdb/commands/release.rb +112 -0
  15. data/lib/unitsdb/commands/search.rb +219 -0
  16. data/lib/unitsdb/commands/si_formatter.rb +485 -0
  17. data/lib/unitsdb/commands/si_matcher.rb +470 -0
  18. data/lib/unitsdb/commands/si_ttl_parser.rb +100 -0
  19. data/lib/unitsdb/commands/si_updater.rb +212 -0
  20. data/lib/unitsdb/commands/validate/identifiers.rb +40 -0
  21. data/lib/unitsdb/commands/validate/references.rb +316 -0
  22. data/lib/unitsdb/commands/validate/si_references.rb +115 -0
  23. data/lib/unitsdb/commands/validate.rb +40 -0
  24. data/lib/unitsdb/database.rb +661 -0
  25. data/lib/unitsdb/dimension.rb +49 -0
  26. data/lib/unitsdb/dimension_details.rb +20 -0
  27. data/lib/unitsdb/dimension_reference.rb +8 -0
  28. data/lib/unitsdb/dimensions.rb +4 -10
  29. data/lib/unitsdb/errors.rb +13 -0
  30. data/lib/unitsdb/external_reference.rb +14 -0
  31. data/lib/unitsdb/identifier.rb +8 -0
  32. data/lib/unitsdb/localized_string.rb +17 -0
  33. data/lib/unitsdb/prefix.rb +30 -0
  34. data/lib/unitsdb/prefix_reference.rb +10 -0
  35. data/lib/unitsdb/prefixes.rb +4 -11
  36. data/lib/unitsdb/quantities.rb +4 -31
  37. data/lib/unitsdb/quantity.rb +21 -0
  38. data/lib/unitsdb/quantity_reference.rb +10 -0
  39. data/lib/unitsdb/root_unit_reference.rb +14 -0
  40. data/lib/unitsdb/scale.rb +17 -0
  41. data/lib/unitsdb/scale_properties.rb +12 -0
  42. data/lib/unitsdb/scale_reference.rb +10 -0
  43. data/lib/unitsdb/scales.rb +11 -0
  44. data/lib/unitsdb/si_derived_base.rb +19 -0
  45. data/lib/unitsdb/symbol_presentations.rb +3 -8
  46. data/lib/unitsdb/unit.rb +63 -0
  47. data/lib/unitsdb/unit_reference.rb +10 -0
  48. data/lib/unitsdb/unit_system.rb +15 -0
  49. data/lib/unitsdb/unit_system_reference.rb +10 -0
  50. data/lib/unitsdb/unit_systems.rb +4 -10
  51. data/lib/unitsdb/units.rb +4 -10
  52. data/lib/unitsdb/utils.rb +84 -0
  53. data/lib/unitsdb/version.rb +1 -1
  54. data/lib/unitsdb.rb +12 -2
  55. data/unitsdb.gemspec +6 -1
  56. metadata +116 -20
  57. data/lib/unitsdb/dimensions/dimension.rb +0 -59
  58. data/lib/unitsdb/dimensions/quantity.rb +0 -32
  59. data/lib/unitsdb/dimensions/symbol.rb +0 -26
  60. data/lib/unitsdb/prefixes/prefix.rb +0 -35
  61. data/lib/unitsdb/prefixes/symbol.rb +0 -17
  62. data/lib/unitsdb/quantities/quantity.rb +0 -37
  63. data/lib/unitsdb/quantities/unit_reference.rb +0 -15
  64. data/lib/unitsdb/unit_systems/unit_system.rb +0 -19
  65. data/lib/unitsdb/units/quantity_reference.rb +0 -17
  66. data/lib/unitsdb/units/root_unit.rb +0 -21
  67. data/lib/unitsdb/units/root_units.rb +0 -18
  68. data/lib/unitsdb/units/si_derived_base.rb +0 -26
  69. data/lib/unitsdb/units/symbol.rb +0 -19
  70. data/lib/unitsdb/units/system.rb +0 -17
  71. data/lib/unitsdb/units/unit.rb +0 -73
  72. data/lib/unitsdb/unitsdb.rb +0 -6
data/README.adoc CHANGED
@@ -26,7 +26,703 @@ to access and manipulate the UnitsDB content.
26
26
  $ gem install unitsdb
27
27
  ----
28
28
 
29
- == Usage
29
+
30
+
31
+ == UnitsDB version support
32
+
33
+ === General
34
+
35
+ This library supports the UnitsDB 2.0.0 format only.
36
+
37
+ The version of the YAML files are stored in the `version` field of the `*.yaml`
38
+ files. The library checks this version when loading the database and raises an
39
+ error if the version is not 2.0.0.
40
+
41
+ === UnitsDB 2.0.0 features
42
+
43
+ ==== General
44
+
45
+ UnitsDB 2.0.0 introduces several significant improvements over version 1.0.0.
46
+
47
+ ==== UnitsML identifiers
48
+
49
+ From UnitsDB 2.0.0, all entities now have an organization-independent identifier
50
+ that is unique across all entities in the scope of `unitsml`.
51
+
52
+ ==== New Content
53
+
54
+ Version 2.0.0 includes several new additions:
55
+
56
+ * New dimensions (fluence, phase, fuel efficiency, etc.)
57
+ * New quantities (like emission_rate, fluence, kerma_rate, etc.)
58
+ * Formal structure for scale definitions
59
+ * Additional symbols and improved representation
60
+
61
+ ==== Multilingual support
62
+
63
+ Version 2.0.0 adds support for localized names in multiple languages:
64
+
65
+ * Names are now structured as objects with `value` and `lang` properties
66
+ * English (en) is the primary language for all entries
67
+ * French (fr) translations for units and quantities are available
68
+
69
+ [source,ruby]
70
+ ----
71
+ # Accessing multilingual names (UnitsDB 2.0.0)
72
+ meter = db.find_by_type(id: "NISTu1", type: "units")
73
+ english_names = meter.names.select { |n| n.lang == "en" }.map(&:value)
74
+ french_names = meter.names.select { |n| n.lang == "fr" }.map(&:value)
75
+ ----
76
+
77
+ ==== Enhanced symbol representation
78
+
79
+ Symbols now have more comprehensive representation formats:
80
+
81
+ * All entities with symbols have representations in multiple formats (ASCII, Unicode, HTML, MathML, LaTeX)
82
+ * Prefixes now use the same symbol structure as units with a collection of symbol objects
83
+ * Dimensions now use `symbols` instead of `dim_symbols`
84
+
85
+ ==== External references
86
+
87
+ The 2.0.0 format includes a formalized approach to external references:
88
+
89
+ * The `references` field links to external resources like the SI Digital Framework
90
+ * More consistent structure for references between entities
91
+
92
+
93
+ === Unified UnitsDB release file format
94
+
95
+ While the UnitsDB database is maintained in separate YAML files for easier
96
+ management (`units.yaml`, `quantities.yaml`, etc.), the unified release file
97
+ consolidates all data into a single YAML file for improved user convenience.
98
+
99
+ Syntax:
100
+
101
+ [source,yaml]
102
+ ----
103
+ schema_version: 2.0.0
104
+ dimensions:
105
+ - identifiers: [...]
106
+ length: {...}
107
+ names: [...]
108
+ - ...
109
+ prefixes:
110
+ - identifiers: [...]
111
+ name: ...
112
+ symbols: [...]
113
+ - ...
114
+ quantities:
115
+ - identifiers: [...]
116
+ quantity_type: ...
117
+ names: [...]
118
+ - ...
119
+ units:
120
+ - identifiers: [...]
121
+ names: [...]
122
+ symbols: [...]
123
+ - ...
124
+ unit_systems:
125
+ - identifiers: [...]
126
+ name: ...
127
+ - ...
128
+ ----
129
+
130
+ There are several advantages to using the unified file format:
131
+
132
+ * Simplified usage: obsoletes the needd to load and manage multiple files
133
+ * Consistency: All data is guaranteed to be compatible and consistently integrated
134
+
135
+ The unified file maintains the same structure and relationships as the separate
136
+ files, with all entities organized by type under their respective top-level keys,
137
+ while preserving all identifiers, references, and properties from the original
138
+ database.
139
+
140
+
141
+
142
+ == Usage: CLI
143
+
144
+ The UnitsDB gem includes a command-line utility for working with UnitsDB data.
145
+ This tool provides several commands for validating and normalizing UnitsDB
146
+ content.
147
+
148
+ === Installation
149
+
150
+ The `unitsdb` command is automatically installed when you install the gem.
151
+
152
+ === Available commands
153
+
154
+ ==== Database validation
155
+
156
+ The UnitsDB CLI provides several validation subcommands to ensure database
157
+ integrity and correctness. These commands help identify potential issues in the
158
+ database structure and content.
159
+
160
+ ===== References validation
161
+
162
+ Validates that all references within the database exist and point to actual entities:
163
+
164
+ [source,sh]
165
+ ----
166
+ # Validate all references
167
+ $ unitsdb validate references --database=/path/to/unitsdb/data
168
+
169
+ # Show valid references too (not just errors)
170
+ $ unitsdb validate references --print-valid --database=/path/to/unitsdb/data
171
+
172
+ # Show detailed registry contents for debugging
173
+ $ unitsdb validate references --debug-registry --database=/path/to/unitsdb/data
174
+ ----
175
+
176
+ This command checks all entity references (unit references, quantity references,
177
+ dimension references, etc.) to ensure they point to existing entities within the
178
+ database. It reports any "dangling" references that point to non-existent
179
+ entities, which could cause issues in applications using the database.
180
+
181
+ Options:
182
+
183
+ `--database`, `-d`:: Path to UnitsDB database (required)
184
+ `--debug_registry`:: Show registry contents for debugging
185
+ `--print_valid`:: Print valid references too, not just invalid ones
186
+
187
+ ===== Identifiers validation
188
+
189
+ Checks for uniqueness of identifier fields to prevent duplicate IDs:
190
+
191
+ [source,sh]
192
+ ----
193
+ $ unitsdb validate identifiers --database=/path/to/unitsdb/data
194
+ ----
195
+
196
+ This command ensures that each identifier within an entity type (units,
197
+ prefixes, quantities, etc.) is unique. Duplicate identifiers could lead to
198
+ ambiguity and unexpected behavior when referencing entities by ID.
199
+
200
+ Options:
201
+
202
+ `--database`, `-d`:: Path to UnitsDB database (required)
203
+
204
+ ===== SI references validation
205
+
206
+ Validates that each SI digital framework reference is unique per entity type:
207
+
208
+ [source,sh]
209
+ ----
210
+ $ unitsdb validate si_references --database=/path/to/unitsdb/data
211
+ ----
212
+
213
+ This command checks that each SI digital framework URI is referenced by at most
214
+ one entity of each type. Multiple entities of the same type referencing the same
215
+ SI URI could cause issues with mapping and conversion processes.
216
+
217
+ The command reports:
218
+
219
+ * Any duplicate SI references within each entity type
220
+ * The entities that share the same SI reference
221
+ * Their position in the database for easy location
222
+
223
+ Options:
224
+
225
+ `--database`, `-d`:: Path to UnitsDB database (required)
226
+
227
+ === Examples of validation commands
228
+
229
+ * Check identifiers for uniqueness:
230
+ +
231
+ [source,sh]
232
+ ----
233
+ $ unitsdb validate identifiers --database=/path/to/unitsdb/data
234
+ ----
235
+
236
+ * Validate references in a specific directory:
237
+ +
238
+ [source,sh]
239
+ ----
240
+ $ unitsdb validate references --database=/path/to/unitsdb/data
241
+ ----
242
+
243
+ * Check for duplicate SI references:
244
+ +
245
+ [source,sh]
246
+ ----
247
+ $ unitsdb validate si_references --database=/path/to/unitsdb/data
248
+ ----
249
+
250
+
251
+ ==== Database Modification (_modify)
252
+
253
+ Commands that modify the database are grouped under the `_modify` namespace:
254
+
255
+ [source,sh]
256
+ ----
257
+ # Normalize YAML file format
258
+ $ unitsdb _modify normalize [INPUT] [OUTPUT] --database=/path/to/unitsdb/data
259
+ $ unitsdb _modify normalize --all --database=/path/to/unitsdb/data
260
+
261
+ # Sort by different ID types
262
+ $ unitsdb _modify normalize --sort=nist [INPUT] [OUTPUT] --database=/path/to/unitsdb/data
263
+ $ unitsdb _modify normalize --sort=unitsml [INPUT] [OUTPUT] --database=/path/to/unitsdb/data
264
+ $ unitsdb _modify normalize --sort=short [INPUT] [OUTPUT] --database=/path/to/unitsdb/data
265
+ $ unitsdb _modify normalize --sort=none [INPUT] [OUTPUT] --database=/path/to/unitsdb/data
266
+ ----
267
+
268
+ Options:
269
+
270
+ `--all`, `-a`:: Process all YAML files in the repository
271
+ `--database`, `-d`:: Path to UnitsDB database (required)
272
+ `--sort`:: Sort units by: 'short' (name, default), 'nist' (ID), 'unitsml' (ID), or 'none'
273
+
274
+
275
+ ==== Search
276
+
277
+ Searches for entities in the database and displays ID and ID Type information for each result:
278
+
279
+ [source,sh]
280
+ ----
281
+ # Search by text content
282
+ $ unitsdb search meter --database=/path/to/unitsdb/data
283
+ $ unitsdb search meter --type=units --database=/path/to/unitsdb/data
284
+
285
+ # Search by ID
286
+ $ unitsdb search any-query --id=NISTu1 --database=/path/to/unitsdb/data
287
+ $ unitsdb search any-query --id=NISTu1 --id_type=nist --database=/path/to/unitsdb/data
288
+
289
+ # Output in different formats
290
+ $ unitsdb search meter --format=json --database=/path/to/unitsdb/data
291
+ $ unitsdb search kilo --format=yaml --database=/path/to/unitsdb/data
292
+ ----
293
+
294
+ Options:
295
+
296
+ `--type`, `-t`:: Entity type to search (units, prefixes, quantities, dimensions, unit_systems)
297
+ `--id`, `-i`:: Search for an entity with a specific identifier
298
+ `--id_type`:: Filter the ID search by identifier type
299
+ `--format`:: Output format (text, json, yaml) - default is text
300
+ `--database`, `-d`:: Path to UnitsDB database (required)
301
+
302
+ ==== Get
303
+
304
+ Retrieves and displays the full details of a specific entity by its identifier:
305
+
306
+ [source,sh]
307
+ ----
308
+ # Get entity details by ID
309
+ $ unitsdb get meter --database=/path/to/unitsdb/data
310
+ $ unitsdb get m --database=/path/to/unitsdb/data
311
+
312
+ # Get entity with specific ID type
313
+ $ unitsdb get meter --id_type=si --database=/path/to/unitsdb/data
314
+
315
+ # Output in different formats
316
+ $ unitsdb get kilogram --format=json --database=/path/to/unitsdb/data
317
+ $ unitsdb get second --format=yaml --database=/path/to/unitsdb/data
318
+ ----
319
+
320
+ Options:
321
+
322
+ `--id_type`:: Filter the search by identifier type
323
+ `--format`:: Output format (text, json, yaml) - default is text
324
+ `--database`, `-d`:: Path to UnitsDB database (required)
325
+
326
+ ==== Check references to SI Digital Framework
327
+
328
+ Performs a comprehensive check of entities in the BIPM's SI digital framework
329
+ TTL files against UnitsDB database entities.
330
+
331
+ This combined command checks in both directions to ensure UnitsDB is a strict
332
+ superset of the SI digital framework:
333
+
334
+ * From SI to UnitsDB: Ensures every TTL entity is referenced by at least one
335
+ UnitsDB entity
336
+
337
+ * From UnitsDB to SI: Identifies UnitsDB entities that should reference TTL
338
+ entities
339
+
340
+ [source,sh]
341
+ ----
342
+ # Check all entity types and generate a report
343
+ $ unitsdb check_si --database=spec/fixtures/unitsdb --ttl-dir=spec/fixtures/bipm-si-ttl
344
+
345
+ # Check a specific entity type (units, quantities, or prefixes)
346
+ $ unitsdb check_si --entity-type=units \
347
+ --database=spec/fixtures/unitsdb \
348
+ --ttl-dir=spec/fixtures/bipm-si-ttl
349
+
350
+ # Check in a specific direction only
351
+ $ unitsdb check_si --direction=from_si \
352
+ --database=spec/fixtures/unitsdb \
353
+ --ttl-dir=spec/fixtures/bipm-si-ttl
354
+
355
+ # Update references and write to output directory
356
+ $ unitsdb check_si --output-updated-database=new_unitsdb \
357
+ --database=spec/fixtures/unitsdb \
358
+ --ttl-dir=spec/fixtures/bipm-si-ttl
359
+
360
+ # Include potential matches when updating references (default: false)
361
+ $ unitsdb check_si --include-potential-matches \
362
+ --output-updated-database=new_unitsdb \
363
+ --database=spec/fixtures/unitsdb \
364
+ --ttl-dir=spec/fixtures/bipm-si-ttl
365
+ ----
366
+
367
+ Options:
368
+
369
+ `--database`, `-d`:: Path to UnitsDB database (required)
370
+
371
+ `--ttl-dir`, `-t`:: Path to the directory containing SI digital framework TTL
372
+ files (required)
373
+
374
+ `--entity-type`, `-e`:: Entity type to check (units, quantities, or prefixes).
375
+ If not specified, all types are checked
376
+
377
+ `--output-updated-database`, `-o`:: Directory path to write updated YAML files
378
+ with added SI references
379
+
380
+ `--direction`, `-r`:: Direction to check: 'to_si' (UnitsDB→TTL), 'from_si'
381
+ (TTL→UnitsDB), or 'both' (default)
382
+
383
+ `--include-potential-matches`, `-p`:: Include potential matches when updating
384
+ references (default: false)
385
+
386
+ ==== Release
387
+
388
+ Creates release files for UnitsDB in unified formats:
389
+
390
+ [source,sh]
391
+ ----
392
+ # Create both unified YAML and ZIP archive
393
+ $ unitsdb release --database=/path/to/unitsdb/data
394
+
395
+ # Create only unified YAML file
396
+ $ unitsdb release --format=yaml --database=/path/to/unitsdb/data
397
+
398
+ # Create only ZIP archive
399
+ $ unitsdb release --format=zip --database=/path/to/unitsdb/data
400
+
401
+ # Specify output directory
402
+ $ unitsdb release --output-dir=/path/to/output --database=/path/to/unitsdb/data
403
+ ----
404
+
405
+ This command creates release files for UnitsDB in two formats:
406
+
407
+ . A unified YAML file that combines all database files into a single file
408
+
409
+ . A ZIP archive containing all individual database files
410
+
411
+ The command verifies that all files have the same schema version before creating
412
+ the release files. The output files are named with the schema version (e.g.,
413
+ `unitsdb-2.0.0.yaml` and `unitsdb-2.0.0.zip`).
414
+
415
+ Options:
416
+
417
+ `--format`, `-f`:: Output format: 'yaml' (single file), 'zip' (archive), or
418
+ 'all' (both). Default is 'all'.
419
+
420
+ `--output-dir`, `-o`:: Directory to output release files. Default is current
421
+ directory.
422
+
423
+ `--database`, `-d`:: Path to UnitsDB database (required)
424
+
425
+ ===== Match types in check_si
426
+
427
+ The `check_si` command classifies matches into two categories:
428
+
429
+ **Exact matches**::
430
+ These are high-confidence matches based on exact name or label equivalence.
431
+
432
+ ** `short_to_name`: UnitsDB short name matches SI name
433
+ ** `short_to_label`: UnitsDB short name matches SI label
434
+ ** `name_to_name`: UnitsDB name matches SI name
435
+ ** `name_to_label`: UnitsDB name matches SI label
436
+ ** `name_to_alt_label`: UnitsDB name matches SI alternative label
437
+
438
+ **Potential matches**::
439
+ These are lower-confidence matches that require manual verification.
440
+
441
+ ** `symbol_match`: Only the symbols match, not the names
442
+ ** `partial_match`: Incomplete match (e.g., "sidereal_day" vs "day")
443
+
444
+ When using `--include-potential-matches`, both exact and potential matches will
445
+ be included in the reference updates. Without this flag, only exact matches are
446
+ used for automatic updates.
447
+
448
+ ===== SI References Workflow
449
+
450
+ When the BIPM updates their SI Digital Reference TTL files, follow these steps
451
+ to ensure UnitsDB remains a strict superset:
452
+
453
+ . Verify unreferenced TTL entries:
454
+
455
+ ** Run this:
456
+ +
457
+ [source,sh]
458
+ ----
459
+ $ unitsdb check_si --database=/path/to/unitsdb/data --ttl-dir=/path/to/si-framework
460
+ ----
461
+
462
+ ** Look for entries in the "SI [Entity Type] not mapped to our database" section
463
+
464
+ ** These are TTL entities that are not currently referenced by any UnitsDB entity
465
+
466
+ . For each unreferenced TTL entry:
467
+
468
+ ** Search for matching entities in UnitsDB:
469
+ +
470
+ [source,sh]
471
+ ----
472
+ $ unitsdb search "entity_name" --database=/path/to/unitsdb/data
473
+ ----
474
+
475
+ ** If a match exists:
476
+
477
+ *** Update its references manually in the appropriate YAML file
478
+ *** Add a new reference with `authority: "si-digital-framework"` and the TTL URI
479
+
480
+ ** If no match exists:
481
+
482
+ *** Create a new entity in the appropriate YAML file (`units.yaml`,
483
+ `quantities.yaml`, or `prefixes.yaml`)
484
+
485
+ *** Include the necessary reference to the TTL entity
486
+
487
+ . Verify all references are complete:
488
+
489
+ ** Run this again:
490
+ +
491
+ [source,sh]
492
+ ----
493
+ $ unitsdb check_si --database=/path/to/unitsdb/data --ttl-dir=/path/to/si-framework
494
+ ----
495
+
496
+ ** Confirm no entries appear in the "SI [Entity Type] not mapped to our database" section
497
+
498
+ ** If needed, run with the output option to automatically add missing references:
499
+ +
500
+ [source,sh]
501
+ ----
502
+ $ unitsdb check_si --output-updated-database=/path/to/output/dir \
503
+ --database=/path/to/unitsdb/data \
504
+ --ttl-dir=/path/to/si-framework
505
+ ----
506
+
507
+ . Verify reference uniqueness:
508
+
509
+ ** Run:
510
+ +
511
+ [source,sh]
512
+ ----
513
+ $ unitsdb validate si_references --database=/path/to/unitsdb/data
514
+ ----
515
+
516
+ ** This checks that each SI URI is used by at most one entity of each type
517
+
518
+ ** Fix any duplicate references found
519
+
520
+ The `check_si` command ensures every entity in the BIPM's SI Digital Reference
521
+ is properly referenced in UnitsDB:
522
+
523
+ * It verifies that every TTL entity has at least one corresponding UnitsDB
524
+ entity referencing it
525
+
526
+ * It identifies UnitsDB entities that should reference SI Digital Framework but
527
+ don't yet
528
+
529
+ * It can automatically update YAML files with proper references when used with
530
+ the `--output-updated-database` option
531
+
532
+ * It correctly differentiates between exact and potential matches, with
533
+ symbol-to-symbol and partial matches always classified as potential
534
+
535
+
536
+ == Usage: Ruby
537
+
538
+ === Loading the database
539
+
540
+ The primary way to load the UnitsDB data is through the `Database.from_db`
541
+ method, which reads data from YAML files:
542
+
543
+ [source,ruby]
544
+ ----
545
+ require 'unitsdb'
546
+
547
+ # Load from the UnitsDB data directory
548
+ db = Unitsdb::Database.from_db('/path/to/unitsdb/data')
549
+
550
+ # Access different collections
551
+ units = db.units
552
+ prefixes = db.prefixes
553
+ dimensions = db.dimensions
554
+ quantities = db.quantities
555
+ unit_systems = db.unit_systems
556
+ ----
557
+
558
+ === Database search methods
559
+
560
+ The UnitsDB Ruby gem provides several methods for searching and retrieving
561
+ entities.
562
+
563
+ ==== Search by text content
564
+
565
+ The `search` method allows you to find entities containing specific text in
566
+ their identifiers, names, or descriptions:
567
+
568
+ [source,ruby]
569
+ ----
570
+ # Search across all entity types
571
+ results = db.search(text: "meter")
572
+
573
+ # Search within a specific entity type
574
+ units_with_meter = db.search(text: "meter", type: "units")
575
+ ----
576
+
577
+ ==== Find entity by ID
578
+
579
+ The `get_by_id` method finds an entity with a specific identifier across all
580
+ entity types:
581
+
582
+ [source,ruby]
583
+ ----
584
+ # Find by ID across all entity types
585
+ meter_entity = db.get_by_id(id: "NISTu1")
586
+
587
+ # Find by ID with specific identifier type
588
+ meter_entity = db.get_by_id(id: "NISTu1", type: "nist")
589
+ ----
590
+
591
+ ==== Find entity by ID within a specific type collection
592
+
593
+ The `find_by_type` method searches for an entity by ID within a specific entity
594
+ type collection:
595
+
596
+ [source,ruby]
597
+ ----
598
+ # Find unit with specific ID
599
+ meter_unit = db.find_by_type(id: "NISTu1", type: "units")
600
+ ----
601
+
602
+ ==== Find entities by symbol
603
+
604
+ The `find_by_symbol` method allows you to search for units and prefixes by their
605
+ symbol representation:
606
+
607
+ [source,ruby]
608
+ ----
609
+ # Find all entities with symbol "m"
610
+ matching_entities = db.find_by_symbol("m")
611
+
612
+ # Find only units with symbol "m"
613
+ matching_units = db.find_by_symbol("m", "units")
614
+
615
+ # Find only prefixes with symbol "k"
616
+ matching_prefixes = db.find_by_symbol("k", "prefixes")
617
+ ----
618
+
619
+ This method performs case-insensitive exact matches on the ASCII representation
620
+ of symbols. It's useful for finding units or prefixes when you know the symbol
621
+ but not the name or identifier.
622
+
623
+ Parameters:
624
+
625
+ `symbol` (String)::
626
+ The symbol to search for
627
+
628
+ `entity_type` (String, Symbol, nil)::
629
+ Optional. Limit search to a specific entity type ("units" or "prefixes"). If
630
+ nil, searches both.
631
+
632
+ Returns:
633
+
634
+ * An array of entities (Unit or Prefix objects) with matching symbols
635
+ * Empty array if no matches are found
636
+
637
+ NOTE: This method only searches units and prefixes, as these are the only entity
638
+ types that have symbol representations.
639
+
640
+ === Main classes
641
+
642
+ The UnitsDB Ruby gem provides the following main classes.
643
+
644
+ ==== Database
645
+
646
+ The `Database` class is the main container that holds all UnitsML components. It
647
+ loads and provides access to units, prefixes, dimensions, quantities, and unit
648
+ systems.
649
+
650
+ [source,ruby]
651
+ ----
652
+ # Access database collections
653
+ db.units # => Array of Unit objects
654
+ db.prefixes # => Array of Prefix objects
655
+ db.dimensions # => Array of Dimension objects
656
+ db.quantities # => Array of Quantity objects
657
+ db.unit_systems # => Array of UnitSystem objects
658
+ ----
659
+
660
+ ==== Unit
661
+
662
+ The `Unit` class represents units of measure with their properties and
663
+ relationships:
664
+
665
+ * Identifiers
666
+ * Short name
667
+ * Whether it's a root unit or can be prefixed
668
+ * Dimension reference
669
+ * Unit system references
670
+ * Unit names
671
+ * Symbol presentations
672
+ * Quantity references
673
+ * SI derived bases
674
+ * Root unit references
675
+
676
+ ==== Prefix
677
+
678
+ The `Prefix` class represents prefixes for units (like kilo-, mega-, etc.):
679
+
680
+ * Identifiers
681
+ * Name
682
+ * Symbol presentations
683
+ * Base (e.g., 10)
684
+ * Power (e.g., 3 for kilo)
685
+
686
+ ==== Dimension
687
+
688
+ The `Dimension` class represents physical dimensions (like length, mass, etc.):
689
+
690
+ * Identifiers
691
+ * Whether it's dimensionless
692
+ * Basic dimensions (length, mass, time, etc.)
693
+ * Dimension details (power, symbol, dimension symbols)
694
+ * Short name
695
+
696
+ ==== UnitSystem
697
+
698
+ The `UnitSystem` class represents systems of units (like SI, Imperial, etc.):
699
+
700
+ * Identifiers
701
+ * Name
702
+ * Short name
703
+ * Whether it's acceptable
704
+
705
+ ==== Quantity
706
+
707
+ The `Quantity` class represents physical quantities that can be measured:
708
+
709
+ * Identifiers
710
+ * Quantity type
711
+ * Quantity names
712
+ * Short name
713
+ * Unit references
714
+ * Dimension reference
715
+
716
+ === Database files
717
+
718
+ The `Database.from_db` method reads the following YAML files:
719
+
720
+ * `prefixes.yaml` - Contains prefix definitions (e.g., kilo-, mega-)
721
+ * `dimensions.yaml` - Contains dimension definitions (e.g., length, mass)
722
+ * `units.yaml` - Contains unit definitions (e.g., meter, kilogram)
723
+ * `quantities.yaml` - Contains quantity definitions (e.g., length, mass)
724
+ * `unit_systems.yaml` - Contains unit system definitions (e.g., SI, Imperial)
725
+
30
726
 
31
727
 
32
728
 
data/exe/unitsdb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "unitsdb"
5
+ require "unitsdb/cli"
6
+
7
+ Unitsdb::CLI.start(ARGV)