measured 0.0.2 → 0.0.3

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: da9806ac3e691e125d4c422f335ed57937a0999f
4
- data.tar.gz: 095674b8607549707baa6bd204438cad9b6bd0ba
3
+ metadata.gz: f0036468352e098a2fa434a576a3a802078666f8
4
+ data.tar.gz: 2e0d6385ada089335c87ebb8e79da28bfcd826bd
5
5
  SHA512:
6
- metadata.gz: 6b0f6142a0d52b4c1ac270d66c247fca692b9577067668b9aff594d0612abc42e40fa85e4f82a9f8b89baddc13b5a15ca55cff847848ca5154ec3059714b4b6e
7
- data.tar.gz: a850df7b190a895b8a660ad4d46fd7c5c9a46733a308c222e917245f1ca0f9c9249ec1be0325bcf10a3cb3c78904c21c6f36da94f18d9454f44c0884a9ac592e
6
+ metadata.gz: d206778b9f16b8fa24c281a68bae304f76325ef9aba6a9d7bdcfe183438beb71f8858a8d77587e54723e67e350f841e5830ed63766758e10432a241671536a92
7
+ data.tar.gz: 88e4bf7c581f6fb755d9c8cf435a0bc4ef025429b22a57a388d66c7f65862cd9c36e04a752970a94e8fe183d41db441df628a64bf751fbffe122545936cfdcdd
@@ -50,6 +50,10 @@ class Measured::Conversion
50
50
  @conversion_table ||= Measured::ConversionTable.new(@units).to_h
51
51
  end
52
52
 
53
+ def significant_digits
54
+ 6
55
+ end
56
+
53
57
  private
54
58
 
55
59
  def add_new_unit(unit_name, aliases:, value: nil, base: false)
@@ -7,8 +7,14 @@ class Measured::Measurable
7
7
  def initialize(value, unit)
8
8
  raise Measured::UnitError, "Unit #{ unit } does not exits." unless self.class.conversion.unit_or_alias?(unit)
9
9
 
10
- @value = value
11
- @value = BigDecimal(@value) unless @value.is_a?(BigDecimal)
10
+ @value = case value
11
+ when Float
12
+ BigDecimal(value, self.class.conversion.significant_digits)
13
+ when BigDecimal
14
+ value
15
+ else
16
+ BigDecimal(value)
17
+ end
12
18
 
13
19
  @unit = self.class.conversion.to_unit_name(unit)
14
20
  end
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -98,6 +98,10 @@ class Measured::ConversionTest < ActiveSupport::TestCase
98
98
  end
99
99
  end
100
100
 
101
+ test "#significatn_digits is fixed for now" do
102
+ assert_equal 6, @conversion.significant_digits
103
+ end
104
+
101
105
  test "#convert raises if either unit is not found" do
102
106
  assert_raises Measured::UnitError do
103
107
  Magic.conversion.convert(1, from: "fire", to: "doesnt_exist")
@@ -33,6 +33,10 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
33
33
  assert_equal BigDecimal("5"), Magic.new("5", :arcane).value
34
34
  end
35
35
 
36
+ test "#initialize converts floats to strings and then to BigDecimal so it does not raise" do
37
+ assert_equal BigDecimal("1.2345"), Magic.new(1.2345, :fire).value
38
+ end
39
+
36
40
  test "#initialize converts to the base unit name" do
37
41
  assert_equal "fireball", Magic.new(1, :fire).unit
38
42
  end
@@ -100,7 +104,7 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
100
104
 
101
105
  test "#inspect shows the number and the unit" do
102
106
  assert_equal "#<Magic: 0.1E2 fireball>", Magic.new(10, :fire).inspect
103
- assert_equal "#<Magic: 0.1234E1 magic_missile>", Magic.new("1.234", :magic_missile).inspect
107
+ assert_equal "#<Magic: 0.1234E1 magic_missile>", Magic.new(1.234, :magic_missile).inspect
104
108
  end
105
109
 
106
110
  test "#<=> compares only if the class and unit are the same" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-21 00:00:00.000000000 Z
11
+ date: 2015-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport