Permalink
Browse files

Get last example in good shape.

  • Loading branch information...
1 parent bd6b410 commit 942a3df3e82688c8b6b0eaa8470fd22626ca0157 @r00k committed Jun 29, 2012
Showing with 61 additions and 0 deletions.
  1. +14 −0 lib/deco.rb
  2. +11 −0 lib/view_plumbing.rb
  3. +26 −0 spec/deco_spec.rb
  4. +5 −0 spec/view_matcher.rb
  5. +5 −0 views/deco.html.erb
View
@@ -0,0 +1,14 @@
+require 'view_plumbing'
+
+class User
+ attr_reader :name
+
+ def initialize(name, is_admin)
+ @name = name
+ @is_admin = is_admin
+ end
+
+ def admin?
+ @is_admin
+ end
+end
View
@@ -0,0 +1,11 @@
+class View
+ def initialize(text, binding=TOPLEVEL_BINDING)
+ @template = text
+ @binding = binding
+ end
+
+ def contents
+ e = ERB.new(@template)
+ e.result(@binding)
+ end
+end
View
@@ -0,0 +1,26 @@
+require_relative '../lib/deco'
+require 'view_matcher'
+
+describe View do
+ context 'when user is an admin' do
+ before do
+ @user = User.new('bob', true)
+ @view = View.new(File.read('views/deco.html.erb'), binding)
+ end
+
+ it 'greets him as an admin' do
+ @view.should include_text("Hail administrator #{@user.name}")
+ end
+ end
+
+ context 'when the user is logged in but not an admin' do
+ before do
+ @user = User.new('bob', false)
+ @view = View.new(File.read('views/deco.html.erb'), binding)
+ end
+
+ it 'greets him as a user' do
+ @view.should include_text("Welcome user #{@user.name}")
+ end
+ end
+end
View
@@ -0,0 +1,5 @@
+RSpec::Matchers.define :include_text do |expected_text|
+ match do |view|
+ view.contents.chomp.include? expected_text
+ end
+end
View
@@ -0,0 +1,5 @@
+<% if @user.admin? %>
+ <p>Hail administrator <%= @user.name %>!</p>
+<% else %>
+ <p>Welcome user <%= @user.name %>!</p>
+<% end %>

0 comments on commit 942a3df

Please sign in to comment.