deas-erbtags 0.1.0 → 0.2.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/README.md CHANGED
@@ -24,26 +24,61 @@ class MyDeasServer
24
24
  end
25
25
  ```
26
26
 
27
- ## Tags
27
+ ## Methods
28
28
 
29
- All helpers are based off of the basic `tag` helper:
29
+ All helper methods are based off of the basic `tag` method. Tag calls should be invoked with `<%= ... -%>`:
30
+
31
+ ```erb
32
+ <%= tag(:br) -%> # => <br />
33
+ <%= tag(:h1, 'shizam', :title => "boom") -%> # => <h1 title="boom">shizam</h1>
34
+ ```
35
+
36
+ **Note**: all tag methods take an option hash of html attributes (like above). This use-case is assumed in the examples below.
37
+
38
+ ### `capture_tag`
39
+
40
+ 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
+
42
+ ```erb
43
+ <% content_tag(:div, :id => 'outer') do %>
44
+ <%= tag(:div, 'inner') -%>
45
+ <% end %>
46
+ # => <div id="outer">\n <div>inner</div>\n</div>\n
47
+ ```
48
+
49
+ **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
+
51
+ ### `link_to`
30
52
 
31
53
  ```ruby
32
- tag(:br) # => <br />
33
- tag(:h1, 'shizam', :title => "boom") # => <h1 title="boom">shizam</h1>
54
+ link_to "http://google.com"
55
+ # => <a href="http://google.com">http://google.com</a>
56
+
57
+ link_to "google", "http://google.com"
58
+ # => <a href="http://google.com">google</a>
59
+
60
+ link_to("http://google.com"){ tag(:span, 'google') }
61
+ # => <a href="http://google.com">\n<span>google</span>\n</a>\n
34
62
  ```
35
63
 
36
- ### LinkTo
64
+ ### `mail_to`
37
65
 
38
- TODO
66
+ ```ruby
67
+ mail_to "me@domain.com"
68
+ # => <a href="mailto:me@domain.com">me@domain.com</a>
39
69
 
40
- ### MailTo
70
+ mail_to "me@domain.com", :at => "_at_", :dot => "_dot_"
71
+ # => <a href="mailto:me@domain.com">me_at_domain_dot_com</a>
41
72
 
