rack-dev-mark 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWFhYjIwOWI1NzI3MzQ0YWZiYjFiNTJiMTgyMzdlNGM3ODU2YTMxYw==
4
+ MjgxZGYyYmNiNzI4ZDFhODBmZWZhOGU5ZjJiZTZiNzhiN2JmYTc5OA==
5
5
  data.tar.gz: !binary |-
6
- NzFhZjU3YzE5Zjg4ZGM1MGYzNzAzNDBiZWQ4ZmY0OTEwNDZjMzhjNA==
6
+ Y2VkNzU1ZTA0OTVmNmRjYWFkMGVhNTlhYjIxZGQ1ODQ5YzJhODliNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTdlYzU3OWNkMjE3ZDYxMTJkMjIzYjAzZWJjYjZjZDNhOWI4MTdhZmMwYWE1
10
- MmM5MWFmYjE1ZDUzNzA5MzkzMjdiZjBkMzQ4MmJkODAzY2ZjOTdmZjI0NjUw
11
- ZTQ5ZjVlOTM4ODlhNTE4NGRlN2RiZjQ3Y2NlODhkNTI0NTJlNzc=
9
+ NTFmNzFiNDA0MzExYTU5ZGRkZDE3ZjBmMDI4NmYwNWMxYWI3MmNjNWM3YmM2
10
+ YzYwNWE5YTUwNmNiN2QwZTA5OWNmYTc2NWY5NTgyMTNhMzM4ZjUyNjI5NjVk
11
+ NjljYjBhOTgwZjFhMzhhM2VjZGNjZGE3MWIwMGUzOTRlMzFmNDQ=
12
12
  data.tar.gz: !binary |-
13
- NTk2ZmFkZjgwNmNlYmE3YTE4ZjUzNTJjNDNhOTg0ZmMwNWY4ZGVlYzZjNzM5
14
- MjdhMjYwMTdmYzdkYjhlY2I1YzNmOTYwNjBiYTNmNWExZmNkZDYzZGRlMWNh
15
- NGI0ZDkwNDYyOTA0NTgyNTBlMmRhMzU1ZmQwNTUwMmM3MDZkMmY=
13
+ YmRmMjNmNGY3YmY1Nzk0ZDAwMDU1ZWZhNGViMDc2MGViNTc5NTU1MzdlMjMy
14
+ MDQ4OGYxYmUyM2Y0ZDMwOTNjOTEyODQ0OWQ5NmVkYzRiMTAxMjFmMzdhODVj
15
+ MDJlNzZiYjBkZTYwYTk4M2JjY2MwNmMyZmEyMGM0YzQyOTk4NDQ=
data/README.md CHANGED
@@ -100,7 +100,7 @@ heroku config:set RACK_DEV_MARK=staging
100
100
 
101
101
  ## Custom Theme
102
102
 
103
- Create your own theme class inheriting `Rack::DevMark::Theme::Base`.
103
+ Although the default themes are `title` and `github_fork_ribbon`, you can create your own themes inheriting `Rack::DevMark::Theme::Base`.
104
104
 
105
105
  ```ruby
106
106
  require 'rack/dev-mark/theme/base'
@@ -26,7 +26,7 @@ module Rack
26
26
  begin
27
27
  new_body << insert_dev_marks(b)
28
28
  rescue => e
29
- $stderr.write "Failed to insert dev marks: #{e.message}\n"
29
+ $stderr.write %Q|Failed to insert dev marks: #{e.message}\n #{e.backtrace.join(" \n")}|
30
30
  end
31
31
  end
32
32
  response.close if response.respond_to?(:close)
@@ -20,14 +20,34 @@ module MyApp
20
20
  end
21
21
  ```
22
22
 
23
+ ## tag
24
+
25
+ Insert the environment string into tags or attributes.
26
+
27
+ e.g.
28
+
29
+ `<div data-title="foo">foo</div>` can be `<div data-title="(env) foo">foo</div>`
30
+
31
+ ### options
32
+
33
+ `type`: `prefix` (default) or `postfix`
34
+
35
+ `upcase`: `true` or `false` (default)
36
+
37
+ `name`: the name of tag to insert env
38
+
39
+ `attribute`: the name of attribute to insert env
40
+
23
41
  ## title
24
42
 
