Skip to content

Instantly share code, notes, and snippets.

@tb0yd
Created May 22, 2014 17:54
Show Gist options
  • Save tb0yd/cbcdc0f447e8f50fa6e7 to your computer and use it in GitHub Desktop.
Save tb0yd/cbcdc0f447e8f50fa6e7 to your computer and use it in GitHub Desktop.
diff --git a/bottles/lib/bottle_number.rb b/bottles/lib/bottle_number.rb
new file mode 100644
index 0000000..fb1602b
--- /dev/null
+++ b/bottles/lib/bottle_number.rb
@@ -0,0 +1,43 @@
+class BottleNumber
+ attr_accessor :num
+
+ def initialize(num)
+ @num = num
+ end
+
+ def self.from(num)
+ if num == 0
+ BottleNumberZero.new(num)
+ elsif num == 1
+ BottleNumberOne.new(num)
+ elsif num == 2
+ BottleNumberTwo.new(num)
+ else
+ BottleNumber.new(num)
+ end
+ end
+
+ def what_to_do
+ "Take #{pronoun} down and pass it around"
+ end
+
+ def how_many
+ "#{num}"
+ end
+
+ def next_how_many
+ "#{num - 1}"
+ end
+
+ def pronoun
+ "one"
+ end
+
+ def containers
+ "bottles"
+ end
+
+ def next_containers
+ 'bottles'
+ end
+end
diff --git a/bottles/lib/bottle_number_one.rb b/bottles/lib/bottle_number_one.rb
new file mode 100644
index 0000000..c28414f
--- /dev/null
+++ b/bottles/lib/bottle_number_one.rb
@@ -0,0 +1,13 @@
+class BottleNumberOne < BottleNumber
+ def next_how_many
+ 'no more'
+ end
+
+ def pronoun
+ "it"
+ end
+
+ def containers
+ "bottle"
+ end
+end
diff --git a/bottles/lib/bottle_number_two.rb b/bottles/lib/bottle_number_two.rb
new file mode 100644
index 0000000..abb6a4a
--- /dev/null
+++ b/bottles/lib/bottle_number_two.rb
@@ -0,0 +1,5 @@
+class BottleNumberTwo < BottleNumber
+ def next_containers
+ "bottle"
+ end
+end
diff --git a/bottles/lib/bottle_number_zero.rb b/bottles/lib/bottle_number_zero.rb
new file mode 100644
index 0000000..164c37e
--- /dev/null
+++ b/bottles/lib/bottle_number_zero.rb
@@ -0,0 +1,13 @@
+class BottleNumberZero < BottleNumber
+ def what_to_do
+ "Go to the store and buy some more"
+ end
+
+ def next_how_many
+ "99"
+ end
+
+ def how_many
+ "no more"
+ end
+end
diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb
index f3304a6..0bc5549 100644
--- a/bottles/lib/bottles.rb
+++ b/bottles/lib/bottles.rb
@@ -1,3 +1,9 @@
+require 'forwardable'
+require_relative 'bottle_number'
+require_relative 'bottle_number_one'
+require_relative 'bottle_number_zero'
+require_relative 'bottle_number_two'
+
class Bottles
def song
verses(99, 0)
@@ -53,59 +59,13 @@ class Verse
end
class VerseVariant
+ extend Forwardable
+
attr_reader :num
def initialize(num)
- @num = num
- end
-
- def what_to_do
- if num.zero?
- "Go to the store and buy some more"
- else
- "Take #{pronoun} down and pass it around"
- end
+ @num = BottleNumber.from(num)
end
- def how_many
- if num == 0
- 'no more'
- else
- "#{num}"
- end
- end
-
- def next_how_many
- if num == 1
- 'no more'
- elsif num == 0
- "99"
- else
- "#{num - 1}"
- end
- end
-
- def pronoun
- if num == 1
- 'it'
- else
- 'one'
- end
- end
-
- def containers
- if num == 1
- 'bottle'
- else
- 'bottles'
- end
- end
-
- def next_containers
- if num == 2
- 'bottle'
- else
- 'bottles'
- end
- end
+ def_delegators :@num, :what_to_do, :how_many, :next_how_many, :pronoun, :containers, :next_containers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment