Skip to content

Instantly share code, notes, and snippets.

@dsyer
Created January 31, 2012 18:20
Show Gist options
  • Save dsyer/1712003 to your computer and use it in GitHub Desktop.
Save dsyer/1712003 to your computer and use it in GitHub Desktop.
Ruby Palindromes
*~
#*
*.gem
.bundle
Gemfile.lock
pkg/*

Ruby code for the LJC February challenge.

Three branches

  • master - is what Dave printed out on the handouts = manual constructor injection
  • modules - is what I presented = mixins with modules
  • cake - is a new branch that introduces a component layer into the master like a Scala cake pattern
class Palindrome::Controller
def initialize(service, persistence)
@service = service
@persistence = persistence
end
def check(word)
[{:word=>word, :palindrome=>@service.palindrome?(word)}, :show]
end
def list
[{:words=>@persistence.list}, :list]
end
end
require 'spec_helper'
describe "Palindrome controller" do
before :each do
@service = mock(Palindrome::Service)
@persistence = mock(Palindrome::Persistence)
@controller = Palindrome::Controller.new(@service, @persistence)
end
it "should accept and check a word" do
@service.should_receive(:palindrome?).with("oxo").and_return(true)
model, view = @controller.check("oxo")
model.should have_key :palindrome
view.should == :show
end
it "should list words" do
@persistence.should_receive(:list).and_return(["oxo"])
model, view = @controller.list()
model.should have_key :words
view.should == :list
end
end
source "http://rubygems.org"
# Specify your gem's dependencies in palindrome.gemspec
gemspec
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "palindrome/version"
Gem::Specification.new do |s|
s.name = "palindrome"
s.version = Palindrome::VERSION
s.authors = ["Dave Syer"]
s.email = ["dsyer@vmware.com"]
s.homepage = ""
s.summary = %q{Palnidrome sample gem}
s.description = %q{Simple gem that can tell you if a word is a palindrome}
s.rubyforge_project = "palindrome"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# specify any dependencies here; for example:
s.add_development_dependency "rspec"
s.add_development_dependency "bundler"
end
require "palindrome/version"
require "palindrome/palindrome"
require "palindrome/persistence"
require "palindrome/controller"
require "palindrome/view"
module Palindrome
persistence = Persistence.new
controller = Controller.new(Service.new(persistence), persistence)
views = {:show=>View::Show.new, :list=>View::List.new}
model, view = controller.check("oxo")
puts views[view].render(model)
model, view = controller.check("wrong")
puts views[view].render(model)
model, view = controller.check("civic")
puts views[view].render(model)
model, view = controller.list
puts views[view].render(model)
end
class Palindrome::Service
def initialize(persistence)
@persistence = persistence
end
def palindrome?(word)
result = word[0..word.length/2] == word[-word.length/2..-1].reverse
@persistence.store(word) if result
result
end
end
require 'spec_helper'
describe "Palindrome service" do
before :each do
@persistence = mock(Palindrome::Persistence)
@service = Palindrome::Service.new(@persistence)
end
it "should recognize and store a palindrome" do
@persistence.should_receive(:store).with("oxo")
@service.palindrome?("oxo").should be_true
end
it "should recognize and not store a non-palindrome" do
@service.palindrome?("pox").should be_false
end
end
#!/usr/bin/env ruby
# Define this early so required dependencies can rely on it being available
module Palindrome
end
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'palindrome'
require 'set'
class Palindrome::Persistence
def initialize()
@words = Set.new([])
end
def store(word)
@words.add(word)
end
def list
@words.to_a.sort
end
end
require 'spec_helper'
describe "Palindrome persistence" do
before :each do
@persistence = Palindrome::Persistence.new
end
it "should store a word" do
@persistence.store("oxo")
@persistence.list.should =~ ["oxo"]
end
it "should list words in alphabetic order" do
@persistence.store("oxo")
@persistence.store("cat")
@persistence.store("able")
@persistence.list.should == %w[able cat oxo]
end
end
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rdoc/task"
task :default => [:test]
RSpec::Core::RakeTask.new("test") do |test|
test.rspec_opts = ["--format", "documentation", "--colour"]
test.pattern = "spec/**/*_spec.rb"
end
RDoc::Task.new do |rd|
rd.rdoc_files.include("lib/**/*.rb")
rd.rdoc_dir = "doc"
end
require 'palindrome'
require 'rspec'
require "palindrome/view"
require "palindrome/controller"
require "palindrome/palindrome"
require "palindrome/persistence"
module Palindrome
VERSION = "0.0.1"
end
module Palindrome::View
class Show
def render(model)
model.to_s
end
end
class List
def render(model)
model.to_s
end
end
end
require 'spec_helper'
describe "Palindrome views" do
context "with :show" do
before :each do
@view = Palindrome::View::Show.new
end
it "should accept a word and a flag" do
@view.render({:word=>"oxo", :palindrome=>true}).should =~ /palindrome=>true/
end
end
context "with :list" do
before :each do
@view = Palindrome::View::List.new
end
it "should accept words" do
@view.render({:words=>%w[oxo]}).should =~ /words=>\["oxo"/
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment