Permalink
Browse files

Refactoring: #script and #style moved to Piano::Helpers::HTML

  • Loading branch information...
1 parent 67f54b8 commit 8cb879a2fa5175029b936324aedd7d4eba7ca5b4 @xaviervia committed Sep 12, 2012
View
6 Gemfile
@@ -5,3 +5,9 @@ gemspec
if RUBY_PLATFORM =~ /linux/
gem "therubyracer"
end
+
+group :test do
+ gem 'rack-test', :git => "https://github.com/brynary/rack-test.git"
+ gem 'pry'
+ gem 'fast'
+end
View
3 bin/piano
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
+
+$LOAD_PATH << File.expand_path("../../lib", __FILE__)
require 'piano'
require 'metafun/delegator'
@@ -43,7 +45,6 @@ self.delegate Piano::Base, :get, :patch, :put, :post, :delete,
:disable, :use, :development?, :test?,
:production?, :helpers, :settings, :play!
-self.helpers Sinatra::Piano
$LOAD_PATH << Dir.pwd + '/lib'
View
37 features/html_helpers.feature
@@ -0,0 +1,37 @@
+Feature: HTML Helpers tryout
+ In order to make html dev easier
+ As a web developer
+ I want to have a share of helpers
+
+Scenario: Script tag helper
+ Given the haml view "test/index.haml" with:
+ """
+ !!! 5
+ %html
+ %head
+ = script 'jquery.js'
+ """
+ When I go to /test/index
+ Then I should see "src='jquery.js'"
+
+Scenario: Style tag helper
+ Given the haml view "test/index.haml" with:
+ """
+ !!! 5
+ %html
+ %head
+ = style 'app.css'
+ """
+ When I go to /test/index
+ Then I should see "href='app.css'"
+
+Scenario: Style helper with text media query
+ Given the haml view "test/index.haml" with:
+ """
+ !!! 5
+ %html
+ %head
+ = style 'app.css', "media='screen'"
+ """
+ When I go to /test/index
+ Then I should see "href='app.css' media='screen'"
View
12 features/stepdefs/html_helpers.rb
@@ -0,0 +1,12 @@
+# -*- encoding : utf-8 -*-
+Given /^the haml view "(.+?)" with:$/ do |name, content|
+ Fast.file.write name, content
+end
+
+When /^I go to (.+?)$/ do |path|
+ get path
+end
+
+Then /^I should see "(.+?)"$/ do |text|
+ last_response.body.should include text
+end
View
14 features/support/env.rb
@@ -0,0 +1,14 @@
+# -*- encoding : utf-8 -*-
+require 'fast'
+
+$LOAD_PATH << File.expand_path("../../../../lib", __FILE__)
+require 'piano'
+require 'piano/routes'
+
+require 'rack/test'
+
+def app
+ Piano::Base
+end
+
+World Rack::Test::Methods
View
2 lib/piano.rb
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'sinatra/base'
require 'sinatra/flash'
require 'piano/tilt_utf8_patch'
@@ -19,6 +20,7 @@
require 'sinatra/piano'
+require 'piano/helpers/html'
require 'piano/base'
require 'piano/controller_loader'
require 'piano/version'
View
4 lib/piano/base.rb
@@ -1,6 +1,8 @@
+# -*- encoding : utf-8 -*-
module Piano
class Base < Sinatra::Base
- register Sinatra::Piano
+ helpers Piano::Helpers::HTML
+ helpers Sinatra::Piano
register Sinatra::Flash
Compass.configuration do |config|
View
1 lib/piano/controller_loader.rb
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module Piano
# Handler of .controller files loading
class ControllerLoader
View
26 lib/piano/helpers/html.rb
@@ -0,0 +1,26 @@
+module Piano
+ module Helpers
+ # Helpers for HTML
+ module HTML
+ # Formats a javascript <script></script> tag with the input
+ # @return [String] the script tag pointing to the argument
+ def script src, extra = ''
+ if extra.empty?
+ "<script type='text/javascript' src='#{src}'></script>"
+ else
+ "<script type='text/javascript' src='#{src}' #{extra}></script>"
+ end
+ end
+
+ # Formats a css stylesheet <link /> tag with the input
+ # @return [String] the stylesheet link tag pointing to the argument
+ def style href, extra = ''
+ if extra.empty?
+ "<link rel='stylesheet' type='text/css' href='#{href}' />"
+ else
+ "<link rel='stylesheet' type='text/css' href='#{href}' #{extra} />"
+ end
+ end
+ end
+ end
+end
View
1 lib/piano/routes.rb
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module Piano
class Base
get '/' do
View
12 lib/sinatra/piano.rb
@@ -43,14 +43,14 @@ def data_for(template)
end
# Sugar: formats a css stylesheet <link /> tag with the input
- def style(path, more = '')
- "<link rel='stylesheet' type='text/css' href='#{path}' #{more} />"
- end
+ #def style(path, more = '')
+ # "<link rel='stylesheet' type='text/css' href='#{path}' #{more} />"
+ #end
# Sugar: formats a javascript <script> tag with the input
- def script(path, more = '')
- "<script type='text/javascript' src='#{path}' #{more} ></script>"
- end
+ #def script(path, more = '')
+ # "<script type='text/javascript' src='#{path}' #{more} ></script>"
+ #end
# Fails. Shouts a 404 response and prints hints
#
View
3 piano.gemspec
@@ -23,6 +23,9 @@ Gem::Specification.new do |s|
s.add_dependency "i18n", ">= 0.6.0"
s.add_dependency "metafun"
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'cucumber'
+
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) }
View
48 spec/piano/helpers/html_spec.rb
@@ -0,0 +1,48 @@
+# -*- encoding : utf-8 -*-
+require 'spec_helper'
+
+describe Piano::Helpers::HTML do
+ before do
+ @controller = stub 'controller'
+ @controller.extend Piano::Helpers::HTML
+ end
+
+ shared_examples_for 'any tag builder' do
+ it 'should serialize a hash of attributes as second argument'
+ it 'should include a second argument string in the tag' do
+ tag = @controller.send @method, 'thedata', 'some="other" attr'
+ tag.should include 'thedata'
+ tag.should include 'some="other" attr'
+ end
+ end
+
+ describe '#script' do
+ before do
+ @method = :script
+ end
+
+ it 'should receive an argument and return the script tag' do
+ tag = @controller.script 'myscript.js'
+ tag.should == "<script type='text/javascript' src='myscript.js'></script>"
+ end
+
+ it_behaves_like 'any tag builder'
+ end
+
+ describe '#style' do
+ before do
+ @method = :style
+ end
+
+ it 'should receive an argument and return the style tag' do
+ tag = @controller.style 'mystyle.css'
+ tag.should == "<link rel='stylesheet' type='text/css' href='mystyle.css' />"
+ end
+
+ it_behaves_like 'any tag builder'
+ end
+
+ describe '#anchor' do
+ it 'should receive the link and the text as arguments and return the tag'
+ end
+end
View
2 spec/spec_helper.rb
@@ -0,0 +1,2 @@
+# -*- encoding : utf-8 -*-
+require 'piano'

0 comments on commit 8cb879a

Please sign in to comment.