ruby-units 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4469dec705c5d77901aca55e790e97160ef23648
4
- data.tar.gz: 9482d71c9eef96842fdfb3d3f18f29726b6eed83
3
+ metadata.gz: 8e6acf899cc64a4b47d9bed66ea695ee59e3d26b
4
+ data.tar.gz: 6c04d8f9069b6944cb0ca0f32adf74a722b9036d
5
5
  SHA512:
6
- metadata.gz: 86f7e2337fef38a8ed931382684b4f2f8b2f75693e3df8055abd1b1a4df453640b85e557619ac9e5ccd3c814be4c38ca0748c32524138679648404cb8916a775
7
- data.tar.gz: ba383e2fe3d816c7f100cfe26e6f48e85c3ced35a646d1fbd9faf92a3e6a6be62be94b87f951a00639bd64cfd158faf70999de9b25a867345bb65f11c2ff120b
6
+ metadata.gz: 594ca11e51bcd2090f715ff431059045ec981cf758f9b72771faa8978d1dee8ac3677dc4b78b3552dc9786ffa3c62ee7f377569213dd6b43b5245a4b8ffc3f36
7
+ data.tar.gz: 8ce5144320bb529a3b4c6eea632e17a424997f458a1efd18b4a6fa6ec5ceb9c7dd2fbf4f93ce349e528cb745327c118690518370cda3327aa3ecabb50b7c4e5d
data/CHANGELOG.txt CHANGED
@@ -1,5 +1,8 @@
1
1
  Change Log for Ruby-units
2
2
  =========================
3
+ 2016-12-28 2.1.0 * add support for ruby 2.4.0
4
+ * allow configuration for optional separator on output
5
+ * Fix issue #105 -- change 'grad' to 'gon'
3
6
  2015-11-07 2.0.0 * remove support for ruby versions less than 2.0
4
7
  * remove `.unit` and `.u` from String
5
8
  * remove `.unit` from Date
data/README.md CHANGED
@@ -118,6 +118,7 @@ Unit.new('1.5 mm').to_s("%0.2f") # "1.50 mm". Enter any valid format
118
118
  Unit.new('1.5 mm').to_s("in") # converts to inches before printing
119
119
  Unit.new("2 m").to_s(:ft) # returns 6'7"
120
120
  Unit.new("100 kg").to_s(:lbs) # returns 220 lbs, 7 oz
121
+ Unit.new("100 kg").to_s(:stone) # returns 15 stone, 10 lb
121
122
  ```
122
123
 
123
124
  Time Helpers
@@ -228,9 +229,21 @@ Unit.redefine!("cup") do |cup|
228
229
  end
229
230
  ```
230
231
 
232
+ ### Useful methods
233
+
234
+ 1. `scalar` will return the numeric portion of the unit without the attached units
235
+ 2. `base_scalar` will return the scalar in base units (SI)
236
+ 3. `units` will return the name of the units (without the scalar)
237
+ 4. `base` will return the unit converted to base units (SI)
238
+
239
+ ### Storing in a database
240
+
241
+ Units can be stored in a database as either the string representation or in two separate columns defining the scalar and the units.
242
+ Note that if sorting by units is desired you will want to ensure that you are storing the scalars in a consistent unit (i.e, the base units).
243
+
231
244
  ### Namespaced Class
232
245
 
233
- Sometimes the default class 'Unit' may conflict with other gems or applications. Internally ruby-units defines itself using the RubyUnits namespace. The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards compatiblity, the '::Unit' class is defined as an alias to '::RubyUnits::Unit'.
246
+ Sometimes the default class 'Unit' may conflict with other gems or applications. Internally ruby-units defines itself using the RubyUnits namespace. The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards compatibility, the '::Unit' class is defined as an alias to '::RubyUnits::Unit'.
234
247
 
235
248
  To load ruby-units without this alias...
236
249
 
