deas-erbtags 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,10 @@ end
26
26
 
27
27
  ## Methods
28
28
 
29
+ ```ruby
30
+ template_helpers(Deas::ErbTags::Tag)
31
+ ```
32
+
29
33
  All helper methods are based off of the basic `tag` method. Tag calls should be invoked with `<%= ... -%>`:
30
34
 
31
35
  ```erb
@@ -37,6 +41,10 @@ All helper methods are based off of the basic `tag` method. Tag calls should be
37
41
 
38
42
  ### `capture_tag`
39
43
 
44
+ ```ruby
45
+ template_helpers(Deas::ErbTags::Capture)
46
+ ```
47
+
40
48
  The `capture_tag` method is used to create tags with nested erb markup. Like the `tag` method, it creates other tags with a similar API. Unlike the `tag` method, it takes a block that contains nested markup and does not take content as an arg. Capture tags should be invoked with the `<% ... %>`:
41
49
 
42
50
  ```erb
@@ -48,9 +56,27 @@ The `capture_tag` method is used to create tags with nested erb markup. Like th
48
56
 
49
57
  **Note**: the convention is that if a tag method is called with a block, `capture_tag` will be used to generate the tag and it should be invoked with `<% ... %>`.
50
58
 
59
+ ### `capture_render`
60
+
61
+ ```ruby
62
+ template_helpers(Deas::ErbTags::Capture)
63
+ ```
64
+
65
+ The `capture_render` method is used to make Deas template `render` calls with nested erb markup. It calls `render` with the given args, and captures its output. Call it just as you would `render`, just invoke the call using `<% ... %>`.
66
+
67
+ ### `capture_render`
68
+
69
+ ```ruby
70
+ template_helpers(Deas::ErbTags::Capture)
71
+ ```
72
+
73
+ The `capture_partial` method is used to make Deas template `partial` calls with nested erb markup. It calls `partial` with the given args, and captures its output. Call it just as you would `partial`, just invoke the call using `<% ... %>`.
74
+
51
75
  ### `link_to`
52
76
 
53
77
  ```ruby
78
+ template_helpers(Deas::ErbTags::LinkTo)
79
+
54
80
  link_to "http://google.com"
55
81
  # => <a href="http://google.com">http://google.com</a>
56
82
 
@@ -64,6 +90,8 @@ link_to("http://google.com"){ tag(:span, 'google') }
64
90
  ### `mail_to`
65
91
 
66
92
  ```ruby
93
+ template_helpers(Deas::ErbTags::MailTo)
94
+
67
95
  mail_to "me@domain.com"
68
96
  # => <a href="mailto:me@domain.com">me@domain.com</a>
69
97
 
@@ -77,6 +105,8 @@ mail_to "me@domain.com", :disabled => true
77
105
  ### `image_tag`
78
106
 
79
107
  ```ruby
108
+ template_helpers(Deas::ErbTags::ImageTag)
109
+
80
110
  image_tag '/logo.jpg' # => <img src="/logo.jpg" />
81
111
  ```
82
112
 
data/lib/deas-erbtags.rb CHANGED
@@ -2,7 +2,7 @@ require 'deas-erbtags/version'
2
2
  require 'deas-erbtags/utils'
3
3
 
4
4
  require 'deas-erbtags/tag'
5
- require 'deas-erbtags/capture_tag'
5
+ require 'deas-erbtags/capture'
6
6
  require 'deas-erbtags/link_to'
7
7
  require 'deas-erbtags/mail_to'
8
8
  require 'deas-erbtags/image_tag'
@@ -14,7 +14,7 @@ module Deas::ErbTags
14
14
 
15
15
  def self.included(receiver)
16
16
  receiver.class_eval do
17
- include Tag, CaptureTag, LinkTo, MailTo, ImageTag
17
+ include Tag, Capture, LinkTo, MailTo, ImageTag
18
18
  end
19
19
  end
20
20
 
@@ -0,0 +1,56 @@
1
+ require 'deas-erbtags/tag'
2
+
3
+ module Deas::ErbTags
4
+ module Capture
5
+
6
+ def self.included(receiver)
7
+ receiver.class_eval{ include Tag, Methods }
8
+ end
9
+
10
+ module Methods
11
+
12
+ def erb_outvar_name
13
+ self.sinatra_call.settings.erb[:outvar]
14
+ end
15
+
16
+ def erb_outvar
17
+ instance_variable_get(self.erb_outvar_name)
18
+ end
19
+
20
+ def capture(content)
21
+ self.erb_outvar << "#{content}\n"
22
+ end
23
+
24
+ def capture_tag(name, *args, &content)
25
+ opts = args.last.kind_of?(::Hash) ? args.pop : {}
26
+ outvar = self.erb_outvar_name
27
+ captured_content = begin
28
+ orig_outvar = self.erb_outvar
29
+ instance_variable_set(outvar, "\n")
30
+
31
+ result = instance_eval(&content) if !content.nil?
32
+
33
+ if instance_variable_get(outvar) == "\n"
34
+ "\n#{result}"
35
+ else
36
+ instance_variable_get(outvar)
37
+ end
38
+ ensure
39
+ instance_variable_set(outvar, orig_outvar)
40
+ end
41
+
42
+ self.capture tag(name, "#{captured_content}\n", opts)
43
+ end
44
+
45
+ def capture_render(*args, &block)
46
+ self.capture self.render(*args, &block)
47
+ end
48
+
49
+ def capture_partial(*args, &block)
50
+ self.capture self.partial(*args, &block)
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -1,11 +1,11 @@
1
1
  require 'deas-erbtags/tag'