25
- Just add the environment into the page title.
43
+ Just add the environment string into the page title.
26
44
 
27
45
  e.g.
28
46
 
29
47
  `My Homepage` on development env will be `(development) My Homepage`
30
48
 
49
+ options are the same as tag theme.
50
+
31
51
  ### options
32
52
 
33
53
  `type`: `prefix` (default) or `postfix`
@@ -17,9 +17,30 @@ module Rack
17
17
 
18
18
  end
19
19
 
20
+ private
21
+
20
22
  def stylesheet_link_tag(path)
21
23
  %Q~<style>#{::File.open(::File.join(::File.dirname(__FILE__), '../../../../vendor/assets/stylesheets', path)).read}</style>~
22
24
  end
25
+
26
+ def gsub_tag_content(html, name, &block)
27
+ String.new(html).gsub(%r{(<#{name}\s*[^>]*>)([^<]*)(</#{name}>)}i) do
28
+ "#{$1}#{block.call($2)}#{$3}"
29
+ end
30
+ end
31
+
32
+ def gsub_tag_attribute(html, name, attr, &block)
33
+ String.new(html).gsub %r{(<#{name}\s*)([^>]*)(>)}i do
34
+ s1, s2, s3 = $1, $2, $3
35
+ s2.gsub! %r{(#{attr}=')([^']*)(')} do
36
+ "#{$1}#{block.call($2)}#{$3}"
37
+ end
38
+ s2.gsub! %r{(#{attr}=")([^"]*)(")} do
39
+ "#{$1}#{block.call($2)}#{$3}"
40
+ end
41
+ "#{s1}#{s2}#{s3}"
42
+ end
43
+ end
23
44
  end
24
45
  end
25
46
  end
@@ -0,0 +1,46 @@
1
+ require 'rack/dev-mark/theme/base'
2
+
3
+ module Rack
4
+ module DevMark
5
+ module Theme
6
+ class Tag < Base
7
+ def initialize(options = {})
8
+ @options = options
9
+ end
10
+
11
+ def insert_into(html)
12
+ name = @options[:name]
13
+
14
+ if name
15
+ html = gsub_tag_content html, name do |value|
16
+ env_with(value)
17
+ end
18
+ end
19
+
20
+ if attribute = @options[:attribute]
21
+ Array(attribute).each do |attr|
22
+ html = gsub_tag_attribute html, name, attr do |value|
23
+ env_with(value)
24
+ end
25
+ end
26
+ end
27
+
28
+ html
29
+ end
30
+
31
+ private
32
+
33
+ def env_with(org)
34
+ s = env.to_s
35
+ s = s.upcase if @options[:upcase]
36
+
37
+ if @options[:type].to_s == 'postfix'
38
+ "#{org} (#{s})"
39
+ else
40
+ "(#{s}) #{org}"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,21 +1,11 @@
1
- require 'rack/dev-mark/theme/base'
1
+ require 'rack/dev-mark/theme/tag'
2
2
 
3
3
  module Rack
4
4
  module DevMark
5
5
  module Theme
6
- class Title < Base
6
+ class Title < Tag
7
7
  def initialize(options = {})
8
- @options = options
9
- end
10
-
11
- def insert_into(html)
12
- s = env.to_s
13
- s = s.upcase if @options[:upcase]
14
- if @options[:type].to_s == 'postfix'
15
- html.sub %r{(</title[^>]*>)}i, " (#{s})\\1"
16
- else
17
- html.sub %r{(<title[^>]*>)}i, "\\1(#{s}) "
18
- end
8
+ super options.merge(name: 'title', attribute: nil)
19
9
  end
20
10
  end
21
11
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module DevMark
3
- VERSION = '0.4.6'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -13,5 +13,54 @@ describe Rack::DevMark::Theme::Base do
13
13
  expect(subject.env).to eq('env')
14
14
  expect(subject.revision).to eq('rev')
15
15
  end
16
+ describe "#gsub_tag_content" do
17
+ let(:input) { %Q|<body><h1>head</h1><a href="something">x</a><span></span>y</div>| }
18
+ let(:output) { %Q|<body><h1>head</h1><a href="something">replaced</a><span></span>y</div>| }
19
+ it "replaces a string" do
20
+ expect(subject.send(:gsub_tag_content, input, 'a', &lambda{ |v| 'replaced' })).to eq(output)
21
+ end
22
+ context "for multiple tags" do
23
+ let(:input) { %Q|<body><h1>head</h1><a href="something">x</a><a href="anything">y</a></div>| }
24
+ let(:output) { %Q|<body><h1>head</h1><a href="something">replaced</a><a href="anything">replaced</a></div>| }
25
+ it "replaces a string" do
26
+ expect(subject.send(:gsub_tag_content, input, 'a', &lambda{ |v| 'replaced' })).to eq(output)
27
+ end
28
+ end
29
+ end
30
+ describe "#gsub_tag_attribute" do
31
+ let(:input) { %Q|<body><h1>head</h1><a href="something" data-title="x" data-body="x">x</a><span data-title="y">y</span></div>| }
32
+ let(:output) { %Q|<body><h1>head</h1><a href="something" data-title="replaced" data-body="x">x</a><span data-title="y">y</span></div>| }
33
+ it "replaces a string" do
34
+ expect(subject.send(:gsub_tag_attribute, input, 'a', 'data-title', &lambda{ |v| 'replaced' })).to eq(output)
35
+ end
36
+ context "for multiple tags" do
37
+ let(:input) { %Q|<body><h1>head</h1><a href="something" data-title="x" data-body="x">x</a><a href="anything" data-title="y">y</a></div>| }
38
+ let(:output) { %Q|<body><h1>head</h1><a href="something" data-title="replaced" data-body="x">x</a><a href="anything" data-title="replaced">y</a></div>| }
39
+ it "replaces a string" do
40
+ expect(subject.send(:gsub_tag_attribute, input, 'a', 'data-title', &lambda{ |v| 'replaced' })).to eq(output)
41
+ end
42
+ end
43
+ context "for single tag" do
44
+ let(:input) { %Q|<body><h1>head</h1><a href="something" data-title="x" data-body="x">x</a><span href="anything" data-title="y">y</span><img data-title="i"></div>| }
45
+ let(:output) { %Q|<body><h1>head</h1><a href="something" data-title="x" data-body="x">x</a><span href="anything" data-title="y">y</span><img data-title="replaced"></div>| }
46
+ it "replaces a string" do
47
+ expect(subject.send(:gsub_tag_attribute, input, 'img', 'data-title', &lambda{ |v| 'replaced' })).to eq(output)
48
+ end
49
+ end
50
+ context "for any tags" do
51
+ let(:input) { %Q|<body><h1>head</h1><a href="something" data-title="x" data-body="x">x</a><span href="anything" data-title="y">y</span></div>| }
52
+ let(:output) { %Q|<body><h1>head</h1><a href="something" data-title="replaced" data-body="x">x</a><span href="anything" data-title="replaced">y</span></div>| }
53
+ it "replaces a string" do
54
+ expect(subject.send(:gsub_tag_attribute, input, nil, 'data-title', &lambda{ |v| 'replaced' })).to eq(output)
55
+ end
56
+ end
57
+ context "with single quotations" do
58
+ let(:input) { %Q|<body><h1>head</h1><a href='something' data-title='x' data-body='x'>x</a><span data-title='y'>y</span></div>| }
59
+ let(:output) { %Q|<body><h1>head</h1><a href='something' data-title='replaced' data-body='x'>x</a><span data-title='y'>y</span></div>| }
60
+ it "replaces a string" do
61
+ expect(subject.send(:gsub_tag_attribute, input, 'a', 'data-title', &lambda{ |v| 'replaced' })).to eq(output)
62
+ end
63
+ end
64
+ end
16
65
  end
17
66
  end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::DevMark::Theme::Tag do
4
+ include_context "theme context"
5
+
6
+ let(:options) { {} }
7
+ subject { Rack::DevMark::Theme::Tag.new(options) }
8
+
9
+ it_behaves_like "theme" do
10
+ # No insert
11
+ end
12
+
13
+ describe "#env_with" do
14
+ context "upcase is true" do
15
+ let(:options) { {upcase: true} }
16
+ it "replace a string" do
17
+ expect(subject.send(:env_with, 'foo')).to eq('(ENV) foo')
18
+ end
19
+ end
20
+ context "upcase is false" do
21
+ let(:options) { {upcase: false} }
22
+ it "replace a string" do
23
+ expect(subject.send(:env_with, 'foo')).to eq('(env) foo')
24
+ end
25
+ end
26
+ context "type is prefix" do
27
+ let(:options) { {type: 'prefix'} }
28
+ it "replace a string" do
29
+ expect(subject.send(:env_with, 'foo')).to eq('(env) foo')
30
+ end
31
+ end
32
+ context "type is postfix" do
33
+ let(:options) { {type: 'postfix'} }
34
+ it "replace a string" do
35
+ expect(subject.send(:env_with, 'foo')).to eq('foo (env)')
36
+ end
37
+ end
38
+ end
39
+
40
+ context "with name option" do
41
+ let(:options) { {name: 'div'} }
42
+ context "with one match" do
43
+ it_behaves_like "theme" do
44
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><span>bar</span></body></html>~ }
45
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div>(env) foo</div><span>bar</span></body></html>~ }
46
+ end
47
+ end
48
+ context "with multiple matches" do
49
+ it_behaves_like "theme" do
50
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><div>bar</div></body></html>~ }
51
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div>(env) foo</div><div>(env) bar</div></body></html>~ }
52
+ end
53
+ end
54
+ context "no match" do
55
+ it_behaves_like "theme" do
56
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<span>foo</span><span>bar</span></body></html>~ }
57
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<span>foo</span><span>bar</span></body></html>~ }
58
+ end
59
+ end
60
+ end
61
+
62
+ context "with attribute option" do
63
+ context "one specified attribute" do
64
+ let(:options) { {attribute: 'data-title1' } }
65
+ context "with one match" do
66
+ it_behaves_like "theme" do
67
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div data-title1="foo">foo</div><div>bar</div></body></html>~ }
68
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div data-title1="(env) foo">foo</div><div>bar</div></body></html>~ }
69
+ end
70
+ end
71
+ context "with multiple matches" do
72
+ it_behaves_like "theme" do
73
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div data-title1="foo">foo</div><div data-title1="bar">bar</div></body></html>~ }
74
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div data-title1="(env) foo">foo</div><div data-title1="(env) bar">bar</div></body></html>~ }
75
+ end
76
+ end
77
+ context "no match" do
78
+ it_behaves_like "theme" do
79
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><div>bar</div></body></html>~ }
80
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><div>bar</div></body></html>~ }
81
+ end
82
+ end
83
+ end
84
+ context "multiple specified attributes" do
85
+ let(:options) { {attribute: ['data-title1', 'data-title2'] } }
86
+ context "with one match" do
87
+ it_behaves_like "theme" do
88
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div data-title1="foo" data-title2="abc">foo</div><div>bar</div></body></html>~ }
89
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div data-title1="(env) foo" data-title2="(env) abc">foo</div><div>bar</div></body></html>~ }
90
+ end
91
+ end
92
+ context "with multiple matches" do
93
+ it_behaves_like "theme" do
94
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div data-title1="foo" data-title2="abc">foo</div><div data-title1="bar" data-title2="xyz">bar</div></body></html>~ }
95
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div data-title1="(env) foo" data-title2="(env) abc">foo</div><div data-title1="(env) bar" data-title2="(env) xyz">bar</div></body></html>~ }
96
+ end
97
+ end
98
+ context "no match" do
99
+ it_behaves_like "theme" do
100
+ let(:src) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><div>bar</div></body></html>~ }
101
+ let(:out) { %Q~<html><head>head<title></title></head><body>body<div>foo</div><div>bar</div></body></html>~ }
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -2,35 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe Rack::DevMark::Theme::Title do
4
4
  include_context "theme context"
