unitwise 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/data/prefix.yaml CHANGED
@@ -65,7 +65,7 @@
65
65
  :secondary_code: M
66
66
  :scalar: 1e-3
67
67
  - :names: micro
68
- :symbol: μ
68
+ :symbol: "μ"
69
69
  :primary_code: u
70
70
  :secondary_code: U
71
71
  :scalar: 1e-6
data/lib/unitwise.rb CHANGED
@@ -3,7 +3,7 @@ require 'liner'
3
3
  require "unitwise/version"
4
4
  require 'unitwise/base'
5
5
  require 'unitwise/expression'
6
- require 'unitwise/composable'
6
+ require 'unitwise/compatible'
7
7
  require 'unitwise/scale'
8
8
  require 'unitwise/functional'
9
9
  require 'unitwise/measurement'
data/lib/unitwise/atom.rb CHANGED
@@ -5,7 +5,7 @@ module Unitwise
5
5
  class Atom < Base
6
6
  liner :classification, :property, :metric, :special, :arbitrary, :dim
7
7
 
8
- include Unitwise::Composable
8
+ include Unitwise::Compatible
9
9
 
10
10
  class << self
11
11
  # Array of hashes representing atom properties.
data/lib/unitwise/base.rb CHANGED
@@ -18,6 +18,11 @@ module Unitwise
18
18
  end
19
19
  end
20
20
 
21
+ def initialize(*args)
22
+ super(*args)
23
+ freeze
24
+ end
25
+
21
26
  def names=(names)
22
27
  @names = Array(names)
23
28
  end
@@ -1,8 +1,8 @@
1
1
  module Unitwise
2
- # Composable provides methods for determing the composition of unit, term, or
3
- # measurement. This is used to establish compatibility between units, terms,
4
- # or measurements.
5
- module Composable
2
+ # Compatible is used to establish compatibility between units, terms, or
3
+ # measurements. This is done by determining the objects atomic composition
4
+ # represented as a Signed Multiset.
5
+ module Compatible
6
6
 
7
7
  # @api private
8
8
  def self.included(base)
@@ -37,7 +37,7 @@ module Unitwise
37
37
  # Determine if this instance is similar to or compatible with other
38
38
  # @return [true false]
39
39
  # @api public
40
- def similar_to?(other)
40
+ def compatible_with?(other)
41
41
  self.composition == other.composition
42
42
  end
43
43
 
@@ -45,10 +45,9 @@ module Unitwise
45
45
  # @return [-1 0 1]
46
46
  # @api public
47
47
  def <=>(other)
48
- if other.respond_to?(:composition) && similar_to?(other)
48
+ if other.respond_to?(:composition) && compatible_with?(other)
49
49
  scalar <=> other.scalar
50
50
  end
51
51
  end
52
-
53
52
  end
54
53
  end
@@ -45,8 +45,8 @@ module Unitwise
45
45
  attr_reader :function_name
46
46
 
47
47
  def initialize(value, unit, function_name)
48
- super(value, unit)
49
48
  @function_name = function_name
49
+ super(value, unit)
50
50
  end
51
51
 
52
52
  def functional(x=scalar, forward = true)
@@ -28,10 +28,10 @@ module Unitwise
28
28
  # @api public
29
29
  def convert_to(other_unit)
30
30
  other_unit = Unit.new(other_unit)
31
- if similar_to?(other_unit)
31
+ if compatible_with?(other_unit)
32
32
  new(converted_value(other_unit), other_unit)
33
33
  else
34
- raise ConversionError, "Can't convert #{inspect} to #{other_unit}."
34
+ raise ConversionError, "Can't convert #{self} to #{other_unit}."
35
35
  end
36
36
  end
37
37
 
@@ -42,7 +42,7 @@ module Unitwise
42
42
  # measurement * some_other_measurement
43
43
  # @api public
44
44
  def *(other)
45
- operate(:*, other) || raise(TypeError, "Can't multiply #{inspect} by #{other}.")
45
+ operate(:*, other) || raise(TypeError, "Can't multiply #{self} by #{other}.")
46
46
  end
47
47
 
48
48
  # Divide this measurement by a number or another measurement
@@ -52,7 +52,7 @@ module Unitwise
52
52
  # measurement / some_other_measurement
53
53
  # @api public
54
54
  def /(other)