42
- TODO
73
+ mail_to "me@domain.com", :disabled => true
74
+ # => me@domain.com
75
+ ```
43
76
 
44
- ### ImageTag
77
+ ### `image_tag`
45
78
 
46
- TODO
79
+ ```ruby
80
+ image_tag '/logo.jpg' # => <img src="/logo.jpg" />
81
+ ```
47
82
 
48
83
  ### FormTags
49
84
 
@@ -0,0 +1,37 @@
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
@@ -0,0 +1,22 @@
1
+ require 'deas-erbtags/tag'
2
+
3
+ module Deas::ErbTags
4
+ module ImageTag
5
+
6
+ def self.included(receiver)
7
+ receiver.class_eval{ include Tag, Method }
8
+ end
9
+
10
+ module Method
11
+
12
+ def image_tag(source, *args)
13
+ opts = args.last.kind_of?(::Hash) ? args.pop : {}
14
+ opts.update(:src => source.to_s)
15
+
16
+ tag(:img, opts)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ require 'deas-erbtags/tag'
2
+ require 'deas-erbtags/capture_tag'
3
+
4
+ module Deas::ErbTags
5
+ module LinkTo
6
+
7
+ def self.included(receiver)
8
+ receiver.class_eval{ include Tag, CaptureTag, Method }
9
+ end
10
+
11
+ module Method
12
+
13
+ def link_to(*args, &block)
14
+ opts, href, content = [
15
+ args.last.kind_of?(::Hash) ? args.pop : {},
16
+ args.pop,
17
+ args.last
18
+ ]
19
+ opts.update(:href => href.to_s) if !href.nil?
20
+
21
+ if block_given?
22
+ capture_tag(:a, opts, &block)
23
+ else
24
+ tag(:a, content || href, opts)
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'deas-erbtags/tag'
2
+ require 'deas-erbtags/link_to'
3
+
4
+ module Deas::ErbTags
5
+ module MailTo
6
+
7
+ def self.included(receiver)
8
+ receiver.class_eval{ include Tag, LinkTo, Method }
9
+ end
10
+
11
+ module Method
12
+
13
+ def mail_to(*args)
14
+ opts, email, content = [
15
+ args.last.kind_of?(::Hash) ? args.pop : {},
16
+ args.pop,
17
+ args.last
18
+ ]
19
+ display = (content || email).dup
20
+ display.gsub!(/@/, opts.delete(:at)) if opts.has_key?(:at)
21
+ display.gsub!(/\./, opts.delete(:dot)) if opts.has_key?(:dot)
22
+
23
+ return display if opts.delete(:disabled)
24
+ link_to(display, "mailto: #{email}", opts)
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -10,7 +10,7 @@ module Deas::ErbTags
10
10
  ]
11
11
  attrs = U.html_attrs(opts)
12
12
 
13
- "<#{name}#{attrs}#{content ? ">#{content}</#{name}" : ' /'}>"
13
+ "<#{name}#{attrs}#{content.nil? ? ' /' : ">#{content}</#{name}"}>"
14
14
  end
15
15
 
16
16
  end
@@ -3,6 +3,16 @@ module Deas::ErbTags
3
3
 
4
4
  module Utils
5
5
 
6
+ ESCAPE_ATTRS = {
7
+ "&" => "&amp;",
8
+ "<" => "&lt;",
9
+ '"' => "&quot;"
10
+ }
11
+ ESCAPE_ATTRS_PATTERN = Regexp.union(*ESCAPE_ATTRS.keys)
12
+ def self.escape_attr_value(value)
13
+ value.to_s.gsub(ESCAPE_ATTRS_PATTERN){|c| ESCAPE_ATTRS[c] }
14
+ end
15
+
6
16
  def self.html_attrs(attrs="", ns=nil)
7
17
  return attrs.to_s if !attrs.kind_of?(::Hash)
8
18
 
@@ -19,16 +29,6 @@ module Deas::ErbTags
19
29
  end.join
20
30
  end
21
31
 
22
- ESCAPE_ATTRS = {
23
- "&" => "&amp;",
24
- "<" => "&lt;",
25
- '"' => "&quot;"
26
- }
27
- ESCAPE_ATTRS_PATTERN = Regexp.union(*ESCAPE_ATTRS.keys)
28
- def self.escape_attr_value(value)
29
- value.to_s.gsub(ESCAPE_ATTRS_PATTERN){|c| ESCAPE_ATTRS[c] }
30
- end
31
-
32
32
  end
33
33
 
34
34
  # alias for brevity
@@ -1,4 +1,4 @@
1
1
  module Deas; end
2
2
  module Deas::ErbTags
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
data/lib/deas-erbtags.rb CHANGED
@@ -2,6 +2,10 @@ require 'deas-erbtags/version'
2
2
  require 'deas-erbtags/utils'
3
3
 
4
4
  require 'deas-erbtags/tag'
5
+ require 'deas-erbtags/capture_tag'
6
+ require 'deas-erbtags/link_to'
7
+ require 'deas-erbtags/mail_to'
8
+ require 'deas-erbtags/image_tag'
5
9
 
6
10
  module Deas; end
7
11
  module Deas::ErbTags
@@ -10,7 +14,7 @@ module Deas::ErbTags
10
14
 
11
15
  def self.included(receiver)
12
16
  receiver.class_eval do
13
- include Tag
17
+ include Tag, CaptureTag, LinkTo, MailTo, ImageTag
14
18
  end
15
19
  end
16
20
 
@@ -1,3 +1,4 @@
1
+ require 'ostruct'
1
2
  require 'deas-erbtags/utils'
2
3
  require 'deas-erbtags/tag'
3
4
 
@@ -6,7 +7,22 @@ module Factory
6
7
  def self.template(*included_modules)
7
8
  template_class = Class.new do
8
9
  include *included_modules
10
+
11
+ attr_reader :_out_buf
12
+ def initialize
13
+ @_out_buf = ""
14
+ end
15
+
16
+ # he expected API for the Deas template scope to access erb settings
17
+ def sinatra_call
18
+ OpenStruct.new({
19
+ :settings => OpenStruct.new({
20
+ :erb => { :outvar => '@_out_buf' }
21
+ })
22
+ })
23
+ end
9
24
  end
25
+
10
26
  template_class.new
11
27
  end
12
28
 
@@ -0,0 +1,49 @@
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
@@ -10,11 +10,11 @@ module Deas::ErbTags
10
10
  end
11
11
  subject{ @template }
12
12
 
13
- should have_imeths :tag
13
+ should have_imeths :tag, :link_to, :mail_to, :image_tag
14
14
 
15
15
  should "include all of the individual modules" do
16
16
  exp_modules = [
17
- Tag
17
+ Tag, CaptureTag, LinkTo, MailTo, ImageTag
18
18
  ]
19
19
 
20
20
  exp_modules.each do |m|
@@ -0,0 +1,35 @@
1
+ require 'assert'
2
+ require 'deas-erbtags/tag'
3
+ require 'deas-erbtags/image_tag'
4
+
5
+ module Deas::ErbTags::ImageTag
6
+
7
+ class BaseTests < Assert::Context
8
+ desc "the `ImageTag` module"
9
+ setup do
10
+ @template = Factory.template(Deas::ErbTags::ImageTag)
11
+ end
12
+ subject{ @template }
13
+
14
+ should have_imeth :image_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 an img tag with the given src" do
21
+ img = subject.tag(:img, {:src => '/logo.jpg'})
22
+ assert_equal img, subject.image_tag('/logo.jpg')
23
+ end
24
+
25
+ should "create an img tag with attrs" do
26
+ img = subject.tag(:img, {
27
+ :src => '/logo.jpg',
28
+ :id => 'image'
29
+ })
30
+ assert_equal img, subject.image_tag('/logo.jpg', :id => 'image')
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,60 @@
1
+ require 'assert'
2
+ require 'deas-erbtags/tag'
3
+ require 'deas-erbtags/capture_tag'
4
+ require 'deas-erbtags/link_to'
5
+
6
+ module Deas::ErbTags::LinkTo
7
+
8
+ class BaseTests < Assert::Context
9
+ desc "the `LinkTo` module"
10
+ setup do
11
+ @template = Factory.template(Deas::ErbTags::LinkTo)
12
+ end
13
+ subject{ @template }
14
+
15
+ should have_imeth :link_to
16
+
17
+ should "include the `Tag` module" do
18
+ assert_includes Deas::ErbTags::Tag, subject.class.included_modules
19
+ end
20
+
21
+ should "include the `CaptureTag` module" do
22
+ assert_includes Deas::ErbTags::CaptureTag, subject.class.included_modules
23
+ end
24
+
25
+ should "create an anchor tag with no content or href" do
26
+ no_href_content = subject.tag(:a)
27
+ assert_equal no_href_content, subject.link_to
28
+ end
29
+
30
+ should "create an anchor tag with just an href" do
31
+ href = subject.tag(:a, 'www.google.com', {:href => 'www.google.com'})
32
+ assert_equal href, subject.link_to('www.google.com')
33
+ end
34
+
35
+ should "create an anchor tag with an href and content" do
36
+ href_content = subject.tag(:a, 'google', {:href => 'www.google.com'})
37
+ assert_equal href_content, subject.link_to('google', 'www.google.com')
38
+ end
39
+
40
+ should "create an anchor tag with an href, content, and attrs" do
41
+ href_content_opts = subject.tag(:a, 'google', {
42
+ :href => 'www.google.com',
43
+ :id => 'google_link'
44
+ })
45
+ link_to = subject.link_to('google', 'www.google.com', {
46
+ :id => 'google_link'
47
+ })
48
+ assert_equal href_content_opts, link_to
49
+ end
50
+
51
+ should "create an anchor tag with an href and captured content" do
52
+ span = subject.tag(:span, 'google')
53
+ exp = subject.tag(:a, "\n#{span}\n", {:href => 'www.google.com'}) + "\n"
54
+
55
+ assert_equal exp, subject.link_to('www.google.com'){ span }
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,51 @@
1
+ require 'assert'
2
+ require 'deas-erbtags/tag'
3
+ require 'deas-erbtags/link_to'
4
+ require 'deas-erbtags/mail_to'
5
+
6
+ module Deas::ErbTags::MailTo
7
+
8
+ class BaseTests < Assert::Context
9
+ desc "the `MailTo` module"
10
+ setup do
11
+ @link_content = "my email"
12
+ @email = "me@domain.com"
13
+ @template = Factory.template(Deas::ErbTags::MailTo)
14
+ end
15
+ subject{ @template }
16
+
17
+ should have_imeth :mail_to
18
+
19
+ should "include the `Tag` module" do
20
+ assert_includes Deas::ErbTags::Tag, subject.class.included_modules
21
+ end
22
+
23
+ should "include the `LinkTo` module" do
24
+ assert_includes Deas::ErbTags::LinkTo, subject.class.included_modules
25
+ end
26
+
27
+ should "render with just an email address" do
28
+ link_tag = subject.link_to(@email, "mailto: #{@email}")
29
+ assert_equal link_tag, subject.mail_to(@email)
30
+ end
31
+
32
+ should "render with custom display value" do
33
+ link_tag = subject.link_to(@link_content, "mailto: #{@email}")
34
+ assert_equal link_tag, subject.mail_to(@link_content, @email)
35
+ end
36
+
37
+ should "render obfuscating the email address displayed" do
38
+ obfus = "me_at_domain_dot_com"
39
+ link_tag = subject.link_to(obfus, "mailto: #{@email}")
40
+ mailto = subject.mail_to(@email, :at => "_at_", :dot => "_dot_")
41
+
42
+ assert_equal link_tag, mailto
43
+ end
44
+
45
+ should "render with link disabled" do
46
+ assert_equal @email, subject.mail_to(@email, :disabled => true)
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -7,8 +7,8 @@ module Deas::ErbTags::Tag
7
7
  desc "the basic tag method"
8
8
  setup do
9
9
  @opts = { :class => 'big', :id => '1234' }
10
- @content = "Loud Noises"
11
10
  @opts_attrs = Factory.html_attrs(@opts)
11
+ @content = "Loud Noises"
12
12
  @template = Factory.template(Deas::ErbTags::Tag)
13
13
  end
14
14
  subject{ @template }
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: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-05-16 00:00:00 Z
19
+ date: 2013-05-20 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: assert
@@ -50,13 +50,21 @@ files:
50
50
  - Rakefile
51
51
  - deas-tags.gemspec
52
52
  - lib/deas-erbtags.rb
53
+ - lib/deas-erbtags/capture_tag.rb
54
+ - lib/deas-erbtags/image_tag.rb
55
+ - lib/deas-erbtags/link_to.rb
56
+ - lib/deas-erbtags/mail_to.rb
53
57
  - lib/deas-erbtags/tag.rb
54
58
  - lib/deas-erbtags/utils.rb
55
59
  - lib/deas-erbtags/version.rb
56
60
  - log/.gitkeep
57
61
  - test/helper.rb
58
62
  - test/support/factory.rb
63
+ - test/unit/capture_tag_tests.rb
59
64
  - test/unit/deas-erbtags_tests.rb
65
+ - test/unit/image_tag_tests.rb
66
+ - test/unit/link_to_tests.rb
67
+ - test/unit/mail_to_tests.rb
60
68
  - test/unit/tag_tests.rb
61
69
  - test/unit/utils_tests.rb
62
70
  - tmp/.gitkeep
@@ -96,6 +104,10 @@ summary: Deas template helpers for creating HTML tags using Erb.
96
104
  test_files:
97
105
  - test/helper.rb
98
106
  - test/support/factory.rb
107
+ - test/unit/capture_tag_tests.rb
99
108
  - test/unit/deas-erbtags_tests.rb
109
+ - test/unit/image_tag_tests.rb
110
+ - test/unit/link_to_tests.rb
111
+ - test/unit/mail_to_tests.rb
100
112
  - test/unit/tag_tests.rb
101
113
  - test/unit/utils_tests.rb