5
+
6
+ let(:options) { {} }
7
+ subject { Rack::DevMark::Theme::Title.new(options) }
8
+
5
9
  it_behaves_like "theme" do
10
+ let (:src) { %Q~<html><head>head<title>title</title></head><body>body</body></html>~ }
6
11
  let (:out) { %Q~<html><head>head<title>(env) title</title></head><body>body</body></html>~ }
7
- subject { Rack::DevMark::Theme::Title.new }
8
- end
9
- context "upcase is true" do
10
- let (:out) { %Q~<html><head>head<title>(ENV) title</title></head><body>body</body></html>~ }
11
- subject { Rack::DevMark::Theme::Title.new(upcase: true) }
12
- it_behaves_like "theme"
13
- end
14
- context "upcase is true" do
15
- let (:out) { %Q~<html><head>head<title>(env) title</title></head><body>body</body></html>~ }
16
- subject { Rack::DevMark::Theme::Title.new(upcase: false) }
17
- it_behaves_like "theme"
18
- end
19
- context "type is prefix" do
20
- let (:out) { %Q~<html><head>head<title>(env) title</title></head><body>body</body></html>~ }
21
- subject { Rack::DevMark::Theme::Title.new(type: 'prefix') }
22
- it_behaves_like "theme"
23
- end
24
- context "type is postfix" do
25
- let (:out) { %Q~<html><head>head<title>title (env)</title></head><body>body</body></html>~ }
26
- subject { Rack::DevMark::Theme::Title.new(type: 'postfix') }
27
- it_behaves_like "theme"
28
- end
29
- context "no title tag" do
30
- let (:src) { %Q~<html><head>head</head><body>body</body></html>~ }
31
- let (:out) { %Q~<html><head>head</head><body>body</body></html>~ }
32
- it "does not insert anything" do
33
- expect(subject.insert_into(src)).to eq(src)
34
- end
35
12
  end
