coinstar 0.0.2 → 0.0.3

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: 459a18fe52d0334bbd9c18d263b5b1e6824865d6
4
- data.tar.gz: 3778d65a9efc5904c25d5303d41d5d6cc234320d
3
+ metadata.gz: c6537ae7c5712623af2103e58e30cc549a6c081e
4
+ data.tar.gz: 43c3453ffbb24b8a59f3d5c6e50146a8c497a569
5
5
  SHA512:
6
- metadata.gz: 209dffffc36cbd8fa5a55c8e26b1bd25b7499d09ced870b8c0dacbbeebde9f6652eec68ae703fa661e9e9d2f6fa0e6770a657c52d88643a4660a752852475422
7
- data.tar.gz: 9c72d55fbbf983d0a37f64eb63affc52804af4aca7fd42d69d6384da0a9bfe6cb5bc796cb95e97cc177c41a2f8103e55a240a78f403410ad12b275a80cc593e0
6
+ metadata.gz: cdaac7671b58765fa4d63c1c5a5fa45f18f292d8220568ef791d650d77c67f05d0b5a8511abbffcba5902e64044e5c456012228ba7babf1fb5f4d80970f4c84a
7
+ data.tar.gz: 0fead1fbb0149cd2d0fc8cb33b951be35f1ea8fac3e83e8c1a2acca5e5af2c1f888a8dee1ad0bec2bbff92b8903c6af071d09627c0ffc0e8c14a7c6a41a97f49
data/bin/coinstar CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'coinstar'
2
2
 
3
- pass_arguments
3
+ clean_input = ChangeMachine.clean_input(ARGV)
4
+ puts ChangeMachine.run(clean_input)
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.2'
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 = cents - (CURRENCY[k] * divisible)
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
- currency.inject(0) do |cents, (k,v)|
21
- cents += (CURRENCY[singularize(k)] * v)
22
- cents
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
@@ -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
- command_line = `ruby bin/coinstar --make_change 98`
61
- expect(command_line).to include '{:quarters=>3, :dimes=>2, :pennies=>3}'
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
- command_line = `ruby bin/coinstar --make_cents quarters=3 dimes=2 pennies=3`
66
- expect(command_line).to include '98'
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.2
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-03 00:00:00.000000000 Z
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