ruby-si-units 0.0.4 → 0.0.5

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: 5f466356845980e98060a4f9570ff9b06f8dad80
4
- data.tar.gz: 8a30b37d30f7f7ec6194b5c51539538b82d4eff3
3
+ metadata.gz: 86b3b3a059205dfbff5012f1a149d91fee0a7b8f
4
+ data.tar.gz: 6c35073b01b66dff3a23cfd95dcf3af7b88afde2
5
5
  SHA512:
6
- metadata.gz: b7d9d08987f2e2a58989a4c93049154d7755854a0d7ddd471e973da475dc0fc4b23045a7b28c9fadacfbcf4c8446c9bebddfb32b4640d5acc37a3edb5debd8dc
7
- data.tar.gz: f3b4c8c2aecd4a40db6ef7f75a1df804df99ee1326eba977df997e8e6425d47c5b9d9a44a39e0126fd09d5ae5d7d1c30603c7f534b0d1978c3812ed4c91b6c2a
6
+ metadata.gz: 1c2f66e138184bfa1c1d3494c30734f02964a03b63f056c19240e0acaba046080e9323840737c510184a8c7479931f93f7511af748c8172734b46d28438b1f3b
7
+ data.tar.gz: d9857e86333565c89cb4245f6bf196b066dd46eaa9f8922b4b466a80823659a293e6449a2cc764b5e140c217c8ffe9b7ea4d168a232f43461d7906957cc33e75
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-si-units (0.0.4)
4
+ ruby-si-units (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/si_units/unit.rb CHANGED
@@ -1,9 +1,15 @@
1
+ #coding: utf-8
2
+ # = SIUnits
3
+ # A SI prefix unit handling library for ruby
4
+ #
5
+ # @author Kenner Kliemann
6
+ # @see https://github.com/gnomex/ruby-si-units
1
7
  module SIUnits
2
8
  class Unit
3
9
  include Comparable
4
10
  attr_reader :unit_value
5
11
 
6
- #SI Prefix Units
12
+ # Definition of SI Prefix Units, with name, aliases and scale
7
13
  UNITS_DEFINITION = {
8
14
  'googol' => [%w{googol}, 1e100],
9
15
  'yebi' => [%w{Yi Yebi yebi}, 2**80],
@@ -34,18 +40,27 @@ module SIUnits
34
40
  'femto' => [%w{f Femto femto}, 1e-15],
35
41
  'atto' => [%w{a Atto atto}, 1e-18],
36
42
  'zepto' => [%w{z Zepto zepto}, 1e-21],
37
- 'yocto' => [%w{y Yocto yocto}, 1e-24]
43
+ 'yocto' => [%w{y Yocto yocto}, 1e-24],
44
+ 'zero' => [%w{zero}, 0.0]
38
45
  }
39
46
 
47
+ # Create a unit object
48
+ # Can be initialized with a number
49
+ # initialize a unit and parse, recognizing the scale of number
50
+ # @param [Fixnum|Float]
51
+ # @return Unit
40
52
  def initialize(unit)
41
53
  @unit_value = unit
42
54
  @unit_kind = parse_unit
43
55
  end
44
56
 
57
+ # Public call to get a unit best representation
58
+ # @return String
45
59
  def best_scale
46
60
  @best_scale ||= best_value_with_scale
47
61
  end
48
62
 
63
+ #
49
64
  def <=>(comparison)
50
65
  UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
51
66
  end
@@ -61,17 +76,28 @@ module SIUnits
61
76
 
62
77
  private
63
78
 
79
+ #Logic to convert the absolute value to a best form of representation
64
80
  def best_value_with_scale
65
81
  aliase, scalar = UNITS_DEFINITION[@unit_kind]
66
82
  [(@unit_value / scalar), aliase.first].join
67
83
  end
68
84
 
85
+ # Logic to convert a value with scale
86
+ # @return Float
69
87
  def convert_base_prefix_to_value(value, scalar)
70
88
  value * scalar
71
89
  end
72
90
 
91
+ # The parser
92
+ # => Finds that the number range is contained
93
+ # @return String with the name of representation
94
+ # @raise ArgumentError
73
95
  def parse_unit
74
96
  case @unit_value
97
+ when 0 then return "zero"
98
+ when 1e-24..1e-21 then return "yocto"
99
+ when 1e-21..1e-18 then return "atto"
100
+ when 1e-18..1e-15 then return "femto"
75
101
  when 1e-15..1e-12 then return "pico"
76
102
  when 1e-12..1e-9 then return "nano"
77
103
  when 1e-9..1e-6 then return "micro"
@@ -84,10 +110,17 @@ module SIUnits
84
110
  when 1e3..1e6 then return "kilo"
85
111
  when 1e6..1e9 then return "mega"
86
112
  when 1e9..1e12 then return "giga"
87
- else raise "Unit out of range"
113
+ when 1e12..1e15 then return "tera"
114
+ when 1e15..1e18 then return "peta"
115
+ when 1e18..1e21 then return "exa"
116
+ when 1e21..1e24 then return "zetta"
117
+ else raise ArgumentError, "Unit out of range"
88
118
  end
89
119
  end
90
120
 
121
+ # Search on DEFINITIONS if the kind is a element
122
+ # @return Fixnum with scale of prefix
123
+ # @raise ArgumentError
91
124
  def prefix_is_defined?(kind)
92
125
  UNITS_DEFINITION.each { |key, value|
93
126
  return value.last if value[0].include?(kind)
@@ -1,3 +1,4 @@
1
1
  module SIUnits
2
- VERSION = "0.0.4"
2
+ # Project Version
3
+ VERSION = "0.0.5"
3
4
  end
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require "unit"
@@ -105,23 +105,15 @@ describe "it's a deci " do
105
105
  end
106
106
 
107
107
  describe "it's a const " do
108
- xit "max limit" do
109
- unit = SIUnits::Unit.new((1e-1) + 0.1)
110
- expect(unit.best_scale).to match(/const/)
111
- end
112
-
113
- xit "zero" do
114
-
115
- end
116
108
 
117
- xit "min limit" do
118
- unit = SIUnits::Unit.new(1e1)
119
- expect(unit.best_scale).to match(/cost/)
109
+ it "zero" do
110
+ unit = SIUnits::Unit.new(0)
111
+ expect(unit.best_scale).to match(/zero/)
120
112
  end
121
113
 
122
- xit "not a const" do
123
- unit = SIUnits::Unit.new((1e-2) + 0.00000000001)
124
- expect(unit.best_scale).to_not match(/const/)
114
+ it "not a zero" do
115
+ unit = SIUnits::Unit.new(0.1)
116
+ expect(unit.best_scale).to_not match(/zero/)
125
117
  end
126
118
  end
127
119
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-si-units
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenner Kliemann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-05 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,7 +100,7 @@ files:
100
100
  - lib/unit.rb
101
101
  - ruby-si-unit.gemspec
102
102
  - spec/spec_helper.rb
103
- - spec/support/units_shared_example.rb
103
+ - spec/units/string_spec.rb
104
104
  - spec/units/unit_spec.rb
105
105
  homepage: https://github.com/gnomex/ruby-si-units
106
106
  licenses:
@@ -128,5 +128,5 @@ specification_version: 4
128
128
  summary: A SI prefix unit handling library for ruby
129
129
  test_files:
130
130
  - spec/spec_helper.rb
131
- - spec/support/units_shared_example.rb
131
+ - spec/units/string_spec.rb
132
132
  - spec/units/unit_spec.rb
@@ -1,17 +0,0 @@
1
- # coding: utf-8
2
- require 'spec_helper'
3
-
4
- shared_examples_for "is unit of" do |unit, expected_prefix|
5
-
6
- it "is max limit on range" do
7
- expect(:unit.to_s).to eql(expected_prefix)
8
- end
9
-
10
- it "is min limit on range" do
11
- expect(:unit.to_s).to eql(expected_prefix)
12
- end
13
-
14
- it "out of unit range" do
15
- expect(:unit.to_s).to_not eql(expected_prefix)
16
- end
17
- end