unit_measurements_us_complete 5.17.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 (91) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/main.yml +31 -0
  3. data/.github/workflows/pages.yml +31 -0
  4. data/.gitignore +12 -0
  5. data/.rspec +3 -0
  6. data/.yardopts +2 -0
  7. data/CHANGELOG.md +645 -0
  8. data/Gemfile +7 -0
  9. data/Gemfile.lock +56 -0
  10. data/LICENSE.md +21 -0
  11. data/README.md +511 -0
  12. data/Rakefile +10 -0
  13. data/lib/unit_measurements/arithmetic.rb +225 -0
  14. data/lib/unit_measurements/base.rb +177 -0
  15. data/lib/unit_measurements/cache.rb +173 -0
  16. data/lib/unit_measurements/comparison.rb +64 -0
  17. data/lib/unit_measurements/configuration.rb +64 -0
  18. data/lib/unit_measurements/conversion.rb +92 -0
  19. data/lib/unit_measurements/errors/blank_quantity_error.rb +21 -0
  20. data/lib/unit_measurements/errors/blank_unit_error.rb +21 -0
  21. data/lib/unit_measurements/errors/missing_primitive_unit_error.rb +24 -0
  22. data/lib/unit_measurements/errors/parse_error.rb +37 -0
  23. data/lib/unit_measurements/errors/primitive_unit_already_set_error.rb +22 -0
  24. data/lib/unit_measurements/errors/unit_already_defined_error.rb +37 -0
  25. data/lib/unit_measurements/errors/unit_error.rb +36 -0
  26. data/lib/unit_measurements/extras/conversion_methods.rb +87 -0
  27. data/lib/unit_measurements/extras/numeric_methods.rb +85 -0
  28. data/lib/unit_measurements/formatter.rb +58 -0
  29. data/lib/unit_measurements/math.rb +120 -0
  30. data/lib/unit_measurements/measurement.rb +539 -0
  31. data/lib/unit_measurements/normalizer.rb +155 -0
  32. data/lib/unit_measurements/parser.rb +220 -0
  33. data/lib/unit_measurements/unit.rb +243 -0
  34. data/lib/unit_measurements/unit_group.rb +297 -0
  35. data/lib/unit_measurements/unit_group_builder.rb +224 -0
  36. data/lib/unit_measurements/unit_groups/acceleration.rb +36 -0
  37. data/lib/unit_measurements/unit_groups/all.rb +56 -0
  38. data/lib/unit_measurements/unit_groups/amount_of_substance.rb +13 -0
  39. data/lib/unit_measurements/unit_groups/angular_acceleration.rb +14 -0
  40. data/lib/unit_measurements/unit_groups/angular_velocity.rb +25 -0
  41. data/lib/unit_measurements/unit_groups/area.rb +49 -0
  42. data/lib/unit_measurements/unit_groups/catalytic_activity.rb +13 -0
  43. data/lib/unit_measurements/unit_groups/density.rb +33 -0
  44. data/lib/unit_measurements/unit_groups/dynamic_viscosity.rb +22 -0
  45. data/lib/unit_measurements/unit_groups/electric_charge.rb +20 -0
  46. data/lib/unit_measurements/unit_groups/electric_conductance.rb +15 -0
  47. data/lib/unit_measurements/unit_groups/electric_current.rb +19 -0
  48. data/lib/unit_measurements/unit_groups/electric_dipole_moment.rb +13 -0
  49. data/lib/unit_measurements/unit_groups/electric_potential.rb +17 -0
  50. data/lib/unit_measurements/unit_groups/electric_quadrupole_moment.rb +14 -0
  51. data/lib/unit_measurements/unit_groups/electrical_capacitance.rb +15 -0
  52. data/lib/unit_measurements/unit_groups/electrical_elastance.rb +13 -0
  53. data/lib/unit_measurements/unit_groups/electrical_inductance.rb +15 -0
  54. data/lib/unit_measurements/unit_groups/electrical_resistance.rb +16 -0
  55. data/lib/unit_measurements/unit_groups/energy.rb +58 -0
  56. data/lib/unit_measurements/unit_groups/force.rb +47 -0
  57. data/lib/unit_measurements/unit_groups/frequency.rb +16 -0
  58. data/lib/unit_measurements/unit_groups/illuminance.rb +18 -0
  59. data/lib/unit_measurements/unit_groups/information_entropy.rb +15 -0
  60. data/lib/unit_measurements/unit_groups/kinetic_viscosity.rb +18 -0
  61. data/lib/unit_measurements/unit_groups/length.rb +67 -0
  62. data/lib/unit_measurements/unit_groups/luminance.rb +21 -0
  63. data/lib/unit_measurements/unit_groups/luminous_flux.rb +11 -0
  64. data/lib/unit_measurements/unit_groups/luminous_intensity.rb +13 -0
  65. data/lib/unit_measurements/unit_groups/magnetic_field.rb +13 -0
  66. data/lib/unit_measurements/unit_groups/magnetic_flux.rb +15 -0
  67. data/lib/unit_measurements/unit_groups/magnetic_induction.rb +13 -0
  68. data/lib/unit_measurements/unit_groups/magnetomotive_force.rb +13 -0
  69. data/lib/unit_measurements/unit_groups/mass_flow_rate.rb +49 -0
  70. data/lib/unit_measurements/unit_groups/plane_angle.rb +30 -0
  71. data/lib/unit_measurements/unit_groups/power.rb +54 -0
  72. data/lib/unit_measurements/unit_groups/pressure.rb +60 -0
  73. data/lib/unit_measurements/unit_groups/quantity.rb +14 -0
  74. data/lib/unit_measurements/unit_groups/radiation_absorbed_dose.rb +14 -0
  75. data/lib/unit_measurements/unit_groups/radiation_equivalent_dose.rb +13 -0
  76. data/lib/unit_measurements/unit_groups/radiation_exposure.rb +15 -0
  77. data/lib/unit_measurements/unit_groups/radioactivity.rb +14 -0
  78. data/lib/unit_measurements/unit_groups/solid_angle.rb +18 -0
  79. data/lib/unit_measurements/unit_groups/sound_level.rb +13 -0
  80. data/lib/unit_measurements/unit_groups/temperature.rb +19 -0
  81. data/lib/unit_measurements/unit_groups/time.rb +29 -0
  82. data/lib/unit_measurements/unit_groups/torque.rb +40 -0
  83. data/lib/unit_measurements/unit_groups/velocity.rb +37 -0
  84. data/lib/unit_measurements/unit_groups/volume.rb +67 -0
  85. data/lib/unit_measurements/unit_groups/volumetric_flow_rate.rb +35 -0
  86. data/lib/unit_measurements/unit_groups/weight.rb +55 -0
  87. data/lib/unit_measurements/version.rb +8 -0
  88. data/lib/unit_measurements.rb +7 -0
  89. data/unit_measurements.gemspec +43 -0
  90. data/units.md +843 -0
  91. metadata +216 -0
