herbie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ require 'herbie/erb_helpers.rb'
2
+ require 'herbie/generic_helpers.rb'
3
+ require 'herbie/html_helpers.rb'
4
+
5
+ module Herbie
6
+ VERSION = '0.0.1'
7
+ end
@@ -0,0 +1,21 @@
1
+ module Herbie
2
+ module Helpers
3
+
4
+ def erb_with_output_buffer(buf = '')
5
+ @_out_buf, old_buffer = buf, @_out_buf
6
+ yield
7
+ @_out_buf
8
+ ensure
9
+ @_out_buf = old_buffer
10
+ end
11
+
12
+ def capture_erb(*args, &block)
13
+ erb_with_output_buffer { block_given? && block.call(*args) }
14
+ end
15
+
16
+ def erb_concat(text)
17
+ @_out_buf << text if !@_out_buf.nil?
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Herbie
2
+ module Helpers
3
+
4
+ def snake_case(string)
5
+ return string.downcase if string =~ /^[A-Z]+$/
6
+ string.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
7
+ return $+.downcase
8
+ end
9
+
10
+ def attributes(hash)
11
+ a = []
12
+ hash.each_pair do |k,v|
13
+ unless v.nil?
14
+ if v === true
15
+ a.push "#{snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}"
16
+ else
17
+ a.push "#{snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}=\"#{v}\""
18
+ end
19
+ end
20
+ end
21
+ a.join(' ')
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Herbie
2
+ module Helpers
3
+ def tag(name, attrs={}, &block)
4
+ if block_given?
5
+ erb_concat "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>#{capture_erb(&block)}</#{name}>"
6
+ elsif !attrs[:content].nil?
7
+ content = attrs.delete :content
8
+ "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>#{content}</#{name}>"
9
+ else
10
+ "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>"
11
+ end
12
+ end
13
+
14
+ def script(source=nil, &block)
15
+ attrs = {
16
+ :type => "text/javascript",
17
+ :charset => "utf-8"
18
+ }
19
+
20
+ if block_given?
21
+ erb_concat "#{tag :script, attrs}\n#{capture_erb(&block)}\n</script>"
22
+ else
23
+ source = "/javascripts/#{source}" unless source.nil? || source.match(/^\//)
24
+ attrs = attrs.merge({:src => source})
25
+ "#{tag :script, attrs}</script>"
26
+ end
27
+ end
28
+
29
+ def style(href=nil, attrs={}, &block)
30
+ default_attrs = {
31
+ :rel => "stylesheet",
32
+ :type => "text/css"
33
+ }
34
+
35
+ if block_given?
36
+ default_attrs.delete :rel
37
+ erb_concat "#{tag :style, default_attrs.merge(attrs)}\n#{capture_erb(&block)}\n</style>"
38
+ else
39
+ href = "/stylesheets/#{href}" unless href.match(/^\//)
40
+ attrs = default_attrs.merge({:href => href}.merge(attrs))
41
+ "#{tag :link, attrs}"
42
+ end
43
+ end
44
+
45
+ def link(href, text=nil, attrs={}, &block)
46
+ attrs = {:href => href}.merge(attrs)
47
+ if block_given?
48
+ erb_concat "#{tag :a, attrs}#{capture_erb(&block)}</a>"
49
+ else
50
+ "#{tag :a, attrs}#{text ||= attrs[:href]}</a>"
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ require_relative "../lib/herbie.rb"
3
+
4
+ describe Herbie::Helpers do
5
+ include Herbie::Helpers
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
+ }
36
+
37
+ tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\" checked>"
38
+ end
39
+
40
+ describe "script helpers" do
41
+ it "should prefix the path to the script with ‘/javascripts’ if the script path is relative" do
42
+ script("script.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/javascripts/script.js\"></script>"
43
+ end
44
+
45
+ it "should output a script with an absolute path to the script if the path provided was absolute" do
46
+ script("/path/to/script.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/path/to/script.js\"></script>"
47
+ end
48
+
49
+ it "should output a script element with arbitrary javascript content provided by a block" do
50
+ pending "Need a mechanism for capturing erb output within a passed block"
51
+ script_block = Proc.new { "function hello_world(){ console.log(\"hello world!\"); }" }
52
+ script(&script_block).should == "<script type=\"text/javascript\" charset=\"utf-8\">\n#{capture_erb(&script_block)}\n</script>"
53
+ end
54
+ end
55
+
56
+ describe "stylesheet helpers" do
57
+ it "should prefix the path to the script with ‘/stylesheets’ if the stylesheet path is relative" do
58
+ style("foo.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/foo.css\">"
59
+ end
60
+
61
+ it "should output a link element with the absolute path to the stylesheet if the path provided was absolute" do
62
+ style("/style/foo.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/foo.css\">"
63
+ end
64
+
65
+ it "should output a link element with appropriate media query attribute if provided" do
66
+ media = "screen and (min-width:500px)"
67
+ style("/style/foo.css", :media => media).should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/foo.css\" media=\"#{media}\">"
68
+ end
69
+
70
+ it "should output a style element with arbitrary content provided by a block" do
71
+ pending "Need a mechanism for capturing erb output within a passed block"
72
+ media = "screen and (min-width:500px)"
73
+ css_block = Proc.new { "body { font-family:'Helvetica'; }" }
74
+ style({:media => media}, &css_block).should == "<script type=\"text/css\" media=\"#{media}\">\n#{capture_erb(&css_block)}\n</script>"
75
+ end
76
+ end
77
+
78
+ describe "link helpers" do
79
+ it "should be able to output a simple link" do
80
+ href = "http://www.foo.com/"
81
+ link(href).should == "<a href=\"#{href}\">#{href}</a>"
82
+ end
83
+
84
+ it "should be able to output a link with href and text" do
85
+ href = "http://www.foo.com/"
86
+ text = "Visit foo.com"
87
+ link(href, text).should == "<a href=\"#{href}\">#{text}</a>"
88
+ end
89
+
90
+ it "should be able to output links with arbitrary attributes" do
91
+ href = "http://www.foo.com/"
92
+ text = "Visit foo.com"
93
+ attrs = {
94
+ :class => "navigation",
95
+ :target => "_parent"
96
+ }
97
+ link(href, text, attrs).should == "<a href=\"#{href}\" class=\"#{attrs[:class]}\" target=\"#{attrs[:target]}\">#{text}</a>"
98
+ end
99
+
100
+ it "should be able to output a link enclosing arbitrary markup provided by a block" do
101
+ pending "Need a mechanism for capturing erb output within a passed block"
102
+ href = "http://www.foo.com/"
103
+ text = "Visit foo.com"
104
+ attrs = {
105
+ :class => "image"
106
+ }
107
+ markup_block = Proc.new { tag :img, :src => "foo.png" }
108
+ link(href, text, attrs, &markup_block).should == "<a href=\"#{href}\" class=\"#{attrs[:class]}\">#{capture_erb(&markup_block)}</a>"
109
+ end
110
+ end
111
+ end
File without changes
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: herbie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Darlow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70317520033180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70317520033180
25
+ description: Lovable HTML view helpers for use with ERB.
26
+ email: ben@kapowaz.net
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - lib/herbie/erb_helpers.rb
32
+ - lib/herbie/generic_helpers.rb
33
+ - lib/herbie/html_helpers.rb
34
+ - lib/herbie.rb
35
+ - spec/herbie_spec.rb
36
+ - spec/spec_helper.rb
37
+ homepage: http://github.com/kapowaz/herbie
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.9.2
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.6
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: herbie
61
+ test_files:
62
+ - spec/herbie_spec.rb
63
+ - spec/spec_helper.rb