metric_conversions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'ruby-units'
4
+
5
+ group :test do
6
+ gem 'minitest'
7
+ end
8
+
3
9
  # Specify your gem's dependencies in metric_conversions.gemspec
4
10
  gemspec
data/README.md CHANGED
@@ -51,9 +51,14 @@ This module was built by <YOUR NAME HERE> and <YOUR NAME HERE>.
51
51
 
52
52
 
53
53
  ### Areas ###
54
- ... notes and explanation goes here ...
55
54
 
56
- This module was built by <YOUR NAME HERE> and <YOUR NAME HERE>.
55
+ Area.convert_[input unit]_to_[desired unit]([number])
56
+
57
+ Example:
58
+
59
+ Area.convert_m_to_mm(1) #=> 1000
60
+
61
+ This module was built by Carlos Diaz-Padron.
57
62
 
58
63
 
59
64
  ### Note on tests ###
@@ -1,3 +1,24 @@
1
+ require 'rubygems'
2
+ require 'ruby-units'
3
+
1
4
  module MetricConversions
2
- # Your code goes here...
3
- end
5
+ class Area
6
+ def self.method_missing(name, *args)
7
+ if name =~ /convert_\w+_to_\w+/
8
+ unit1, unit2 = name.to_s.scan(/convert_(\w+)_to_(\w+)/).first[0..1]
9
+
10
+ convert(args[0], unit1, unit2)
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.convert(num1, unit1, unit2)
17
+ if Unit(unit1) && Unit(unit2) && Unit(unit1).compatible?(Unit(unit2)) && num1.is_a?(Numeric)
18
+ Unit(num1.to_s + ' ' + unit1).convert_to(unit2).scalar
19
+ else
20
+ throw ArgumentError.new('Not a valid number')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module MetricConversions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require '../lib/metric_conversions/areas.rb'
4
+ include MetricConversions
5
+
6
+ describe Area do
7
+ describe 'When given a valid number and units, ' do
8
+ it 'converts the areas correctly' do
9
+ assert_equal 1, Area.convert_mm_to_m(1000)
10
+ assert_equal 1000, Area.convert_m_to_mm(1)
11
+ assert_equal 1000, Area.convert_km_to_m(1)
12
+ end
13
+
14
+ it 'outputs a number' do
15
+ assert Area.convert_m_to_mm(1).is_a? Numeric
16
+ end
17
+ end
18
+
19
+ describe 'When given an invalid unit, ' do
20
+ it 'throws an exception' do
21
+ assert_raises(ArgumentError) do
22
+ Area.convert_this_to_that(5)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe 'When given an invalid number, ' do
28
+ it 'throws and exception' do
29
+ assert_raises(ArgumentError) do
30
+ Area.convert_mm_to_m(true)
31
+ end
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metric_conversions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -32,6 +32,7 @@ files:
32
32
  - lib/metric_conversions/volumes.rb
33
33
  - lib/metric_conversions_tests/lengths_test.rb
34
34
  - metric_conversions.gemspec
35
+ - test/areas_test.rb
35
36
  homepage: https://github.com/profh/metric_conversions
36
37
  licenses: []
37
38
  post_install_message:
@@ -57,4 +58,5 @@ signing_key:
57
58
  specification_version: 3
58
59
  summary: This was initially done as a class project for 67-275 at Carnegie Mellon
59
60
  University
60
- test_files: []
61
+ test_files:
62
+ - test/areas_test.rb