@@ -245,3 +258,37 @@ gem 'ruby-units', require: 'ruby_units/namespaced'
245
258
  ```
246
259
 
247
260
  Note: when using the namespaced version, the Unit.new('unit string') helper will not be defined.
261
+
262
+ ### Configuration
263
+
264
+ Configuration options can be set like:
265
+
266
+ ```
267
+ RubyUnits.configure do |config|
268
+ config.separator = false
269
+ end
270
+ ```
271
+
272
+ Currently there is only one configuration you can set:
273
+
274
+ 1. separator (true/false): should a space be used to separate the scalar from the unit part during output.
275
+
276
+
277
+ ### NOTES
278
+
279
+ #### Mathn
280
+
281
+ Note that the current implementation of ruby-units requires 'mathn' from the ruby standard library.
282
+ This tends to change the behavior of integer math in ways that many people do not expect, and can be the source
283
+ of numerous bugs and odd behaviors. If you encounter what appears to be a bug in your code that seems to be related
284
+
285
+ to the use of ruby-units, try to reproduce the bug by just including 'mathn' by itself.
286
+
287
+ If you identify a bug in a gem or code that uses mathn, please file a bug report or create a pull request to fix it.
288
+
289
+ #### Performance vs. Accuracy
290
+
291
+ Ruby units was originally intended to provide a robust and accurate way to do arbitrary unit conversions.
292
+ In some cases, these conversions can result in the creation and garbage collection of a lot of intermediate objects during
293
+ calculations. This in turn can have a negative impact on performance. The design of ruby-units has emphasized accuracy
294
+ over speed. YMMV if you are doing a lot of math involving units.
File without changes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 2.1.0
@@ -1,20 +1,21 @@
1
- class RubyUnits::Unit < Numeric
2
- @@cached_units = {}
3
-
4
- class Cache
5
- def self.get(key = nil)
6
- key.nil? ? @@cached_units : @@cached_units[key]
7
- end
8
-
9
- def self.set(key, value)
10
- @@cached_units[key] = value
11
- end
1
+ module RubyUnits
2
+ class Unit < Numeric
3
+ @@cached_units = {}
4
+
5
+ class Cache
6
+ def self.get(key = nil)
7
+ key.nil? ? @@cached_units : @@cached_units[key]
8
+ end
9
+
10
+ def self.set(key, value)
11
+ @@cached_units[key] = value
12
+ end
12
13
 
13
- def self.clear
14
- @@cached_units = {}
15
- @@base_unit_cache = {}
16
- Unit.new(1)
14
+ def self.clear
15
+ @@cached_units = {}
16
+ @@base_unit_cache = {}
17
+ Unit.new(1)
18
+ end
17
19
  end
18
-
19
20
  end
20
21
  end
@@ -0,0 +1,41 @@
1
+ # allow for optional configuration of RubyUnits
2
+ #
3
+ # Usage:
4
+ #
5
+ # RubyUnits.configure do |config|
6
+ # config.separator = false
7
+ # end
8
+ module RubyUnits
9
+ class << self
10
+ attr_writer :configuration
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.reset
18
+ @configuration = Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield configuration
23
+ end
24
+
25
+ # holds actual configuration values for RubyUnits
26
+ class Configuration
27
+ # used to separate the scalar from the unit when generating output.
28
+ # set to nil to prevent adding a space to the string representation of a unit
29
+ # separators other than ' ' and '' may work, but you may encounter problems
30
+ attr_reader :separator
31
+
32
+ def initialize
33
+ self.separator = true
34
+ end
35
+
36
+ def separator=(value)
37
+ raise ArgumentError, "configuration 'separator' may only be true or false" unless value.class == TrueClass || value.class == FalseClass
38
+ @separator = value ? ' ' : nil
39
+ end
40
+ end
41
+ end
@@ -3,7 +3,7 @@ require 'date'
3
3
  # Allow date objects to do offsets by a time unit
4
4
  # Date.today + Unit.new("1 week") => gives today+1 week
5
5
  class Date
6
- alias_method :unit_date_add, :+
6
+ alias unit_date_add +
7
7
  # @param [Object] other
8
8
  # @return [Unit]
9
9
  def +(other)
@@ -16,7 +16,7 @@ class Date
16
16
  end
17
17
  end
18
18
 
19
- alias_method :unit_date_sub, :-
19
+ alias unit_date_sub -
20
20
  # @param [Object] other
21
21
  # @return [Unit]
22
22
  def -(other)
@@ -46,7 +46,7 @@ class Date
46
46
  end
47
47
  # :nocov_19:
48
48
 
49
- alias_method :units_datetime_inspect, :inspect
49
+ alias units_datetime_inspect inspect
50
50
  # @deprecated
51
51
  def inspect(dump = false)
52
52
  return units_datetime_inspect if dump
@@ -1,8 +1,6 @@
1
1
  class RubyUnits::Unit < Numeric
2
-
3
2
  # Handle the definition of units
4
3
  class Definition
5
-
6
4
  # @return [Array]
7
5
  attr_writer :aliases
8
6
 
@@ -30,14 +28,14 @@ class RubyUnits::Unit < Numeric
30
28
  # unit.definition = RubyUnits::Unit.new("7/4 inches")
31
29
  # end
32
30
  #
33
- def initialize(_name, _definition = [], &block)
31
+ def initialize(name, definition = [])
34
32
  yield self if block_given?
35
- self.name ||= _name.gsub(/[<>]/,'')
36
- @aliases ||= (_definition[0] || [_name])
37
- @scalar ||= _definition[1]
38
- @kind ||= _definition[2]
39
- @numerator ||= _definition[3] || RubyUnits::Unit::UNITY_ARRAY
40
- @denominator ||= _definition[4] || RubyUnits::Unit::UNITY_ARRAY
33
+ self.name ||= name.gsub(/[<>]/, '')
34
+ @aliases ||= (definition[0] || [name])
35
+ @scalar ||= definition[1]
36
+ @kind ||= definition[2]
37
+ @numerator ||= definition[3] || RubyUnits::Unit::UNITY_ARRAY
38
+ @denominator ||= definition[4] || RubyUnits::Unit::UNITY_ARRAY
41
39
  @display_name ||= @aliases.first
42
40
  end
43
41
 
@@ -46,14 +44,14 @@ class RubyUnits::Unit < Numeric
46
44
  # @return [String, nil]
47
45
  # @todo refactor Unit and Unit::Definition so we don't need to wrap units with angle brackets
48
46
  def name
49
- "<#{@name}>" if (defined?(@name) && @name)
47
+ "<#{@name}>" if defined?(@name) && @name
50
48
  end
51
49
 
52
50
  # set the name, strip off '<' and '>'
53
51
  # @param [String]
54
52
  # @return [String]
55
- def name=(_name)
56
- @name = _name.gsub(/[<>]/,'')
53
+ def name=(name_value)
54
+ @name = name_value.gsub(/[<>]/, '')
57
55
  end
58
56
 
59
57
  # alias array must contain the name of the unit and entries must be unique
@@ -66,35 +64,35 @@ class RubyUnits::Unit < Numeric
66
64
  # @param [Unit] unit
67
65
  # @return [Unit::Definition]
68
66
  def definition=(unit)
69
- _base = unit.to_base
70
- @scalar = _base.scalar
71
- @kind = _base.kind
72
- @numerator = _base.numerator
73
- @denominator = _base.denominator
67
+ base = unit.to_base
68
+ @scalar = base.scalar
69
+ @kind = base.kind
70
+ @numerator = base.numerator
71
+ @denominator = base.denominator
74
72
  self
75
73
  end
76
74
 
77
75
  # is this definition for a prefix?
78
76
  # @return [Boolean]
79
77
  def prefix?
80
- self.kind == :prefix
78
+ kind == :prefix
81
79
  end
82
80
 
83
81
  # Is this definition the unity definition?
84
82
  # @return [Boolean]
85
83
  def unity?
86
- self.prefix? && self.scalar == 1
84
+ prefix? && scalar == 1
87
85
  end
88
86
 
89
87
  # is this a base unit?
90
88
  # units are base units if the scalar is one, and the unit is defined in terms of itself.
91
89
  # @return [Boolean]
92
90
  def base?
93
- (self.denominator == RubyUnits::Unit::UNITY_ARRAY) &&
94
- (self.numerator != RubyUnits::Unit::UNITY_ARRAY) &&
95
- (self.numerator.size == 1) &&
96
- (self.scalar == 1) &&
97
- (self.numerator.first == self.name)
91
+ (denominator == RubyUnits::Unit::UNITY_ARRAY) &&
92
+ (numerator != RubyUnits::Unit::UNITY_ARRAY) &&
93
+ (numerator.size == 1) &&
94
+ (scalar == 1) &&
95
+ (numerator.first == self.name)
98
96
  end
99
97
  end
100
98
  end
@@ -1,14 +1,13 @@
1
1
  require 'mathn'
2
2
 
3
- # Math will convert unit objects to radians and then attempt to use the value for
4
- # trigonometric functions.
3
+ # Math will convert unit objects to radians and then attempt to use the value for
4
+ # trigonometric functions.
5
5
  module Math
6
-
7
- alias :unit_sqrt :sqrt
6
+ alias unit_sqrt sqrt
8
7
  # @return [Numeric]
9
8
  def sqrt(n)
10
- if RubyUnits::Unit === n
11
- (n**(Rational(1,2))).to_unit
9
+ if n.is_a?(RubyUnits::Unit)
10
+ (n**Rational(1, 2)).to_unit
12
11
  else
13
12
  unit_sqrt(n)
14
13
  end
@@ -19,12 +18,12 @@ module Math
19
18
  module_function :sqrt
20
19
 
21
20
  #:nocov:
22
- if self.respond_to?(:cbrt)
23
- alias :unit_cbrt :cbrt
21
+ if respond_to?(:cbrt)
22
+ alias unit_cbrt cbrt
24
23
  # @return [Numeric]
25
24
  def cbrt(n)
26
25
  if RubyUnits::Unit === n
27
- (n**(Rational(1,3))).to_unit
26
+ (n**Rational(1, 3)).to_unit
28
27
  else
29
28
  unit_cbrt(n)
30
29
  end
@@ -35,18 +34,18 @@ module Math
35
34
  module_function :cbrt
36
35
  end
37
36
  #:nocov:
38
-
39
- alias :unit_sin :sin
37
+
38
+ alias unit_sin sin
40
39
  # @return [Numeric]
41
40
  def sin(n)
42
- RubyUnits::Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
41
+ RubyUnits::Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
43
42
  end
44
43
  # @return [Numeric]
45
44
  module_function :unit_sin
46
45
  # @return [Numeric]
47
46
  module_function :sin
48
47
 
49
- alias :unit_cos :cos
48
+ alias unit_cos cos
50
49
  # @return [Numeric]
51
50
  def cos(n)
52
51
  RubyUnits::Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
@@ -55,8 +54,8 @@ module Math
55
54
  module_function :unit_cos
56
55
  # @return [Numeric]
57
56
  module_function :cos
58
-
59
- alias :unit_sinh :sinh
57
+
58
+ alias unit_sinh sinh
60
59
  # @return [Numeric]
61
60
  def sinh(n)
62
61
  RubyUnits::Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
@@ -66,7 +65,7 @@ module Math
66
65
  # @return [Numeric]
67
66
  module_function :sinh
68
67
 
69
- alias :unit_cosh :cosh
68
+ alias unit_cosh cosh
70
69
  # @return [Numeric]
71
70
  def cosh(n)
72
71
  RubyUnits::Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
@@ -76,17 +75,17 @@ module Math
76
75
  # @return [Numeric]
77
76
  module_function :cosh
78
77
 
79
- alias :unit_tan :tan
78
+ alias unit_tan tan
80
79
  # @return [Numeric]
81
80
  def tan(n)
82
- RubyUnits::Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
81
+ RubyUnits::Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
83
82
  end
84
83
  # @return [Numeric]
85
84
  module_function :tan
86
85
  # @return [Numeric]
87
86
  module_function :unit_tan
88
87
 
89
- alias :unit_tanh :tanh
88
+ alias unit_tanh tanh
90
89
  # @return [Numeric]
91
90
  def tanh(n)
92
91
  RubyUnits::Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
@@ -96,36 +95,33 @@ module Math
96
95
  # @return [Numeric]
97
96
  module_function :tanh
98
97
 
99
- alias :unit_hypot :hypot
98
+ alias unit_hypot hypot
100
99
  # Convert parameters to consistent units and perform the function
101
100
  # @return [Numeric]
102
- def hypot(x,y)
101
+ def hypot(x, y)
103
102
  if RubyUnits::Unit === x && RubyUnits::Unit === y
104
- (x**2 + y**2)**(1/2)
103
+ (x**2 + y**2)**(1 / 2)
105
104
  else
106
- unit_hypot(x,y)
105
+ unit_hypot(x, y)
107
106
  end
108
107
  end
109
108
  # @return [Numeric]
110
109
  module_function :unit_hypot
111
110
  # @return [Numeric]
112
111
  module_function :hypot
113
-
114
- alias :unit_atan2 :atan2
115
- # @return [Numeric]
116
- def atan2(x,y)
117
- case
118
- when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x !~ y)
119
- raise ArgumentError, "Incompatible RubyUnits::Units"
120
- when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x =~ y)
121
- Math::unit_atan2(x.base_scalar, y.base_scalar)
112
+
113
+ alias unit_atan2 atan2
114
+ # @return [Numeric]
115
+ def atan2(x, y)
116
+ raise ArgumentError, 'Incompatible RubyUnits::Units' if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && !x.compatible?(y)
117
+ if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && x.compatible?(y)
118
+ Math.unit_atan2(x.base_scalar, y.base_scalar)
122
119
  else
123
- Math::unit_atan2(x,y)
120
+ Math.unit_atan2(x, y)
124
121
  end
125
122
  end
126
123
  # @return [Numeric]
127
124
  module_function :unit_atan2
128
125
  # @return [Numeric]
129
126
  module_function :atan2
130
-
131
127
  end
@@ -3,6 +3,7 @@ $LOAD_PATH << File.dirname(__FILE__)
3
3
 
4
4
  # require_relative this file to avoid creating an class alias from Unit to RubyUnits::Unit
5
5
  require_relative 'version'
6
+ require_relative 'configuration'
6
7
  require_relative 'definition'
7
8
  require_relative 'cache'
8
9
  require_relative 'array'
@@ -6,19 +6,18 @@ class String
6
6
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
7
7
  end
8
8
 
9
- alias_method :original_format, :%
9
+ alias original_format %
10
10
  # format unit output using formating codes
11
11
  # @example '%0.2f' % '1 mm'.to_unit => '1.00 mm'
12
12
  # @return [String]
13
13
  def format_with_unit(*other)
14
- case
15
- when other.first.is_a?(RubyUnits::Unit)
14
+ if other.first.is_a?(RubyUnits::Unit)
16
15
  other.first.to_s(self)
17
16
  else
18
17
  original_format(*other)
19
18
  end
20
19
  end
21
- alias_method :%, :format_with_unit
20
+ alias % format_with_unit
22
21
 
23
22
  # @param (see RubyUnits::Unit#convert_to)
24
23
  # @return (see RubyUnits::Unit#convert_to)
@@ -5,7 +5,6 @@ require 'time'
5
5
  # is in years, decades, or centuries. This leads to less precise values, but ones that match the
6
6
  # calendar better.
7
7
  class Time
8
-
9
8
  class << self
10
9
  alias unit_time_at at
11
10
  end
@@ -15,15 +14,15 @@ class Time
15
14
  # @param [Time] arg
16
15
  # @param [Integer] ms
17
16
  # @return [RubyUnits::Unit, Time]
18
- def self.at(arg, ms=nil)
17
+ def self.at(arg, ms = nil)
19
18
  case arg
20
19
  when Time
21
20
  unit_time_at(arg)
22
21
  when RubyUnits::Unit
23
22
  if ms
24
- unit_time_at(arg.convert_to("s").scalar, ms)
23
+ unit_time_at(arg.convert_to('s').scalar, ms)
25
24
  else
26
- unit_time_at(arg.convert_to("s").scalar)
25
+ unit_time_at(arg.convert_to('s').scalar)
27
26
  end
28
27
  else
29
28
  ms.nil? ? unit_time_at(arg) : unit_time_at(arg, ms)
@@ -41,16 +40,16 @@ class Time
41
40
  # :nocov_19:
42
41
  end
43
42
 
44
- alias :unit_add :+
43
+ alias unit_add +
45
44
  # @return [RubyUnits::Unit, Time]
46
45
  def +(other)
47
46
  case other
48
47
  when RubyUnits::Unit
49
- other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units
48
+ other = other.convert_to('d').round.convert_to('s') if %w(y decade century).include? other.units
50
49
  begin
51
50
  unit_add(other.convert_to('s').scalar)
52
51
  rescue RangeError
53
- self.to_datetime + other
52
+ to_datetime + other
54
53
  end
55
54
  else
56
55
  unit_add(other)
@@ -64,17 +63,17 @@ class Time
64
63
  Time.now + duration.to_unit
65
64
  end
66
65
 
67
- alias :unit_sub :-
66
+ alias unit_sub -
68
67
 
69
68
  # @return [RubyUnits::Unit, Time]
70
69
  def -(other)
71
70
  case other
72
71
  when RubyUnits::Unit
73
- other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units
72
+ other = other.convert_to('d').round.convert_to('s') if %w(y decade century).include? other.units
74
73
  begin
75
74
  unit_sub(other.convert_to('s').scalar)
76
75
  rescue RangeError
77
- self.send(:to_datetime) - other
76
+ send(:to_datetime) - other
78
77
  end
79
78
  else
80
79
  unit_sub(other)