markevans-block_helpers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mark Evans
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,141 @@
1
+ Block Helpers
2
+ =============
3
+
4
+ When we write ERB views in Rails, etc., we generally DRY up the markup using helpers or partials.
5
+
6
+ However, it's quite common to overdo the 'DRYing up'.
7
+
8
+ When you find yourself passing in optional arguments to a helper/partial such as `:extra_text => 'eggs', :to_s_method => 'cheese'`, you know that there must be a better way.
9
+
10
+ Rails already has a great solution for forms with form-builders, using helpers which yield an object which can be used for further rendering.
11
+
12
+ This small gem generates helpers similar to the form-builders, but for the general case.
13
+
14
+ Example usage
15
+ =============
16
+ Please note that these examples are very contrived just for brevity! These block helpers are much more useful than just printing 'Hi there Marmaduke!'
17
+
18
+ Simple case
19
+ -----------
20
+
21
+ In the helper file:
22
+
23
+ module MyHelper
24
+
25
+ class MyBlockHelper < BlockHelpers::BlockHelper
26
+
27
+ def hello(name)
28
+ "<p>Hi there #{name}!</p>"
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ This has generated a helper called `my_block_helper`.
36
+ So in the view:
37
+
38
+ <% my_block_helper do |h| %>
39
+ Here goes...
40
+ <%= h.hello('Marmaduke') %>
41
+ ...hooray!
42
+ <% end %>
43
+
44
+ This will generate the following:
45
+
46
+ Here goes...
47
+ <p>Hi there Marmaduke!</p>
48
+ ...hooray!
49
+
50
+ Using arguments
51
+ ---------------
52
+
53
+ You can pass in arguments to the helper, and these will be passed through to the class's `initialize` method.
54
+ In the helper:
55
+
56
+ module MyHelper
57
+
58
+ class MyBlockHelper < BlockHelpers::BlockHelper
59
+
60
+ def initialize(tag_type)
61
+ @tag_type = tag_type
62
+ end
63
+
64
+ def hello(name)
65
+ content_tag @tag_type, "Hi there #{name}!"
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ In the view:
73
+
74
+ <% my_block_helper(:span) do |h| %>
75
+ <%= h.hello('Marmaduke') %>
76
+ <% end %>
77
+
78
+ This generates:
79
+
80
+ <span>Hi there Marmaduke!</span>
81
+
82
+ Note that methods available in the helper (e.g. `content_tag`) are also available in the block helper class.
83
+
84
+ Surrounding markup
85
+ ------------------
86
+
87
+ Use the `to_s` method to surround the block with markup, e.g.
88
+ In the helper:
89
+
90
+ module MyHelper
91
+
92
+ class RoundedBox < BlockHelpers::BlockHelper
93
+
94
+ def to_s(body)
95
+ %(
96
+ <div class="tl">
97
+ <div class="tr">
98
+ <div class="bl">
99
+ <div class="br">
100
+ #{body}
101
+ </div>
102
+ </div>
103
+ </div>
104
+ </div>
105
+ )
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+
112
+ In the view:
113
+
114
+ <% rounded_box do %>
115
+ Oi oi!!!
116
+ <% end %>
117
+
118
+ This generates:
119
+
120
+ <div class="tl">
121
+ <div class="tr">
122
+ <div class="bl">
123
+ <div class="br">
124
+ Oi oi!!!
125
+ </div>
126
+ </div>
127
+ </div>
128
+ </div>
129
+
130
+
131
+ Installation
132
+ ============
133
+
134
+ To use in Rails, add to your `environment.rb`:
135
+
136
+ config.gem "markevans-block_helpers", :lib => "block_helpers", :source => "http://gems.github.com"
137
+
138
+ Copyright
139
+ ========
140
+
141
+ Copyright (c) 2009 Mark Evans. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "block_helpers"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "mark@new-bamboo.co.uk"
10
+ gem.homepage = "http://github.com/markevans/block_helpers"
11
+ gem.authors = ["Mark Evans"]
12
+ gem.add_dependency('activesupport', '>= 2.0')
13
+ gem.add_dependency('actionpack', '>= 2.0')
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION.yml')
39
+ config = YAML.load(File.read('VERSION.yml'))
40
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "block_helpers #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{block_helpers}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mark Evans"]
9
+ s.date = %q{2009-07-20}
10
+ s.email = %q{mark@new-bamboo.co.uk}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.markdown"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "block_helpers.gemspec",
23
+ "lib/block_helpers.rb",
24
+ "spec/for_spec_rails/application.rb",
25
+ "spec/helpers/block_helpers_spec.rb",
26
+ "spec/spec_helper.rb"
27
+ ]
28
+ s.has_rdoc = true
29
+ s.homepage = %q{http://github.com/markevans/block_helpers}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.1}
33
+ s.summary = %q{TODO}
34
+ s.test_files = [
35
+ "spec/for_spec_rails/application.rb",
36
+ "spec/helpers/block_helpers_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 2
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0"])
46
+ s.add_runtime_dependency(%q<actionpack>, [">= 2.0"])
47
+ else
48
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
49
+ s.add_dependency(%q<actionpack>, [">= 2.0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
53
+ s.add_dependency(%q<actionpack>, [">= 2.0"])
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ require 'activesupport'
2
+ require 'action_view'
3
+
4
+ module BlockHelpers
5
+
6
+ class BlockHelper
7
+
8
+ def self.inherited(klass)
9
+ # Define the helper method
10
+ # e.g. for a class:
11
+ # class HelloHelper < BlockHelpers::BlockHelper
12
+ # #.....
13
+ # end
14
+ #
15
+ # then we define a helper method 'hello_helper'
16
+ #
17
+ method_name = klass.name.split('::').last.underscore
18
+ klass.parent.class_eval %(
19
+ def #{method_name}(*args, &block)
20
+ renderer = #{klass.name}.new(*args)
21
+ if renderer.public_methods(false).include? 'to_s'
22
+ concat renderer.to_s(capture(renderer, &block))
23
+ else
24
+ block.call(renderer)
25
+ end
26
+ end
27
+ )
28
+ klass.class_eval do
29
+ include klass.parent
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,10 @@
1
+ # Hacks to get spec/rails to work
2
+
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+
6
+ module Rails
7
+ module VERSION
8
+ STRING = '2.3.0'
9
+ end
10
+ end
@@ -0,0 +1,97 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module TestHelperModule
4
+ end
5
+
6
+ describe TestHelperModule do
7
+
8
+ describe "simple block_helper" do
9
+
10
+ before(:each) do
11
+ class TestHelperModule::TestHelper < BlockHelpers::BlockHelper
12
+ def hello
13
+ 'Hi there'
14
+ end
15
+ end
16
+ end
17
+
18
+ it "should make the named helper available" do
19
+ helper.should respond_to(:test_helper)
20
+ end
21
+
22
+ it "should work for a simple yielded object" do
23
+ eval_erb(%(
24
+ <% test_helper do |h| %>
25
+ <p>Before</p>
26
+ <%= h.hello %>
27
+ <p>After</p>
28
+ <% end %>
29
+ )).should match_html("<p>Before</p> Hi there <p>After</p>")
30
+ end
31
+
32
+ end
33
+
34
+ describe "access to other methods" do
35
+ before(:each) do
36
+ module TestHelperModule
37
+ def yoghurt; 'Yoghurt'; end
38
+ class TestHelper
39
+ def yog
40
+ yoghurt[0..2]
41
+ end
42
+ end
43
+ end
44
+ end
45
+ it "should give the yielded renderer access to other methods" do
46
+ eval_erb(%(
47
+ <% test_helper do |r| %>
48
+ <%= r.yog %>
49
+ <% end %>
50
+ )).should match_html("Yog")
51
+ end
52
+ end
53
+
54
+ describe "surrounding the block" do
55
+
56
+ before(:each) do
57
+ class TestHelperModule::TestHelperSurround < BlockHelpers::BlockHelper
58
+ def to_s(body)
59
+ %(
60
+ <p>Before</p>
61
+ #{body}
62
+ <p>After</p>
63
+ )
64
+ end
65
+ end
66
+ end
67
+
68
+ it "should surround a simple block" do
69
+ eval_erb(%(
70
+ <% test_helper_surround do %>
71
+ Body here!!!
72
+ <% end %>
73
+ )).should match_html("<p>Before</p> Body here!!! <p>After</p>")
74
+ end
75
+ end
76
+
77
+ describe "block helpers with arguments" do
78
+ before(:each) do
79
+ class TestHelperModule::TestHelperWithArgs < BlockHelpers::BlockHelper
80
+ def initialize(id, klass)
81
+ @id, @klass = id, klass
82
+ end
83
+ def hello
84
+ %(<p class="#{@klass}" id="#{@id}">Hello</p>)
85
+ end
86
+ end
87
+ end
88
+ it "should use the args passed in" do
89
+ eval_erb(%(
90
+ <% test_helper_with_args('hello', 'there') do |r| %>
91
+ <%= r.hello %>
92
+ <% end %>
93
+ )).should match_html(%(<p class="there" id="hello">Hello</p>))
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ gem 'activesupport'
3
+ gem 'actionpack'
4
+ require File.dirname(__FILE__)+'/../lib/block_helpers'
5
+
6
+ # Hacks to get spec/rails to work
7
+ require 'action_controller'
8
+ $:.unshift File.dirname(__FILE__)+'/for_spec_rails'
9
+ RAILS_ENV = 'test'
10
+
11
+ require 'spec'
12
+ require 'spec/rails'
13
+
14
+ Spec::Runner.configure do |config|
15
+ end
16
+
17
+ def match_html(html)
18
+ # Match two strings, but don't care about whitespace
19
+ simple_matcher("should match #{html}"){|given| given.strip.gsub(/\s+/,' ').gsub('> <','><') == html.strip.gsub(/\s+/,' ').gsub('> <','><') }
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markevans-block_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "2.0"
34
+ version:
35
+ description:
36
+ email: mark@new-bamboo.co.uk
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.markdown
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.markdown
49
+ - Rakefile
50
+ - VERSION
51
+ - block_helpers.gemspec
52
+ - lib/block_helpers.rb
53
+ - spec/for_spec_rails/application.rb
54
+ - spec/helpers/block_helpers_spec.rb
55
+ - spec/spec_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/markevans/block_helpers
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: TODO
82
+ test_files:
83
+ - spec/for_spec_rails/application.rb
84
+ - spec/helpers/block_helpers_spec.rb
85
+ - spec/spec_helper.rb