coinstar 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/bin/coinstar +2 -1
- data/coinstar.gemspec +1 -1
- data/lib/coinstar/change_machine.rb +11 -4
- data/lib/coinstar/executable.rb +34 -0
- data/lib/coinstar.rb +0 -16
- data/spec/change_machine_spec.rb +18 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6537ae7c5712623af2103e58e30cc549a6c081e
|
4
|
+
data.tar.gz: 43c3453ffbb24b8a59f3d5c6e50146a8c497a569
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdaac7671b58765fa4d63c1c5a5fa45f18f292d8220568ef791d650d77c67f05d0b5a8511abbffcba5902e64044e5c456012228ba7babf1fb5f4d80970f4c84a
|
7
|
+
data.tar.gz: 0fead1fbb0149cd2d0fc8cb33b951be35f1ea8fac3e83e8c1a2acca5e5af2c1f888a8dee1ad0bec2bbff92b8903c6af071d09627c0ffc0e8c14a7c6a41a97f49
|
data/bin/coinstar
CHANGED
data/coinstar.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'coinstar'
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.3'
|
8
8
|
spec.authors = ['Brooks Swinnerton']
|
9
9
|
spec.email = ['bswinnerton@gmail.com']
|
10
10
|
spec.description = %q{GA Apprentice Code Challenge}
|
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
|
+
require_relative 'executable'
|
2
3
|
|
3
4
|
class ChangeMachine
|
5
|
+
extend Executable
|
6
|
+
|
4
7
|
CURRENCY = {penny: 1, nickel: 5, dime: 10, quarter: 25}
|
5
8
|
|
6
9
|
def self.make_change(cents)
|
@@ -9,7 +12,7 @@ class ChangeMachine
|
|
9
12
|
unless cents < v
|
10
13
|
divisible = cents / v
|
11
14
|
change[k] = divisible
|
12
|
-
cents
|
15
|
+
cents -= (CURRENCY[k] * divisible)
|
13
16
|
end
|
14
17
|
change
|
15
18
|
end
|
@@ -17,9 +20,13 @@ class ChangeMachine
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def self.make_cents(currency)
|
20
|
-
|
21
|
-
cents
|
22
|
-
|
23
|
+
begin
|
24
|
+
currency.inject(0) do |cents, (k,v)|
|
25
|
+
cents += (CURRENCY[singularize(k)] * v)
|
26
|
+
cents
|
27
|
+
end
|
28
|
+
rescue NoMethodError
|
29
|
+
raise 'Unknown currency type'
|
23
30
|
end
|
24
31
|
end
|
25
32
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Executable
|
2
|
+
def run(args)
|
3
|
+
self.send(args[:argument], args[:params]) if self.respond_to? args[:argument]
|
4
|
+
end
|
5
|
+
|
6
|
+
def clean_input(args)
|
7
|
+
arg, params = args.group_by { |a| a.include? '--' }.values
|
8
|
+
|
9
|
+
begin
|
10
|
+
arg = arg.first.gsub('--', '').to_sym
|
11
|
+
rescue NoMethodError
|
12
|
+
raise 'Please enter either --make_change or --make_cents'
|
13
|
+
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
params = params.first.to_i if arg == :make_change
|
17
|
+
params = params_to_hash(params) if arg == :make_cents
|
18
|
+
rescue NoMethodError
|
19
|
+
raise 'Please enter the change amount' if arg == :make_change
|
20
|
+
raise 'Please enter the cents as quarters=25 format' if arg == :make_cents
|
21
|
+
end
|
22
|
+
{argument: arg, params: params}
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def params_to_hash(params)
|
28
|
+
params.inject({}) do |hash, param|
|
29
|
+
cents = param.split('=')
|
30
|
+
hash[cents.first.to_sym] = cents.last.to_i
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/coinstar.rb
CHANGED
@@ -1,17 +1 @@
|
|
1
1
|
require 'coinstar/change_machine'
|
2
|
-
|
3
|
-
def pass_arguments
|
4
|
-
ARGV.each_slice(2) do |method, parameter|
|
5
|
-
sanitized_arg = method.gsub('--', '')
|
6
|
-
if sanitized_arg == 'make_change'
|
7
|
-
puts ChangeMachine.make_change(parameter.to_i)
|
8
|
-
elsif sanitized_arg == 'make_cents'
|
9
|
-
cents = ARGV.drop(1).inject({}) do |cents, arg|
|
10
|
-
cent = arg.split('=')
|
11
|
-
cents[cent[0].to_sym] = cent[1].to_i
|
12
|
-
cents
|
13
|
-
end
|
14
|
-
puts ChangeMachine.make_cents(cents)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/spec/change_machine_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'pry'
|
2
3
|
|
3
4
|
describe ChangeMachine do
|
4
5
|
context 'Makes change from a given amount of cents and returns a currency set' do
|
@@ -53,17 +54,30 @@ describe ChangeMachine do
|
|
53
54
|
cents = ChangeMachine.make_cents(currency)
|
54
55
|
expect(cents).to eq( 47 )
|
55
56
|
end
|
57
|
+
|
58
|
+
it 'gracefully fails if the currency is unknown' do
|
59
|
+
currency = {pesos: 1}
|
60
|
+
expect { ChangeMachine.make_cents(currency) }.to raise_error 'Unknown currency type'
|
61
|
+
end
|
56
62
|
end
|
57
63
|
|
58
64
|
context 'Takes input from the command line' do
|
59
65
|
it 'makes change' do
|
60
|
-
|
61
|
-
expect(
|
66
|
+
clean_input = ChangeMachine.clean_input(["--make_change", "98"])
|
67
|
+
expect(ChangeMachine.run(clean_input)).to eq({:quarters=>3, :dimes=>2, :pennies=>3})
|
62
68
|
end
|
63
69
|
|
64
70
|
it 'makes cents' do
|
65
|
-
|
66
|
-
expect(
|
71
|
+
clean_input = ChangeMachine.clean_input(["--make_cents", "quarters=3", "dimes=2", "pennies=3"])
|
72
|
+
expect(ChangeMachine.run(clean_input)).to eq 98
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'gracefully fails if an argument isn\'t set' do
|
76
|
+
expect { ChangeMachine.clean_input([]) }.to raise_error "Please enter either --make_change or --make_cents"
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'gracefully fails if parameters aren\'t set' do
|
80
|
+
expect { ChangeMachine.clean_input(["--make_change"]) }.to raise_error "Please enter the change amount"
|
67
81
|
end
|
68
82
|
end
|
69
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coinstar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooks Swinnerton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- coinstar.gemspec
|
98
98
|
- lib/coinstar.rb
|
99
99
|
- lib/coinstar/change_machine.rb
|
100
|
+
- lib/coinstar/executable.rb
|
100
101
|
- spec/change_machine_spec.rb
|
101
102
|
- spec/spec_helper.rb
|
102
103
|
homepage: https://github.com/bswinnerton/coinstar
|