herbie 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ module Herbie
14
14
  end
15
15
 
16
16
  def erb_concat(text)
17
- @_out_buf << text if !@_out_buf.nil?
17
+ @_out_buf << text unless @_out_buf.nil?
18
18
  end
19
19
 
20
20
  end
@@ -2,14 +2,39 @@ module Herbie
2
2
  module Helpers
3
3
  def tag(name, attrs={}, &block)
4
4
  if block_given?
5
- erb_concat "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>#{capture_erb(&block)}</#{name}>"
5
+ erb_concat "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>#{capture_erb(&block)}</#{name}>" # if we're capturing ERB...
6
+ "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>#{block.call}</#{name}>" # if we're nesting function calls...
6
7
  elsif !attrs[:content].nil?
7
8
  content = attrs.delete :content
8
- "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>#{content}</#{name}>"
9
+ "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>#{content}</#{name}>"
9
10
  else
10
- "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>"
11
+ "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>"
11
12
  end
12
13
  end
14
+
15
+ # work in progress...
16
+ def tags(name, collection, attrs={}, &block)
17
+ cycle = attrs.delete :cycle
18
+ result = ""
19
+ collection.each_with_index do |item, i|
20
+ a = attrs.dup
21
+
22
+ unless cycle.nil?
23
+ c = a.delete :class
24
+ classes = []
25
+ classes << c unless c.nil?
26
+ classes << cycle[:even] if i.even? && cycle.key?(:even)
27
+ classes << cycle[:odd] if i.odd? && cycle.key?(:odd)
28
+ a[:class] = classes.join " " unless classes.empty?
29
+ end
30
+
31
+ result += tag name, a do
32
+ block.call(item)
33
+ end
34
+ result += "\n"
35
+ end
36
+ result
37
+ end
13
38
 
14
39
  def script(source=nil, &block)
15
40
  attrs = {
data/lib/herbie.rb CHANGED
@@ -1,7 +1,8 @@
1
+ require 'erubis'
1
2
  require 'herbie/erb_helpers.rb'
2
3
  require 'herbie/generic_helpers.rb'
3
4
  require 'herbie/html_helpers.rb'
4
5
 
5
6
  module Herbie
6
- VERSION = '0.0.3'
7
+ VERSION = '0.1.1'
7
8
  end
data/spec/herbie_spec.rb CHANGED
@@ -4,37 +4,85 @@ require_relative "../lib/herbie.rb"
4
4
  describe Herbie::Helpers do
5
5
  include Herbie::Helpers
6
6
 
7
- it "should be able to output a simple tag" do
8
- attrs = {
9
- :src => "/path/to/image.jpg",
10
- :height => 320,
11
- :width => 640,
12
- :alt => "An image of something"
13
- }
14
-
15
- tag(:img, attrs).should == "<img src=\"#{attrs[:src]}\" height=\"#{attrs[:height]}\" width=\"#{attrs[:width]}\" alt=\"#{attrs[:alt]}\">"
16
- end
17
-
18
- it "should not output attributes whose values are nil" do
19
- attrs = {
20
- :type => "checkbox",
21
- :name => "stay_logged_in",
22
- :value => "true",
23
- :checked => nil
24
- }
25
-
26
- tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\">"
27
- end
28
-
29
- it "should output just the property name for attributes with a boolean value of true" do
30
- attrs = {
31
- :type => "checkbox",
32
- :name => "stay_logged_in",
33
- :value => "true",
34
- :checked => true
35
- }
7
+ describe "tag helpers" do
8
+ it "should be able to output a simple tag" do
9
+ attrs = {
10
+ :src => "/path/to/image.jpg",
11
+ :height => 320,
12
+ :width => 640,
13
+ :alt => "An image of something"
14
+ }
15
+
16
+ tag(:img, attrs).should == "<img src=\"#{attrs[:src]}\" height=\"#{attrs[:height]}\" width=\"#{attrs[:width]}\" alt=\"#{attrs[:alt]}\">"
17
+ end
18
+
19
+ it "should not output attributes whose values are nil" do
20
+ attrs = {
21
+ :type => "checkbox",
22
+ :name => "stay_logged_in",
23
+ :value => "true",
24
+ :checked => nil
25
+ }
26
+
27
+ tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\">"
28
+ end
29
+
30
+ it "should output just the property name for attributes with a boolean value of true" do
31
+ attrs = {
32
+ :type => "checkbox",
33
+ :name => "stay_logged_in",
34
+ :value => "true",
35
+ :checked => true
36
+ }
37
+
38
+ tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\" checked>"
39
+ end
40
+
41
+ it "should output all nested tag method calls" do
42
+ markup = "<div class=\"container\"><h1>Status Report</h1></div>"
43
+ result = tag :div, :class => "container" do
44
+ tag :h1, :content => "Status Report"
45
+ end
46
+ result.should == markup
47
+ end
48
+
49
+ it "should output a collection of tags" do
50
+ markup = <<-MARKUP
51
+ <li><a href="/" title="Back to the homepage">Home</a></li>
52
+ <li><a href="/shop" title="View all our products">Shop</a></li>
53
+ <li><a href="/orders" title="View your existing orders">Orders</a></li>
54
+ <li><a href="/contactus" title="Get in touch with us">Contact Us</a></li>
55
+ <li><a href="/help" title="Can't find what you need? Get help here">Help</a></li>
56
+ MARKUP
57
+
58
+ navigation_items = [
59
+ {:text => "Home", :href => "/", :title => "Back to the homepage"},
60
+ {:text => "Shop", :href => "/shop", :title => "View all our products"},
61
+ {:text => "Orders", :href => "/orders", :title => "View your existing orders"},
62
+ {:text => "Contact Us", :href => "/contactus", :title => "Get in touch with us"},
63
+ {:text => "Help", :href => "/help", :title => "Can't find what you need? Get help here"}
64
+ ]
65
+ result = tags :li, navigation_items do |item|
66
+ link_to item[:href], item[:text], :title => item[:title]
67
+ end
68
+ result.should == markup
69
+ end
36
70
 
37
- tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\" checked>"
71
+ it "should allow an arbitrary class to be added to alternating elements within the collection" do
72
+ markup = <<-MARKUP
73
+ <li class="even">Annie</li>
74
+ <li>Brenda</li>
75
+ <li class="even">Charlie</li>
76
+ <li>Davinia</li>
77
+ <li class="even">Emma</li>
78
+ MARKUP
79
+
80
+ names = ['Annie', 'Brenda', 'Charlie', 'Davinia', 'Emma']
81
+ result = tags :li, names, :cycle => {:even => "even"} do |name|
82
+ name
83
+ end
84
+ result.should == markup
85
+ end
38
86
  end
39
87
 
40
88
  describe "script helpers" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: herbie
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Darlow
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-01 00:00:00 +01:00
13
+ date: 2011-10-10 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -24,6 +24,17 @@ dependencies:
24
24
  version: "2.6"
25
25
  type: :development
26
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: colored
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
27
38
  description: Lovable HTML view helpers for use with ERB.
28
39
  email: ben@kapowaz.net
29
40
  executables: []