2
- require 'deas-erbtags/capture_tag'
2
+ require 'deas-erbtags/capture'
3
3
 
4
4
  module Deas::ErbTags
5
5
  module LinkTo
6
6
 
7
7
  def self.included(receiver)
8
- receiver.class_eval{ include Tag, CaptureTag, Method }
8
+ receiver.class_eval{ include Tag, Capture, Method }
9
9
  end
10
10
 
11
11
  module Method
@@ -1,4 +1,4 @@
1
1
  module Deas; end
2
2
  module Deas::ErbTags
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -21,6 +21,15 @@ module Factory
21
21
  })
22
22
  })
23
23
  end
24
+
25
+ def render(*args, &block)
26
+ [args, block].inspect
27
+ end
28
+
29
+ def partial(*args, &block)
30
+ [args, block].inspect
31
+ end
32
+
24
33
  end
25
34
 
26
35
  template_class.new
@@ -0,0 +1,80 @@
1
+ require 'assert'
2
+ require 'deas-erbtags/tag'
3
+ require 'deas-erbtags/capture'
4
+
5
+ module Deas::ErbTags::Capture
6
+
7
+ class BaseTests < Assert::Context
8
+ desc "the `Capture` module"
9
+ setup do
10
+ @template = Factory.template(Deas::ErbTags::Capture)
11
+ end
12
+ subject{ @template }
13
+
14
+ should have_imeths :erb_outvar_name, :erb_outvar
15
+ should have_imeths :capture, :capture_tag, :capture_render, :capture_partial
16
+
17
+ should "include the `Tag` module" do
18
+ assert_includes Deas::ErbTags::Tag, subject.class.included_modules
19
+ end
20
+
21
+ end
22
+
23
+ class CaptureTagTests < BaseTests
24
+
25
+ should "create content by capturing content from a given block" do
26
+ div_div = subject.tag(:div, "\n#{subject.tag(:div, "\ninner\n")}\n\n", {
27
+ :id => 'outer'
28
+ }) + "\n"
29
+ buf_content_div = subject.capture_tag(:div, :id => 'outer') do
30
+ capture_tag(:div){ 'inner' }
31
+ end
32
+
33
+ assert_equal div_div, buf_content_div
34
+ end
35
+
36
+ should "create content by returning content from a given block" do
37
+ div_div = subject.tag(:div, "\n#{subject.tag(:div, "inner")}\n", {
38
+ :id => 'outer'
39
+ }) + "\n"
40
+ returned_content_div = subject.capture_tag(:div, :id => 'outer') do
41
+ tag(:div, 'inner')
42
+ end
43
+
44
+ assert_equal div_div, returned_content_div
45
+ end
46
+
47
+ should "create empty tags if no block given" do
48
+ empty_div = subject.tag(:div, "\n\n", :id => 'outer') + "\n"
49
+ assert_equal empty_div, subject.capture_tag(:div, :id => 'outer')
50
+ end
51
+
52
+ end
53
+
54
+ class CaptureRenderTests < BaseTests
55
+
56
+ should "capture output from Deas' template `render` call" do
57
+ exp_output = "#{subject.render('something')}\n"
58
+ assert_empty subject._out_buf
59
+
60
+ cap_output = subject.capture_render('something')
61
+ assert_equal exp_output, cap_output
62
+ assert_equal exp_output, subject._out_buf
63
+ end
64
+
65
+ end
66
+
67
+ class CapturePartialTests < BaseTests
68
+
69
+ should "capture output from Deas' template `partial` call" do
70
+ exp_output = "#{subject.partial('something')}\n"
71
+ assert_empty subject._out_buf
72
+
73
+ cap_output = subject.capture_partial('something')
74
+ assert_equal exp_output, cap_output
75
+ assert_equal exp_output, subject._out_buf
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -14,7 +14,7 @@ module Deas::ErbTags
14
14
 
15
15
  should "include all of the individual modules" do
16
16
  exp_modules = [
17
- Tag, CaptureTag, LinkTo, MailTo, ImageTag
17
+ Tag, Capture, LinkTo, MailTo, ImageTag
18
18
  ]
19
19
 
20
20
  exp_modules.each do |m|
@@ -1,6 +1,6 @@
1
1
  require 'assert'
2
2
  require 'deas-erbtags/tag'
3
- require 'deas-erbtags/capture_tag'
3
+ require 'deas-erbtags/capture'
4
4
  require 'deas-erbtags/link_to'