55
- operate(:/, other) || raise(TypeError, "Can't divide #{inspect} by #{other}")
55
+ operate(:/, other) || raise(TypeError, "Can't divide #{self} by #{other}")
56
56
  end
57
57
 
58
58
  # Add another measurement to this unit. Units must be compatible.
@@ -61,7 +61,7 @@ module Unitwise
61
61
  # measurement + some_other_measurement
62
62
  # @api public
63
63
  def +(other)
64
- combine(:+, other) || raise(TypeError, "Can't add #{other} to #{inspect}.")
64
+ combine(:+, other) || raise(TypeError, "Can't add #{other} to #{self}.")
65
65
  end
66
66
 
67
67
  # Subtract another measurement from this unit. Units must be compatible.
@@ -70,7 +70,7 @@ module Unitwise
70
70
  # measurement - some_other_measurement
71
71
  # @api public
72
72
  def -(other)
73
- combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{inspect}.")
73
+ combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{self}.")
74
74
  end
75
75
 
76
76
  # Raise a measurement to a numeric power.
@@ -82,7 +82,7 @@ module Unitwise
82
82
  if number.is_a?(Numeric)
83
83
  new( value ** number, unit ** number )
84
84
  else
85
- raise TypeError, "Can't raise #{inspect} to #{number} power."
85
+ raise TypeError, "Can't raise #{self} to #{number} power."
86
86
  end
87
87
  end
88
88
 
@@ -171,7 +171,7 @@ module Unitwise
171
171
  # Add or subtract other unit
172
172
  # @api private
173
173
  def combine(operator, other)
174
- if similar_to?(other)
174
+ if other.respond_to?(:composition) && compatible_with?(other)
175
175
  new(value.send(operator, other.convert_to(unit).value), unit)
176
176
  end
177
177
  end
@@ -182,7 +182,7 @@ module Unitwise
182
182
  if other.is_a?(Numeric)
183
183
  new(value.send(operator, other), unit)
184
184
  elsif other.respond_to?(:composition)
185
- if similar_to?(other)
185
+ if compatible_with?(other)
186
186
  converted = other.convert_to(unit)
187
187
  new(value.send(operator, converted.value), unit.send(operator, converted.unit))
188
188
  else
@@ -5,7 +5,12 @@ module Unitwise
5
5
  class Scale
6
6
  liner :value, :unit
7
7
 
8
- include Unitwise::Composable
8
+ include Unitwise::Compatible
9
+
10
+ def initialize(*args)
11
+ super(*args)
12
+ freeze
13
+ end
9
14
 
10
15
  # The unit associated with this scale.
11
16
  # @return [Unitwise::Unit]
data/lib/unitwise/term.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  require 'signed_multiset'
2
2
  module Unitwise
3
3
  class Term < Liner.new(:atom, :prefix, :factor, :exponent, :annotation)
4
+ include Unitwise::Compatible
4
5
 
5
- include Unitwise::Composable
6
+ def initialize(*args)
7
+ super(*args)
8
+ freeze
9
+ end
6
10
 
7
11
  def atom=(value)
8
12
  value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
@@ -25,11 +29,11 @@ module Unitwise
25
29
  end
26
30
 
27
31
  def factor
28
- @factor ||= 1
32
+ super || 1
29
33
  end
30
34
 
31
35
  def exponent
32
- @exponent ||= 1
36
+ super || 1
33
37
  end
34
38
 
35
39
  def scalar
data/lib/unitwise/unit.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  module Unitwise
2
2
  class Unit
3
3
  liner :expression, :terms
4
-
5
- include Unitwise::Composable
4
+ include Unitwise::Compatible
6
5
 
7
6
  def initialize(input)
8
7
  if input.respond_to?(:expression)
@@ -1,3 +1,3 @@
1
1
  module Unitwise
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unitwise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lewis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-06 00:00:00.000000000 Z
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liner
@@ -142,7 +142,7 @@ files:
142
142
  - lib/unitwise.rb
143
143
  - lib/unitwise/atom.rb
144
144
  - lib/unitwise/base.rb
145
- - lib/unitwise/composable.rb
145
+ - lib/unitwise/compatible.rb
146
146
  - lib/unitwise/compound.rb
147
147
  - lib/unitwise/errors.rb
148
148
  - lib/unitwise/expression.rb