36
13
  end
@@ -30,8 +30,8 @@ describe Rack::DevMark do
30
30
  end
31
31
  describe "::env=" do
32
32
  it "sets custom env" do
33
- Rack::DevMark.env = 'custom'
34
- expect(Rack::DevMark.env).to eq('custom')
33
+ subject.env = 'custom'
34
+ expect(subject.env).to eq('custom')
35
35
  end
36
36
  end
37
37
  describe "::revision" do
@@ -51,8 +51,8 @@ describe Rack::DevMark do
51
51
  end
52
52
  describe "::revision=" do
53
53
  it "sets custom revision" do
54
- Rack::DevMark.revision = 'custom'
55
- expect(Rack::DevMark.revision).to eq('custom')
54
+ subject.revision = 'custom'
55
+ expect(subject.revision).to eq('custom')
56
56
  end
57
57
  end
58
58
  end
@@ -10,6 +10,7 @@ end
10
10
 
11
11
  RSpec.shared_examples "theme" do
12
12
  let (:src) { %Q~<html><head>head<title>title</title></head><body>body</body></html>~ }
13
+ let (:out) { %Q~<html><head>head<title>title</title></head><body>body</body></html>~ }
13
14
  it "inserts env mark" do
14
15
  expect(subject.insert_into(src)).to eq(out)
