units_converter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +3 -0
- data/lib/inflections.rb +3 -0
- data/lib/units_converter/quantity_with_unit.rb +13 -0
- data/lib/units_converter/version.rb +3 -0
- data/lib/units_converter.rb +25 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/units_converter_spec.rb +31 -0
- data/tasks/rspec.rake +3 -0
- data/units_converter.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjFhMmVjYmE3YmZmMjdjOWNmNDZjYzEzM2JjMGNmYmZhZWRkYjczNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjQ1YTdkZTFiNDBjOWM2YmFmMTlmY2M5YTA0Y2EzNjRmNWUyZjJhZA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MThiOWY3OTg4MzNkODUwZDQ2YTU1OTRhZGMzMWFjYTU1OWJkMjA1ZDE5MjVi
|
10
|
+
NjcxZjcwNjIzNWU3Mzg1YTBiNDE1ZTk5MWY1ZjgxMDhhZmQ2ZmZmMjUwN2M3
|
11
|
+
MmY5YTRiZDE3MTQ2ZmQyZTA5ODgwMDdhNDgwZTI3ZWY4YWRhZDM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZWQ4ZGVjMDA0MTE4YWUxMzlkOTY5NzgxYWI0YzQ3NDQyZjg1N2M0YmE3MDBi
|
14
|
+
ODA2ZTEwMDJiODYwZjc3Y2FmYTA5N2NiYTlmYjRjMjMxOWRlYWRkN2Y3MTE4
|
15
|
+
ZDNiMjFjMGJhODkxMjBkYWFhMDkwMmViYTg2NDk1M2ZhMTM2MTU=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Peter Inglesby
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# UnitsConverter
|
2
|
+
|
3
|
+
A toy library for converting quantities between different units.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'units_converter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install units_converter
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
> UnitsConverter.convert(12, :miles).to(:metres).to_f
|
25
|
+
=> 19312.128
|
26
|
+
```
|
data/Rakefile
ADDED
data/lib/inflections.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module UnitsConverter
|
2
|
+
class QuantityWithUnit
|
3
|
+
def initialize(quantity, unit)
|
4
|
+
unit = ActiveSupport::Inflector.singularize(unit)
|
5
|
+
@quantity_in_metres = UnitsConverter::CONVERSIONS_TO_METRES.fetch(unit) * quantity
|
6
|
+
end
|
7
|
+
|
8
|
+
def to(unit)
|
9
|
+
unit = ActiveSupport::Inflector.singularize(unit)
|
10
|
+
@quantity_in_metres / UnitsConverter::CONVERSIONS_TO_METRES.fetch(unit)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "bigdecimal"
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/inflector"
|
5
|
+
|
6
|
+
require "inflections"
|
7
|
+
require "units_converter/quantity_with_unit"
|
8
|
+
require "units_converter/version"
|
9
|
+
|
10
|
+
module UnitsConverter
|
11
|
+
def self.convert(quantity, unit)
|
12
|
+
QuantityWithUnit.new(quantity, unit)
|
13
|
+
end
|
14
|
+
|
15
|
+
CONVERSIONS_TO_METRES = {
|
16
|
+
:metre => BigDecimal.new("1"),
|
17
|
+
:kilometre => BigDecimal.new("1000"),
|
18
|
+
:centimetre => BigDecimal.new("0.01"),
|
19
|
+
:millimetre => BigDecimal.new("0.001"),
|
20
|
+
:mile => BigDecimal.new("1609.344"),
|
21
|
+
:yard => BigDecimal.new("0.9144"),
|
22
|
+
:foot => BigDecimal.new("0.3048"),
|
23
|
+
:inch => BigDecimal.new("0.0254"),
|
24
|
+
}.with_indifferent_access
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "units_converter"
|
2
|
+
|
3
|
+
RSpec::Matchers.define(:be_converted_to) do |new_quantity_and_unit|
|
4
|
+
new_quantity = BigDecimal.new(new_quantity_and_unit[0])
|
5
|
+
new_unit = new_quantity_and_unit[1]
|
6
|
+
|
7
|
+
match do |old_quantity_and_unit|
|
8
|
+
old_quantity = BigDecimal.new(old_quantity_and_unit[0])
|
9
|
+
old_unit = old_quantity_and_unit[1]
|
10
|
+
UnitsConverter.convert(old_quantity, old_unit).to(new_unit) == new_quantity
|
11
|
+
end
|
12
|
+
|
13
|
+
failure_message do |old_quantity_and_unit|
|
14
|
+
old_quantity = BigDecimal.new(old_quantity_and_unit[0])
|
15
|
+
old_unit = old_quantity_and_unit[1]
|
16
|
+
|
17
|
+
"expected that #{old_quantity} #{old_unit} in #{new_unit} would be #{new_quantity}"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe UnitsConverter do
|
4
|
+
specify "converting metres to metres" do
|
5
|
+
expect(["0.01", :metres]).to be_converted_to(["0.01", :metres])
|
6
|
+
expect(["1", :metres]).to be_converted_to(["1", :metres])
|
7
|
+
expect(["100", :metres]).to be_converted_to(["100", :metres])
|
8
|
+
end
|
9
|
+
|
10
|
+
specify "conveting metres to non-metres" do
|
11
|
+
expect(["0.01", :metres]).to be_converted_to(["1", :centimetre])
|
12
|
+
expect(["1", :metre]).to be_converted_to(["100", :centimetres])
|
13
|
+
expect(["100", :metres]).to be_converted_to(["10000", :centimetres])
|
14
|
+
end
|
15
|
+
|
16
|
+
specify "converting non-metres to metres" do
|
17
|
+
expect(["0.01", :centimetres]).to be_converted_to(["0.0001", :metres])
|
18
|
+
expect(["1", :centimetre]).to be_converted_to(["0.01", :metres])
|
19
|
+
expect(["100", :centimetres]).to be_converted_to(["1", :metres])
|
20
|
+
end
|
21
|
+
|
22
|
+
specify "converting non-metres to other non-metres" do
|
23
|
+
expect(["1", :centimetre]).to be_converted_to(["0.00001", :kilometres])
|
24
|
+
expect(["1", :kilometre]).to be_converted_to(["100000", :centimetres])
|
25
|
+
end
|
26
|
+
|
27
|
+
specify "converting to and from unit with irregular plural" do
|
28
|
+
expect(["1", :foot]).to be_converted_to(["12", :inches])
|
29
|
+
expect(["1", :yard]).to be_converted_to(["3", :feet])
|
30
|
+
end
|
31
|
+
end
|
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'units_converter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "units_converter"
|
8
|
+
spec.version = UnitsConverter::VERSION
|
9
|
+
spec.authors = ["Peter Inglesby"]
|
10
|
+
spec.email = ["peter.inglesby@gmail.com"]
|
11
|
+
spec.summary = "A toy library for converting quantities between different units."
|
12
|
+
spec.homepage = "https://github.com/inglesp/units_converter/"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "activesupport"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: units_converter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Inglesby
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- peter.inglesby@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/inflections.rb
|
82
|
+
- lib/units_converter.rb
|
83
|
+
- lib/units_converter/quantity_with_unit.rb
|
84
|
+
- lib/units_converter/version.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/units_converter_spec.rb
|
87
|
+
- tasks/rspec.rake
|
88
|
+
- units_converter.gemspec
|
89
|
+
homepage: https://github.com/inglesp/units_converter/
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: A toy library for converting quantities between different units.
|
113
|
+
test_files:
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/units_converter_spec.rb
|