unit_measurements 2.6.2 → 3.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/{LICENSE → LICENSE.md} +1 -1
- data/README.md +18 -18
- data/lib/unit_measurements/measurement.rb +9 -1
- data/lib/unit_measurements/unit_group_builder.rb +0 -4
- data/lib/unit_measurements/unit_groups/area.rb +1 -1
- data/lib/unit_measurements/unit_groups/density.rb +1 -1
- data/lib/unit_measurements/unit_groups/quantity.rb +1 -1
- data/lib/unit_measurements/version.rb +1 -1
- data/unit_measurements.gemspec +1 -1
- data/units.md +16 -17
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b48c60ef57c1c3c3da286747f91effb58b512a5c827c1867ca6266446ff25722
|
4
|
+
data.tar.gz: 68e0a02544ba31a167ba99ebe94ba44e10876a3fb8922a65bae740becb770d22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bef997801950b327b2665be18acb6836598606a28860945a7486dfd50afb5583e43b0fee050c50e3955272c88de6cf933131c9fa6e0e760e4ced83a59d68b164
|
7
|
+
data.tar.gz: '09c708ffbc5fa6f74dd30e5b0feee871826b15508bdec4de5c84b75246712a0379d5266f1685defa6fb6b161ef3d14b25f28422fb70c8e3c0f3e13b775612b5b'
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## [3.0.0](https://github.com/shivam091/unit_measurements/compare/v2.6.2...v3.0.0) - 2023-08-25
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added method `Measurement.name` to get humanized name of the unit group.
|
6
|
+
- Raised `BaseError` in `Measurement.unit_group`.
|
7
|
+
- Added aliases `in`, `as`, `in!`, `as` for `convert_to` and `convert_to!` methods.
|
8
|
+
|
9
|
+
### What's changed
|
10
|
+
|
11
|
+
- Moved LICENSE to markdown file.
|
12
|
+
- Replaced `$LOAD_PATH` by shorthand `$:`
|
13
|
+
- Replaced `base` method by `unit`.
|
14
|
+
|
15
|
+
----------
|
16
|
+
|
1
17
|
## [2.6.2](https://github.com/shivam091/unit_measurements/compare/v2.6.1...v2.6.2) - 2023-08-25
|
2
18
|
|
3
19
|
### What's changed
|
data/Gemfile.lock
CHANGED
data/{LICENSE → LICENSE.md}
RENAMED
data/README.md
CHANGED
@@ -72,8 +72,9 @@ UnitMeasurements::Weight.new(1, :kg)
|
|
72
72
|
**Converting to other units:**
|
73
73
|
|
74
74
|
This gem allows you to convert among units of same unit group.
|
75
|
-
You can convert measurement to other unit using `#convert_to`
|
76
|
-
|
75
|
+
You can convert measurement to other unit using `#convert_to`
|
76
|
+
(aliased as `#to`, `#in`, and `as`) or `#convert_to!`
|
77
|
+
(aliased as `#to!`, `#in!`, and `as!`) methods.
|
77
78
|
|
78
79
|
You can use `#convert_to` as:
|
79
80
|
|
@@ -243,6 +244,13 @@ weight.unit
|
|
243
244
|
#=> #<UnitMeasurements::Unit: kg (kilogram, kilogramme, kilogrammes, kilograms)>
|
244
245
|
```
|
245
246
|
|
247
|
+
**See humanized name of the unit group:**
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
UnitMeasurements::Weight.name
|
251
|
+
#=> weight
|
252
|
+
```
|
253
|
+
|
246
254
|
**See all units of the unit group:**
|
247
255
|
|
248
256
|
```ruby
|
@@ -467,31 +475,23 @@ gem "unit_measurements", require: ["unit_measurements/base", "unit_measurements/
|
|
467
475
|
### Building new unit groups
|
468
476
|
|
469
477
|
This library provides simpler way to build your own unit groups. To build new unit group,
|
470
|
-
use `UnitMeasurements.build` in order to define
|
478
|
+
use `UnitMeasurements.build` in order to define units within it:
|
471
479
|
|
472
|
-
If
|
480
|
+
If the unit is supporting [si prefixes](#si-units-support), you can use `si_unit` method to build it.
|
481
|
+
If you build unit using `si_unit`, the unit will be added along with all SI prefixes for it.
|
473
482
|
|
474
483
|
```ruby
|
475
484
|
UnitMeasurements::Time = UnitMeasurements.build do
|
476
|
-
|
477
|
-
base :s, aliases: [:second, :seconds]
|
478
|
-
|
479
|
-
# Add other units to the group, along with their conversion multipliers against
|
480
|
-
# base unit.
|
481
|
-
unit :min, value: 60.0, aliases: [:minute, :minutes]
|
485
|
+
unit :s, aliases: [:second, :seconds]
|
482
486
|
|
483
|
-
#
|
484
|
-
|
485
|
-
unit :h, value: "60 min", aliases: [:hour, :hours]
|
487
|
+
# Add units to the group, along with their conversion multipliers.
|
488
|
+
unit :min, value: "60 s", aliases: [:hour, :hours]
|
486
489
|
|
487
490
|
# You can also specify unit value as an array.
|
488
|
-
unit :
|
491
|
+
unit :h, value: [60, :min], aliases: [:day, :days]
|
489
492
|
end
|
490
493
|
```
|
491
494
|
|
492
|
-
If the unit is supporting [si prefixes](#si-units-support), you can use `si_unit` method to build it.
|
493
|
-
If you build unit using `si_unit`, Base unit is automatically added to the group along with all SI prefixes for it.
|
494
|
-
|
495
495
|
```ruby
|
496
496
|
UnitMeasurements::Time = UnitMeasurements.build do
|
497
497
|
# Add a SI unit to the unit group
|
@@ -501,7 +501,7 @@ UnitMeasurements::Time = UnitMeasurements.build do
|
|
501
501
|
end
|
502
502
|
```
|
503
503
|
|
504
|
-
All units allow aliases, as long as they are unique. Unit
|
504
|
+
All units allow aliases, as long as they are unique. Unit names can be used to
|
505
505
|
define the unit as long as it is unique. All unit names are case sensitive.
|
506
506
|
|
507
507
|
### Namespaces
|
@@ -32,6 +32,8 @@ module UnitMeasurements
|
|
32
32
|
self.class.new((quantity * conversion_factor), target_unit)
|
33
33
|
end
|
34
34
|
alias_method :to, :convert_to
|
35
|
+
alias_method :in, :convert_to
|
36
|
+
alias_method :as, :convert_to
|
35
37
|
|
36
38
|
def convert_to!(target_unit)
|
37
39
|
measurement = convert_to(target_unit)
|
@@ -40,6 +42,8 @@ module UnitMeasurements
|
|
40
42
|
self
|
41
43
|
end
|
42
44
|
alias_method :to!, :convert_to!
|
45
|
+
alias_method :in!, :convert_to!
|
46
|
+
alias_method :as!, :convert_to!
|
43
47
|
|
44
48
|
def inspect(dump: false)
|
45
49
|
return super() if dump
|
@@ -64,7 +68,7 @@ module UnitMeasurements
|
|
64
68
|
extend Forwardable
|
65
69
|
|
66
70
|
def unit_group
|
67
|
-
raise "`Measurement` does not have a `unit_group` object. You cannot directly use `Measurement`. Instead, build a new unit group by calling `UnitMeasurements.build`."
|
71
|
+
raise BaseError, "`Measurement` does not have a `unit_group` object. You cannot directly use `Measurement`. Instead, build a new unit group by calling `UnitMeasurements.build`."
|
68
72
|
end
|
69
73
|
|
70
74
|
def_delegators :unit_group, :units, :unit_names, :unit_with_name_and_aliases,
|
@@ -78,6 +82,10 @@ module UnitMeasurements
|
|
78
82
|
target ? _parse(source).convert_to(target) : _parse(source)
|
79
83
|
end
|
80
84
|
|
85
|
+
def name
|
86
|
+
to_s.split("::").last.underscore.humanize.downcase
|
87
|
+
end
|
88
|
+
|
81
89
|
private
|
82
90
|
|
83
91
|
def _parse(string)
|
@@ -10,10 +10,6 @@ module UnitMeasurements
|
|
10
10
|
@units = []
|
11
11
|
end
|
12
12
|
|
13
|
-
def base(name, aliases: [])
|
14
|
-
@units << build_unit(name, value: 1.0, aliases: aliases)
|
15
|
-
end
|
16
|
-
|
17
13
|
def unit(name, value: 1.0, aliases: [])
|
18
14
|
@units << build_unit(name, value: value, aliases: aliases)
|
19
15
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
UnitMeasurements::Area = UnitMeasurements.build do
|
6
|
-
|
6
|
+
unit :m², aliases: [:"m^2", :"sq m", :"square meter", :"square meters", :"square metre", :"square metres"]
|
7
7
|
|
8
8
|
unit :km², value: "1e+6 m²", aliases: [:"km^2", :"sq km", :"square kilometer", :"square kilometers", :"square kilometre", :"square kilometres"]
|
9
9
|
unit :in², value: "0.00064516 m²", aliases: [:"in^2", :"sq in", :"square inch", :"square inches"]
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
UnitMeasurements::Density = UnitMeasurements.build do
|
6
|
-
|
6
|
+
unit :"g/m³", aliases: [:"g/m^3", :"g·m⁻³", :"gram per cubic meter", :"grams per cubic meter", :"gramme per cubic metre", :"grammes per cubic metre"]
|
7
7
|
|
8
8
|
unit :"g/l", value: "1 kg/m³", aliases: [:"g·l⁻¹", :"gram per liter", :"grams per liter", :"gramme per litre", :"grammes per litre"]
|
9
9
|
unit :"g/ml", value: "1000 g/l", aliases: [:"g·ml⁻¹", :"gram per milliliter", :"grams per milliliter", :"gramme per millilitre", :"grammes per millilitre"]
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
UnitMeasurements::Quantity = UnitMeasurements.build do
|
6
|
-
|
6
|
+
unit :pc, aliases: [:pcs, :piece, :pieces]
|
7
7
|
|
8
8
|
unit :pr, value: "2 pc", aliases: [:pair, :pairs]
|
9
9
|
unit :dz, value: "6 pr", aliases: [:doz, :dozen, :dozens]
|
data/unit_measurements.gemspec
CHANGED
data/units.md
CHANGED
@@ -4,8 +4,7 @@ As there are lots of units bundled with `unit_measurements`, we recommend you to
|
|
4
4
|
bundled units before converting your measurements.
|
5
5
|
|
6
6
|
**Notes:**
|
7
|
-
1.
|
8
|
-
2. Unit names suffixed with `*` support all [SI prefixes](README.md#si-units-support).
|
7
|
+
1. Unit names suffixed with `*` support all [SI prefixes](README.md#si-units-support).
|
9
8
|
|
10
9
|
Below are the units which are bundled in the unit_measurements.
|
11
10
|
|
@@ -15,7 +14,7 @@ These units are defined in `UnitMeasurements::Length`.
|
|
15
14
|
|
16
15
|
| # | Name | Aliases |
|
17
16
|
|:--|:--|:--|
|
18
|
-
|
|
17
|
+
| 1 | m* | meter, metre, meters, metres |
|
19
18
|
| 2 | in | ", inch, inches |
|
20
19
|
| 3 | ft | ', foot, feet |
|
21
20
|
| 4 | yd | yard, yards |
|
@@ -27,7 +26,7 @@ These units are defined in `UnitMeasurements::Weight`.
|
|
27
26
|
|
28
27
|
| # | Name | Aliases |
|
29
28
|
|--|--|--|
|
30
|
-
|
|
29
|
+
| 1 | g* | gram, grams, gramme, grammes |
|
31
30
|
| 2 | q | quintal, quintals |
|
32
31
|
| 3 | t | tonne, tonnes, metric tonne, metric tonnes |
|
33
32
|
|
@@ -37,7 +36,7 @@ These units are defined in `UnitMeasurements::Time`.
|
|
37
36
|
|
38
37
|
| # | Name | Aliases |
|
39
38
|
|:--|:--|:--|
|
40
|
-
|
|
39
|
+
| 1 | s* | sec, second, seconds |
|
41
40
|
| 2 | h | hr, hour, hours |
|
42
41
|
| 3 | d | day, days |
|
43
42
|
| 4 | wk | week, weeks |
|
@@ -55,7 +54,7 @@ These units are defined in `UnitMeasurements::AmountOfSubstance`.
|
|
55
54
|
|
56
55
|
| # | Name | Aliases |
|
57
56
|
|:--|:--|:--|
|
58
|
-
|
|
57
|
+
| 1 | mol* | mole, moles |
|
59
58
|
| 2 | NA | avogadro constant |
|
60
59
|
|
61
60
|
## 5. Electric current
|
@@ -64,7 +63,7 @@ These units are defined in `UnitMeasurements::ElectricCurrent`.
|
|
64
63
|
|
65
64
|
| # | Name | Aliases |
|
66
65
|
|:--|:--|:--|
|
67
|
-
|
|
66
|
+
| 1 | A* | amp, ampere, amperes |
|
68
67
|
| 2 | abA | abampere, abamperes |
|
69
68
|
| 3 | Bi | biot, biots |
|
70
69
|
| 4 | statA | statampere, statamperes |
|
@@ -75,7 +74,7 @@ These units are defined in `UnitMeasurements::LuminousIntensity`.
|
|
75
74
|
|
76
75
|
| # | Name | Aliases |
|
77
76
|
|:--|:--|:--|
|
78
|
-
|
|
77
|
+
| 1 | cd* | candela, candelas |
|
79
78
|
| 2 | hk | hefnerkerze |
|
80
79
|
|
81
80
|
## 7. Temperature
|
@@ -84,7 +83,7 @@ These units are defined in `UnitMeasurements::Temperature`.
|
|
84
83
|
|
85
84
|
| # | Name | Aliases |
|
86
85
|
|:--|:--|:--|
|
87
|
-
|
|
86
|
+
| 1 | K* | kelvin, kelvins |
|
88
87
|
| 2 | °R | R, °Ra, Ra, rankine |
|
89
88
|
|
90
89
|
## 8. Area
|
@@ -93,7 +92,7 @@ These units are defined in `UnitMeasurements::Area`.
|
|
93
92
|
|
94
93
|
| # | Name | Aliases |
|
95
94
|
|:--|:--|:--|
|
96
|
-
|
|
95
|
+
| 1 | m² | m^2, sq m, square meter, square meters, square metre, square metres |
|
97
96
|
| 2 | km² | km^2, sq km, square kilometer, square kilometers, square kilometre, square kilometres |
|
98
97
|
| 3 | in² | in^2, sq in, square inch, square inches |
|
99
98
|
| 4 | ft² | ft^2, sq ft, square foot, square feet |
|
@@ -106,7 +105,7 @@ These units are defined in `UnitMeasurements::Volume`.
|
|
106
105
|
|
107
106
|
| # | Name | Aliases |
|
108
107
|
|:--|:--|:--|
|
109
|
-
|
|
108
|
+
| 1 | l* | liter, litre, liters, litres |
|
110
109
|
| 2 | m³ | m^3, cu m, cubic meter, cubic meters, cubic metre, cubic metres |
|
111
110
|
| 3 | mm³ | mm^3, cu mm, cubic millimeter, cubic millimeters, cubic millimetre, cubic millimetres |
|
112
111
|
| 4 | cm³ | cm^3, cu cm, cubic centimeter, cubic centimeters, cubic centimetre, cubic centimetres |
|
@@ -123,7 +122,7 @@ These units are defined in `UnitMeasurements::Density`.
|
|
123
122
|
|
124
123
|
| # | Name | Aliases |
|
125
124
|
|:--|:--|:--|
|
126
|
-
|
|
125
|
+
| 1 | g/m³ | g/m^3, g·m⁻³, gram per cubic meter, grams per cubic meter, gramme per cubic metre, grammes per cubic metre |
|
127
126
|
| 2 | kg/m³ | kg/m^3, kg·m⁻³, kilogram per cubic meter, kilograms per cubic meter, kilogramme per cubic metre, kilogrammes per cubic metre |
|
128
127
|
| 3 | g/l | g·l⁻¹, gram per liter, grams per liter, gramme per litre, grammes per litre |
|
129
128
|
| 4 | g/ml | g·ml⁻¹, gram per milliliter, grams per milliliter, gramme per millilitre, grammes per millilitre |
|
@@ -135,7 +134,7 @@ These units are defined in `UnitMeasurements::Quantity`.
|
|
135
134
|
|
136
135
|
| # | Name | Aliases |
|
137
136
|
|:--|:--|:--|
|
138
|
-
|
|
137
|
+
| 1 | pc | pcs, piece, pieces |
|
139
138
|
| 2 | pr | pair, pairs |
|
140
139
|
| 3 | gr | gross, grosses |
|
141
140
|
| 4 | dz | doz, dozen, dozens |
|
@@ -147,7 +146,7 @@ These units are defined in `UnitMeasurements::SoundLevel`.
|
|
147
146
|
|
148
147
|
| # | Name | Aliases |
|
149
148
|
|:--|:--|:--|
|
150
|
-
|
|
149
|
+
| 1 | B* | bel, bels |
|
151
150
|
| 2 | Np | neper, nepers |
|
152
151
|
|
153
152
|
## 13. Plane angle
|
@@ -156,7 +155,7 @@ These units are defined in `UnitMeasurements::PlaneAngle`.
|
|
156
155
|
|
157
156
|
| # | Name | Aliases |
|
158
157
|
|:--|:--|:--|
|
159
|
-
|
|
158
|
+
| 1 | rad* | radian, radians |
|
160
159
|
| 2 | deg | °, degree, degrees |
|
161
160
|
| 3 | gon | ᵍ, grad, gradian, gradians |
|
162
161
|
| 4 | cir | circle, circles |
|
@@ -172,7 +171,7 @@ These units are defined in `UnitMeasurements::SolidAngle`.
|
|
172
171
|
|
173
172
|
| # | Name | Aliases |
|
174
173
|
|:--|:--|:--|
|
175
|
-
|
|
174
|
+
| 1 | sr* | steradian, steradians |
|
176
175
|
| 2 | sp | spat, spats |
|
177
176
|
| 3 | deg² | (°)², sq °, square degree, square degrees |
|
178
177
|
| 4 | arcmin² | (′)², sq ′, square arcminute, square arcminutes |
|
@@ -184,7 +183,7 @@ These units are defined in `UnitMeasurements::Force`.
|
|
184
183
|
|
185
184
|
| # | Name | Aliases |
|
186
185
|
|:--|:--|:--|
|
187
|
-
|
|
186
|
+
| 1 | N* | newton, newtons |
|
188
187
|
| 2 | tf | tonne-force |
|
189
188
|
| 3 | dyn | dyne, dynes |
|
190
189
|
| 4 | kgf | kilogram-force, kilogramme-force |
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unit_measurements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harshal LADHE
|
@@ -99,7 +99,7 @@ files:
|
|
99
99
|
- CHANGELOG.md
|
100
100
|
- Gemfile
|
101
101
|
- Gemfile.lock
|
102
|
-
- LICENSE
|
102
|
+
- LICENSE.md
|
103
103
|
- README.md
|
104
104
|
- Rakefile
|
105
105
|
- lib/unit_measurements.rb
|