15
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-dev-mark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Taniwaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -98,6 +98,7 @@ files:
98
98
  - lib/rack/dev-mark/theme/base.rb
99
99
  - lib/rack/dev-mark/theme/github_fork_ribbon.rb
100
100
  - lib/rack/dev-mark/theme/screenshots/github_fork_ribbon.png
101
+ - lib/rack/dev-mark/theme/tag.rb
101
102
  - lib/rack/dev-mark/theme/title.rb
102
103
  - lib/rack/dev-mark/utils.rb
103
104
  - lib/rack/dev-mark/version.rb
@@ -110,6 +111,7 @@ files:
110
111
  - spec/rack/dev-mark/railtie_spec.rb
111
112
  - spec/rack/dev-mark/theme/base_spec.rb
112
113
  - spec/rack/dev-mark/theme/github_fork_ribbon_spec.rb
114
+ - spec/rack/dev-mark/theme/tag_spec.rb
113
115
  - spec/rack/dev-mark/theme/title_spec.rb
114
116
  - spec/rack/dev-mark/utils_spec.rb
115
117
  - spec/rack/dev-mark_spec.rb
@@ -148,6 +150,7 @@ test_files:
148
150
  - spec/rack/dev-mark/railtie_spec.rb
149
151
  - spec/rack/dev-mark/theme/base_spec.rb
150
152
  - spec/rack/dev-mark/theme/github_fork_ribbon_spec.rb
153
+ - spec/rack/dev-mark/theme/tag_spec.rb
151
154
  - spec/rack/dev-mark/theme/title_spec.rb
152
155
  - spec/rack/dev-mark/utils_spec.rb
153
156
  - spec/rack/dev-mark_spec.rb