@@ -0,0 +1,297 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ # The +UnitMeasurements::UnitGroup+ class provides a collection of units with
7
+ # methods to retrieve units by name, check if a unit is defined, and much more.
8
+ #
9
+ # It serves as a container for organizing and working with units within the
10
+ # unit group.
11
+ #
12
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
13
+ # @since 1.0.0
14
+ class UnitGroup
15
+ # The primitive unit of the unit group.
16
+ #
17
+ # @example
18
+ # UnitMeasurements::Length.primitive
19
+ # => #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
20
+ #
21
+ # @return [Unit] The primitive unit of the unit group.
22
+ #
23
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
24
+ # @since 1.0.0
25
+ attr_reader :primitive
26
+
27
+ # An array of units within the unit group.
28
+ #
29
+ # @example
30
+ # UnitMeasurements::Length.units
31
+ # => [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ...]
32
+ #
33
+ # @return [Array<Unit>] An array of +Unit+ instances.
34
+ #
35
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
36
+ # @since 1.0.0
37
+ attr_reader :units
38
+
39
+ # The name of the cache file.
40
+ #
41
+ # @example
42
+ # UnitMeasurements::Length.cache_file
43
+ # => "length.json"
44
+ #
45
+ # @return [String] The name of the cache file.
46
+ #
47
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
48
+ # @since 5.2.0
49
+ attr_reader :cache_file
50
+
51
+ # Initializes a new +UnitGroup+ instance.
52
+ #
53
+ # @param [String|Symbol, optional] primitive The name of the primitive unit.
54
+ # @param [Array<Unit>] units An array of +Unit+ instances.
55
+ # @param [String] cache_file The name of the cache file.
56
+ #
57
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
58
+ # @since 1.0.0
59
+ def initialize(primitive, units, cache_file)
60
+ @units = units.map { |unit| unit.with(unit_group: self) }
61
+ @primitive = unit_for!(primitive) if primitive
62
+ @cache_file = cache_file
63
+ end
64
+
65
+ # Returns the unit instance for a given unit name. It returns +nil+ if unit
66
+ # is not defined within the unit group.
67
+ #
68
+ # @example
69
+ # UnitMeasurements::Length.unit_for("m")
70
+ # => #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
71
+ #
72
+ # UnitMeasurements::Length.unit_for("z")
73
+ # => nil
74
+ #
75
+ # @param [String|Symbol] name The name of the unit.
76
+ #
77
+ # @return [Unit|NilClass] A +Unit+ instance or +nil+ if the unit is not found.
78
+ #
79
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
80
+ # @since 1.0.0
81
+ def unit_for(name)
82
+ unit_name_to_unit(name)
83
+ end
84
+
85
+ # This method works same as +unit_for+ but it raises +UnitError+ if the
86
+ # unit is not defined within the unit group.
87
+ #
88
+ # @example
89
+ # UnitMeasurements::Length.unit_for!("m")
90
+ # => #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
91
+ #
92
+ # UnitMeasurements::Length.unit_for!("z")
93
+ # => Invalid unit: 'z'. (UnitMeasurements::UnitError)
94
+ #
95
+ # @param [String|Symbol] name The name of the unit to look for.
96
+ #
97
+ # @return [Unit] A +Unit+ instance.
98
+ #
99
+ # @raise [UnitError] If the unit is not found.
100
+ #
101
+ # @see #unit_for
102
+ # @see UnitError
103
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
104
+ # @since 1.0.0
105
+ def unit_for!(name)
106
+ unit = unit_for(name)
107
+ raise UnitError, name unless unit
108
+
109
+ unit
110
+ end
111
+ alias_method :[], :unit_for!
112
+
113
+ # Returns an array of names of all the units defined within the unit group,
114
+ # sorted alphabetically.
115
+ #
116
+ # @example
117
+ # UnitMeasurements::Length.unit_names
118
+ # => ["ft", "in", "m", "mi", "yd"]
119
+ #
120
+ # @return [Array<String>] An array of unit names.
121
+ #
122
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
123
+ # @since 1.0.0
124
+ def unit_names
125
+ units.map(&:name).sort
126
+ end
127
+
128
+ # Returns an array of names and aliases of all the units defined within the
129
+ # unit group, sorted alphabetically.
130
+ #
131
+ # @example
132
+ # UnitMeasurements::Length.unit_names_with_aliases
133
+ # => ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
134
+ #
135
+ # @return [Array<String>] An array of unit names and aliases.
136
+ #
137
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
138
+ # @since 1.0.0
139
+ def unit_names_with_aliases
140
+ units.flat_map(&:names).sort
141
+ end
142
+
143
+ # Checks if a unit with a given name is defined within the unit group.
144
+ #
145
+ # @example
146
+ # UnitMeasurements::Length.defined?("m")
147
+ # => true
148
+ #
149
+ # UnitMeasurements::Length.defined?("metre")
150
+ # => false
151
+ #
152
+ # @param [String|Symbol] name The name of the unit to look for.
153
+ #
154
+ # @return [TrueClass|FalseClass] +true+
155
+ # if the unit is defined, +false+ otherwise.
156
+ #
157
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
158
+ # @since 1.0.0
159
+ def defined?(name)
160
+ unit = unit_for(name)
161
+
162
+ unit ? unit.name == name.to_s : false
163
+ end
164
+
165
+ # Checks if a given name corresponds to a defined unit or an alias of any
166
+ # defined unit.
167
+ #
168
+ # @example
169
+ # UnitMeasurements::Length.unit_or_alias?("m")
170
+ # => true
171
+ #
172
+ # UnitMeasurements::Length.unit_or_alias?("metre")
173
+ # => true
174
+ #
175
+ # @param [String|Symbol] name The name or alias of the unit to look for.
176
+ #
177
+ # @return [TrueClass|FalseClass] +true+
178
+ # if the name corresponds to a unit or alias, +false+ otherwise.
179
+ #
180
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
181
+ # @since 1.0.0
182
+ def unit_or_alias?(name)
183
+ !!unit_for(name)
184
+ end
185
+
186
+ # Returns an array of units associated with a specified +unit_system+.
187
+ #
188
+ # This method takes a unit system name as an argument and filters the units
189
+ # in the unit group to return only those units that belong to the specified
190
+ # unit system. It then returns an array containing these filtered units. If
191
+ # there are no units associated with unit system, it returns empty array.
192
+ #
193
+ # @example
194
+ # UnitMeasurements::Length.units_for("metric")
195
+ # => [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>]
196
+ #
197
+ # UnitMeasurements::Length.units_for("imperial")
198
+ # => [#<UnitMeasurements::Unit: in (", inch, inches)>, ...]
199
+ #
200
+ # UnitMeasurements::Length.units_for("troy")
201
+ # => []
202
+ #
203
+ # @param [String|Symbol] system_name
204
+ # The name of the unit system to retrieve units for.
205
+ #
206
+ # @return [Array<Unit>]
207
+ # An array of +Unit+ instances associated with the specified unit system.
208
+ #
209
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
210
+ # @since 5.0.0
211
+ def units_for(system_name)
212
+ units.select { |unit| unit.system.to_s == system_name.to_s }
213
+ end
214
+
215
+ # This method works same as {units_for} method but it raises an error if
216
+ # the unit system +system_name+ is not defined within the unit group.
217
+ #
218
+ # @example
219
+ # UnitMeasurements::Length.units_for!("metric")
220
+ # => [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>]
221
+ #
222
+ # UnitMeasurements::Length.units_for!("imperial")
223
+ # => [#<UnitMeasurements::Unit: in (", inch, inches)>, ...]
224
+ #
225
+ # UnitMeasurements::Length.units_for!("troy")
226
+ # => Invalid unit system 'troy' within the unit group. (UnitMeasurements::BaseError)
227
+ #
228
+ # @param [String|Symbol] system_name
229
+ # The name of the unit system to retrieve units for.
230
+ #
231
+ # @return [Array<Unit>]
232
+ # An array of +Unit+ instances associated with the specified unit system.
233
+ #
234
+ # @raise [RuntimeError]
235
+ # If unit system is not defined within the unit group.
236
+ #
237
+ # @see #units_for
238
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
239
+ # @since 5.0.0
240
+ def units_for!(system_name)
241
+ unless systems.include?(system_name.to_s)
242
+ raise "Invalid unit system '#{system_name}' within the unit group."
243
+ end
244
+
245
+ units_for(system_name)
246
+ end
247
+
248
+ # Returns an array of unit system names defined within the unit group.
249
+ #
250
+ # It scans through the units and compiles a list of unique system names.
251
+ #
252
+ # @example
253
+ # UnitMeasurements::Length.systems
254
+ # => ["metric", "imperial", "us_customary", "astronomical"]
255
+ #
256
+ # @return [Array<String>] An array of unit system names.
257
+ #
258
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
259
+ # @since 5.11.0
260
+ def systems
261
+ units.map { _1.system }.uniq
262
+ end
263
+
264
+ private
265
+
266
+ # @private
267
+ # Returns a hash where keys are unit names (including aliases) and values
268
+ # are corresponding +Unit+ instances.
269
+ #
270
+ # @example
271
+ # UnitMeasurements::Length.unit_with_name_and_aliases
272
+ # => {"m"=>#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ...}
273
+ #
274
+ # @return [Hash] A hash containing unit +names+ as keys and +Unit+ objects as values.
275
+ #
276
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
277
+ # @since 1.0.0
278
+ def unit_with_name_and_aliases
279
+ units.each_with_object({}) do |unit, hash|
280
+ unit.names.each { |name| hash[name.to_s] = unit }
281
+ end
282
+ end
283
+
284
+ # @private
285
+ # Returns the +Unit+ instance for a given unit name.
286
+ #
287
+ # @param [String|Symbol] name The name of the unit.
288
+ #
289
+ # @return [Unit|NilClass] A +Unit+ instance or +nil+ if the unit is not found.
290
+ #
291
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
292
+ # @since 1.0.0
293
+ def unit_name_to_unit(name)
294
+ unit_with_name_and_aliases[name.to_s]
295
+ end
296
+ end
297
+ end
@@ -0,0 +1,224 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ # The +UnitMeasurements::UnitGroupBuilder+ class provides a flexible and
7
+ # configurable way to define units and create unit groups with specific systems
8
+ # and primitive unit.
9
+ #
10
+ # It provides methods like +primitive+, +system+, +unit+, and +si_unit+ to define
11
+ # units and their conversions within the unit group.
12
+ #
13
+ # @see UnitGroup
14
+ # @see PrimitiveUnitAlreadySetError
15
+ # @see UnitAlreadyDefinedError
16
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
17
+ # @since 1.0.0
18
+ class UnitGroupBuilder
19
+ # An array to store the units defined using the builder.
20
+ #
21
+ # @return [Array<Unit>] An array of +Unit+ instances.
22
+ #
23
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
24
+ # @since 1.0.0
25
+ attr_reader :units
26
+
27
+ # Initializes a new +UnitGroupBuilder+ instance.
28
+ #
29
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
30
+ # @since 1.0.0
31
+ def initialize
32
+ @units = []
33
+ end
34
+
35
+ # Defines a +unit+ and adds it to the list of units.
36
+ #
37
+ # @example
38
+ # unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]
39
+ #
40
+ # @param [String|Symbol] name The name of the unit.
41
+ # @param [Numeric|String] value The conversion value of the unit.
42
+ # @param [Array<String|Symbol>, optional] aliases An array of alternative names for the unit.
43
+ #
44
+ # @return [Unit] An instance of +Unit+.
45
+ #
46
+ # @see #build_unit
47
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
48
+ # @since 1.0.0
49
+ def unit(name, value: 1.0, aliases: [])
50
+ @units << build_unit(name, value: value, aliases: aliases)
51
+ end
52
+
53
+ # Builds a set of +SI units+ and adds them to the list of units. This method
54
+ # defines unit defined using +name+ along with all the SI prefixes.
55
+ #
56
+ # @example
57
+ # si_unit "m", aliases: ["meter", "metre", "meters", "metres"]
58
+ #
59
+ # @param [String|Symbol] name The name of the unit.
60
+ # @param [Numeric|String] value The conversion value of the unit.
61
+ # @param [TrueClass|FalseClass] add_binary_prefixes
62
+ # Whether the unit supports binary SI prefixes along with decimal SI
63
+ # prefixes.
64
+ # @param [Array<String|Symbol>, optional] aliases An array of alternative names for the unit.
65
+ #
66
+ # @return [Array<Unit>] An array of +Unit+ instances.
67
+ #
68
+ # @see #build_si_units
69
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
70
+ # @since 1.0.0
71
+ def si_unit(name, value: 1.0, add_binary_prefixes: false, aliases: [])
72
+ @units += build_si_units(name, value: value, add_binary_prefixes: add_binary_prefixes, aliases: aliases)
73
+ end
74
+
75
+ # Constructs and returns a +UnitGroup+ instance based on the units defined
76
+ # using the builder.
77
+ #
78
+ # @return [UnitGroup]
79
+ # An instance of +UnitGroup+ containing units and their conversions.
80
+ #
81
+ # @see UnitGroup
82
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
83
+ # @since 1.0.0
84
+ def build
85
+ UnitGroup.new(@primitive, @units, @cache_file)
86
+ end
87
+
88
+ # Defines the +unit system+ within the unit group and evaluates the provided
89
+ # block in the context of the builder.
90
+ #
91
+ # This method is used to group multiple units within the certain unit system,
92
+ # viz., metric, imperial, etc.
93
+ #
94
+ # @example Defining an imperial system with inches and feet:
95
+ # system :imperial do
96
+ # unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]
97
+ # unit "ft", value: "12 in", aliases: ["'", "foot", "feet"]
98
+ # end
99
+ #
100
+ # @param [String|Symbol] system_name
101
+ # The name of the unit system in which the units are to be grouped.
102
+ # @param block A block of code to be executed in the context of the builder.
103
+ #
104
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
105
+ # @since 4.0.0
106
+ def system(system_name, &block)
107
+ @system = system_name
108
+ instance_eval(&block) if block_given?
109
+ ensure
110
+ @system = nil
111
+ end
112
+
113
+ # Sets the +primitive+ unit for the unit group. It raises +PrimitiveUnitAlreadySetError+
114
+ # if the primitive unit has already been set for the unit group.
115
+ #
116
+ # @example
117
+ # primitive "m"
118
+ #
119
+ # @param [String|Symbol] primitive The name of the primitive unit.
120
+ #
121
+ # @raise [PrimitiveUnitAlreadySetError]
122
+ # If the primitive unit has already been set for the unit group.
123
+ #
124
+ # @see PrimitiveUnitAlreadySetError
125
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
126
+ # @since 4.0.0
127
+ def primitive(primitive)
128
+ raise PrimitiveUnitAlreadySetError if @primitive
129
+
130
+ @primitive = primitive
131
+ end
132
+
133
+ # Sets the name of the cache file for the unit group.
134
+ #
135
+ # @example
136
+ # cache "conversion_cache.json"
137
+ #
138
+ # @param [String] cache_file The name of the cache file.
139
+ #
140
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
141
+ # @since 5.2.0
142
+ def cache(cache_file)
143
+ @cache_file = cache_file
144
+ end
145
+
146
+ private
147
+
148
+ # @private
149
+ # Builds an array of +Unit+ instances with one instance of +Unit+ with name
150
+ # +name+ along with all SI prefixed +Unit+ instances for it.
151
+ #
152
+ # @param [String|Symbol] name The name of the unit.
153
+ # @param [Numeric|String] value The conversion value of the unit.
154
+ # @param [TrueClass|FalseClass] add_binary_prefixes
155
+ # Whether the unit supports binary SI prefixes along with decimal SI
156
+ # prefixes.
157
+ # @param [Array<String|Symbol>, optional] aliases
158
+ # An array of alternative names for the unit.
159
+ #
160
+ # @return [Array<Unit>] An array of +Unit+ instances.
161
+ #
162
+ # @see #build_unit
163
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
164
+ # @since 1.0.0
165
+ def build_si_units(name, value:, add_binary_prefixes:, aliases:)
166
+ si_units = [build_unit(name, value: value, aliases: aliases)]
167
+
168
+ si_prefixes = add_binary_prefixes ? (Unit::SI_DECIMAL_PREFIXES + Unit::SI_BINARY_PREFIXES) : Unit::SI_DECIMAL_PREFIXES
169
+
170
+ si_prefixes.each do |short_prefix, long_prefix, multiplier|
171
+ si_aliases = long_prefix.product(aliases.to_a).flat_map do |prefix, unit|
172
+ aliases.map { |alias_unit| prefix + alias_unit.to_s }
173
+ end
174
+ si_units << build_unit("#{short_prefix}#{name}", value: "#{multiplier} #{name}", aliases: si_aliases)
175
+ end
176
+
177
+ si_units
178
+ end
179
+
180
+ # @private
181
+ # Builds an instance of +Unit+ with name +name+, and specified conversion
182
+ # value, and alternate names.
183
+ #
184
+ # @param [String|Symbol] name The name of the unit.
185
+ # @param [Numeric|String] value The conversion value of the unit.
186
+ # @param [Array<String|Symbol>, optional] aliases
187
+ # An array of alternative names for the unit.
188
+ #
189
+ # @return [Unit] An instance of +Unit+.
190
+ #
191
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
192
+ # @since 1.0.0
193
+ def build_unit(name, value:, aliases:)
194
+ unit = Unit.new(name, value: value, aliases: aliases, system: @system)
195
+ check_for_duplicate_unit_names!(unit)
196
+
197
+ unit
198
+ end
199
+
200
+ # @private
201
+ # Checks for duplicate unit names within the list of units.
202
+ #
203
+ # This method ensures that there are no duplicate unit names within the list
204
+ # of units. If a duplicate name is found, it raises a +UnitAlreadyDefinedError+.
205
+ #
206
+ # This method is used internally by the +UnitGroupBuilder+ class to build units
207
+ # and handle unit definitions.
208
+ #
209
+ # @param [Unit] unit The unit to be checked.
210
+ #
211
+ # @raise [UnitAlreadyDefinedError]
212
+ # If the unit name is already defined in the list of units.
213
+ #
214
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
215
+ # @since 1.0.0
216
+ def check_for_duplicate_unit_names!(unit)
217
+ names = @units.flat_map(&:names)
218
+
219
+ if names.any? { |name| unit.names.include?(name) }
220
+ raise UnitAlreadyDefinedError, unit.name
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ UnitMeasurements::Acceleration = UnitMeasurements.build do
6
+ primitive "m/s²"
7
+
8
+ system :metric do
9
+ unit "Gal", value: "1e-2 m/s²", aliases: ["gal", "galileo"]
10
+ unit "m/s²", aliases: ["m/s^2", "meter per second squared", "meters per second squared", "metre per second squared", "metres per second squared"]
11
+ end
12
+
13
+ system :imperial do
14
+ unit "in/s²", value: "0.0254 m/s²", aliases: ["in/s^2", "ips²", "inch per second squared", "inches per second squared"]
15
+ unit "ipm/s", value: "0.0004233333 m/s²", aliases: ["in/(min⋅s)", "inch per minute per second", "inches per minute per second"]
16
+ unit "iph/s", value: "7.055555e-6 m/s²", aliases: ["in/(h⋅s)", "inch per hour per second", "inches per hour per second"]
17
+
18
+ unit "mi/s²", value: "1609.344 m/s²", aliases: ["mi/s^2", "mps²", "mile per second squared", "miles per second squared"]
19
+ unit "mpm/s", value: "26.8224 m/s²", aliases: ["mi/(min⋅s)", "mile per minute per second", "miles per minute per second"]
20
+ unit "mph/s", value: "0.44704 m/s²", aliases: ["mi/(h⋅s)", "mile per hour per second", "miles per hour per second"]
21
+ end
22
+
23
+ system :foot_pound_second do
24
+ unit "ft/s²", value: "0.3048 m/s²", aliases: ["ft/s^2", "fps²", "foot per second squared", "feet per second squared"]
25
+ unit "fpm/s", value: "0.00508 m/s²", aliases: ["ft/(min⋅s)", "foot per minute per second", "feet per minute per second"]
26
+ unit "fph/s", value: "8.466667e-5 m/s²", aliases: ["ft/(h⋅s)", "foot per hour per second", "feet per hour per second"]
27
+ end
28
+
29
+ system :meteorology_aviation_maritime do
30
+ unit "Kn/s", value: "0.5144444 m/s²", aliases: ["knot per second", "knots per second"]
31
+ end
32
+
33
+ system :planck_units do
34
+ unit "aₚ", value: "5.5608e+51 m/s²", aliases: ["planck acceleration", "quantum acceleration"]
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ require_relative "amount_of_substance"
6
+ require_relative "electric_current"
7
+ require_relative "length"
8
+ require_relative "luminous_intensity"
9
+ require_relative "temperature"
10
+ require_relative "time"
11
+ require_relative "weight"
12
+
13
+ require_relative "area"
14
+ require_relative "volume"
15
+ require_relative "density"
16
+ require_relative "velocity"
17
+ require_relative "acceleration"
18
+ require_relative "angular_velocity"
19
+ require_relative "angular_acceleration"
20
+ require_relative "force"
21
+ require_relative "electric_potential"
22
+ require_relative "electric_charge"
23
+ require_relative "electric_conductance"
24
+ require_relative "electric_dipole_moment"
25
+ require_relative "electric_quadrupole_moment"
26
+ require_relative "electrical_capacitance"
27
+ require_relative "electrical_elastance"
28
+ require_relative "electrical_resistance"
29
+ require_relative "electrical_inductance"
30
+ require_relative "magnetic_flux"
31
+ require_relative "magnetic_induction"
32
+ require_relative "magnetic_field"
33
+ require_relative "catalytic_activity"
34
+ require_relative "pressure"
35
+ require_relative "torque"
36
+ require_relative "luminous_flux"
37
+ require_relative "illuminance"
38
+ require_relative "luminance"
39
+ require_relative "frequency"
40
+ require_relative "power"
41
+ require_relative "energy"
42
+ require_relative "dynamic_viscosity"
43
+ require_relative "kinetic_viscosity"
44
+ require_relative "magnetomotive_force"
45
+ require_relative "radioactivity"
46
+ require_relative "radiation_exposure"
47
+ require_relative "radiation_absorbed_dose"
48
+ require_relative "radiation_equivalent_dose"
49
+ require_relative "information_entropy"
50
+ require_relative "mass_flow_rate"
51
+ require_relative "volumetric_flow_rate"
52
+
53
+ require_relative "sound_level"
54
+ require_relative "plane_angle"
55
+ require_relative "solid_angle"
56
+ require_relative "quantity"
@@ -0,0 +1,13 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ UnitMeasurements::AmountOfSubstance = UnitMeasurements.build do
6
+ primitive "mol"
7
+
8
+ system :metric do
9
+ si_unit "mol", aliases: ["mole", "moles"]
10
+ end
11
+
12
+ unit "NA", value: "1.6605389210322e-24 mol", aliases: ["avogadro constant"]
13
+ end
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ UnitMeasurements::AngularAcceleration = UnitMeasurements.build do
6
+ primitive "rad/s²"
7
+
8
+ system :metric do
9
+ si_unit "rad/s²", aliases: ["rad/s^2", "radian per second squared", "radians per second squared"]
10
+
11
+ unit "deg/s²", value: [(Math::PI / 180), "rad/s²"], aliases: ["°/s²", "deg/s^2", "degree per second squared", "degrees per second squared"]
12
+ unit "gon/s²", value: [(Math::PI / 200), "rad/s²"], aliases: ["ᵍ/s²", "gon/s^2", "gradian per second squared", "gradians per second squared"]
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ UnitMeasurements::AngularVelocity = UnitMeasurements.build do
6
+ primitive "rad/s"
7
+
8
+ system :metric do
9
+ unit "rad/s", aliases: ["radian per second", "radians per second"]
10
+ unit "rad/min", value: "1/60 rad/s", aliases: ["radian per minute", "radians per minute"]
11
+ unit "rad/h", value: "1/60 rad/min", aliases: ["radian per hour", "radians per hour"]
12
+
13
+ unit "deg/s", value: [(Math::PI / 180), "rad/s"], aliases: ["°/s", "degree per second", "degrees per second"]
14
+ unit "deg/min", value: "1/60 deg/s", aliases: ["°/min", "degree per minute", "degrees per minute"]
15
+ unit "deg/h", value: "1/60 deg/min", aliases: ["°/h", "degree per hour", "degrees per hour"]
16
+
17
+ unit "gon/s", value: [(Math::PI / 200), "rad/s"], aliases: ["ᵍ/s", "gradian per second", "gradians per second"]
18
+ unit "gon/min", value: "1/60 gon/s", aliases: ["ᵍ/min", "gradian per minute", "gradians per minute"]
19
+ unit "gon/h", value: "1/60 gon/min", aliases: ["ᵍ/h", "gradian per hour", "gradians per hour"]
20
+ end
21
+
22
+ unit "rev/s", value: "360 deg/s", aliases: ["revolution per second", "revolutions per second"]
23
+ unit "rev/min", value: "1/60 rev/s", aliases: ["revolution per minute", "revolutions per minute"]
24
+ unit "rev/h", value: "1/60 rev/min", aliases: ["revolution per hour", "revolutions per hour"]
25
+ end