change_weight 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/change_weight.rb +45 -0
  3. metadata +42 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6633fb193fcccbe6a2fcde329fcc14003fae997fb8d0e4bd39a57eba6f060f6e
4
+ data.tar.gz: c5782a8abb88b1dc3f0f025e08414da163719b1d02a955fa5376ddee6402e14f
5
+ SHA512:
6
+ metadata.gz: 4cf50688a6ed9dbc81195c7c604d7d70117a198539ef7208f1f70d58bc4e11987a23de4f5f6cd48bd7aa946786a106ab64f3a53d4700e9d49f28c35ae87a526c
7
+ data.tar.gz: 6103cd819b35a2e1a4ff1a652db4594238fb6aa2ee479d692c7d1854ffd1c366dae01a3425a22213adb5e529775cb8e92ba51f457552f833f8a0ffdcd8f4bed1
@@ -0,0 +1,45 @@
1
+ require 'singleton'
2
+ module ChangeFinal
3
+
4
+ class ConversionError < StandardError
5
+ end
6
+ class ChangeWeight
7
+ include Singleton
8
+ # lookup table
9
+ CONVERSIONS = {
10
+ kg: {
11
+ pounds: 2.20462,
12
+ stone: 0.157473,
13
+ },
14
+ pounds: {
15
+ kg: 0.453592,
16
+ stone: 0.0714286
17
+ },
18
+ stone: {
19
+ kg: 6.35029,
20
+ pounds: 14
21
+ }
22
+ }
23
+
24
+ def convert(value, from, to:)
25
+ raise ConversionError, "Value is not numeric" unless value.is_a? Numeric
26
+ raise ConversionError, "Value #{value} is not positive" unless value >= 0
27
+ "#{value * conversion_ratio(from, to) }#{to.to_s}"
28
+ end
29
+
30
+ def conversions(value, unit)
31
+ CONVERSIONS[unit].keys.map do |other_unit|
32
+ convert(value, unit, to: other_unit)
33
+ end
34
+ end
35
+
36
+ private
37
+ def conversion_ratio(from, to)
38
+ begin
39
+ CONVERSIONS.fetch(from).fetch(to)
40
+ rescue KeyError
41
+ raise ConversionError, "Cannot convert #{from.to_s} to #{to.to_s}"
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,42 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: change_weight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gavin Smyth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem
14
+ email: gavinsmyth1998@gmail.ie
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/change_weight.rb
20
+ homepage:
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 3.0.3
39
+ signing_key:
40
+ specification_version: 4
41
+ summary: change weight!
42
+ test_files: []