5
5
 
6
6
  module Deas::ErbTags::LinkTo
@@ -18,8 +18,8 @@ module Deas::ErbTags::LinkTo
18
18
  assert_includes Deas::ErbTags::Tag, subject.class.included_modules
19
19
  end
20
20
 
21
- should "include the `CaptureTag` module" do
22
- assert_includes Deas::ErbTags::CaptureTag, subject.class.included_modules
21
+ should "include the `Capture` module" do
22
+ assert_includes Deas::ErbTags::Capture, subject.class.included_modules
23
23
  end
24
24
 
25
25
  should "create an anchor tag with no content or href" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deas-erbtags
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -50,7 +50,7 @@ files:
50
50
  - Rakefile
51
51
  - deas-tags.gemspec
52
52
  - lib/deas-erbtags.rb
53
- - lib/deas-erbtags/capture_tag.rb
53
+ - lib/deas-erbtags/capture.rb
54
54
  - lib/deas-erbtags/image_tag.rb
55
55
  - lib/deas-erbtags/link_to.rb
56
56
  - lib/deas-erbtags/mail_to.rb
@@ -60,7 +60,7 @@ files:
60
60
  - log/.gitkeep
61
61
  - test/helper.rb
62
62
  - test/support/factory.rb
63
- - test/unit/capture_tag_tests.rb
63
+ - test/unit/capture_tests.rb
64
64
  - test/unit/deas-erbtags_tests.rb
65
65
  - test/unit/image_tag_tests.rb
66
66
  - test/unit/link_to_tests.rb
@@ -104,7 +104,7 @@ summary: Deas template helpers for creating HTML tags using Erb.
104
104
  test_files:
105
105
  - test/helper.rb
106
106
  - test/support/factory.rb
107
- - test/unit/capture_tag_tests.rb
107
+ - test/unit/capture_tests.rb
108
108
  - test/unit/deas-erbtags_tests.rb
109
109
  - test/unit/image_tag_tests.rb
110
110
  - test/unit/link_to_tests.rb
@@ -1,37 +0,0 @@
1
- require 'deas-erbtags/tag'
2
-
3
- module Deas::ErbTags
4
- module CaptureTag
5
-
6
- def self.included(receiver)
7
- receiver.class_eval{ include Tag, Method }
8
- end
9
-
10
- module Method
11
-
12
- def capture_tag(name, *args)
13
- opts = args.last.kind_of?(::Hash) ? args.pop : {}
14
- outvar = self.sinatra_call.settings.erb[:outvar]
15
- captured_content = begin
16
- orig_outvar = instance_variable_get(outvar)
17
- instance_variable_set(outvar, "\n")
18
-
19
- result = yield if block_given?
20
-
21
- if instance_variable_get(outvar) == "\n"
22
- "\n#{result}"
23
- else
24
- instance_variable_get(outvar)
25
- end
26
- ensure
27
- instance_variable_set(outvar, orig_outvar)
28
- end
29
-
30
- tag_markup = tag(name, "#{captured_content}\n", opts)
31
- instance_variable_get(outvar) << "#{tag_markup}\n"
32
- end
33
-
34
- end
35
-
36
- end
37
- end
@@ -1,49 +0,0 @@
1
- require 'assert'
2
- require 'deas-erbtags/tag'
3
- require 'deas-erbtags/capture_tag'
4
-
5
- module Deas::ErbTags::CaptureTag
6
-
7
- class BaseTests < Assert::Context
8
- desc "the `CaptureTag` module"
9
- setup do
10
- @template = Factory.template(Deas::ErbTags::CaptureTag)
11
- end
12
- subject{ @template }
13
-
14
- should have_imeth :capture_tag
15
-
16
- should "include the `Tag` module" do
17
- assert_includes Deas::ErbTags::Tag, subject.class.included_modules
18
- end
19
-
20
- should "create content by capturing content from a given block" do
21
- div_div = subject.tag(:div, "\n#{subject.tag(:div, "\ninner\n")}\n\n", {
22
- :id => 'outer'
23
- }) + "\n"
24
- buf_content_div = subject.capture_tag(:div, :id => 'outer') do
25
- subject.capture_tag(:div){ subject._out_buf << 'inner' }
26
- end
27
-
28
- assert_equal div_div, buf_content_div
29
- end
30
-
31
- should "create content by returning content from a given block" do
32
- div_div = subject.tag(:div, "\n#{subject.tag(:div, "\ninner\n")}\n\n", {
33
- :id => 'outer'
34
- }) + "\n"
35
- returned_content_div = subject.capture_tag(:div, :id => 'outer') do
36
- subject.capture_tag(:div){ 'inner' }
37
- end
38
-
39
- assert_equal div_div, returned_content_div
40
- end
41
-
42
- should "create empty tags if no block given" do
43
- empty_div = subject.tag(:div, "\n\n", :id => 'outer') + "\n"
44
- assert_equal empty_div, subject.capture_tag(:div, :id => 'outer')
45
- end
46
-
47
- end
48
-
49
- end