viewlet 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/README.md +3 -0
- data/lib/viewlet.rb +1 -1
- data/lib/viewlet/base.rb +6 -2
- data/lib/viewlet/haml.rb +24 -9
- data/lib/viewlet/template.rb +2 -0
- data/lib/viewlet/version.rb +1 -1
- data/spec/app/viewlets/attributes.html.haml +2 -0
- data/spec/app/viewlets/block_with_argument.html.haml +3 -0
- data/spec/app/viewlets/block_without_argument.html.haml +1 -0
- data/spec/app/viewlets/content.html.haml +1 -0
- data/spec/app/viewlets/div.html.haml +1 -0
- data/spec/haml/block_variables_with_arguments.html.haml +28 -0
- data/spec/haml/block_variables_without_arguments.html.haml +34 -0
- data/spec/haml/nested_viewlet.html.haml +11 -0
- data/spec/haml/tag.html.haml +4 -0
- data/spec/haml/variables_by_attributes.html.haml +9 -0
- data/spec/haml/variables_by_block.html.haml +24 -0
- data/spec/haml_syntax_spec.rb +21 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/viewlet_helper_spec.rb +65 -0
- data/viewlet.gemspec +5 -3
- metadata +93 -12
data/.rspec
ADDED
data/README.md
CHANGED
@@ -229,6 +229,9 @@ that could be used as HTML id.
|
|
229
229
|
= render :partial => "some_other_partial", :locals => {:member => member}
|
230
230
|
|
231
231
|
%password_strength_viewlet{:levels => %w(none weak good)}
|
232
|
+
|
233
|
+
%password_strength_viewlet
|
234
|
+
- levels %w(none weak good) # notice optional dash at the beginning
|
232
235
|
```
|
233
236
|
|
234
237
|
## Todo
|
data/lib/viewlet.rb
CHANGED
data/lib/viewlet/base.rb
CHANGED
@@ -29,7 +29,7 @@ module Viewlet
|
|
29
29
|
|
30
30
|
def _read_variable(name, *args, &block)
|
31
31
|
if @variables[name].is_a?(Proc)
|
32
|
-
@
|
32
|
+
@variables[name].call(*args)
|
33
33
|
else
|
34
34
|
@variables[name]
|
35
35
|
end
|
@@ -39,7 +39,11 @@ module Viewlet
|
|
39
39
|
@variables[name] = if block
|
40
40
|
# HAML changes argument-less block {|| } into block {|*a| }
|
41
41
|
# which makes block.arity to be -1 instead of just 0
|
42
|
-
block.arity == -1
|
42
|
+
if block.arity == -1
|
43
|
+
@view.capture(&block)
|
44
|
+
else
|
45
|
+
Proc.new { |*a| @view.capture(*a, &block) }
|
46
|
+
end
|
43
47
|
else
|
44
48
|
args.first
|
45
49
|
end
|
data/lib/viewlet/haml.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require "active_support/core_ext/object/try"
|
2
|
+
require "active_support/core_ext/module/aliasing"
|
3
|
+
|
1
4
|
Haml::Parser.class_eval do
|
2
5
|
# `%simple_viewlet` becomes `= viewlet(:simple)`
|
3
6
|
# `%complex_viewlet` becomes `= viewlet(:complex) do |viewlet_0|`
|
@@ -14,10 +17,19 @@ Haml::Parser.class_eval do
|
|
14
17
|
viewlet_pop
|
15
18
|
end
|
16
19
|
|
20
|
+
# `- prop "whatever"` becomes `- viewlet_0.prop "whatever"`
|
21
|
+
def silent_script_with_viewlet_parent_tag(text)
|
22
|
+
if viewlet_definition?
|
23
|
+
text = "- #{viewlet_peek}.#{text.gsub(/^\-\s*/, "")}"
|
24
|
+
end
|
25
|
+
silent_script_without_viewlet_parent_tag(text)
|
26
|
+
end
|
27
|
+
alias_method_chain :silent_script, :viewlet_parent_tag
|
28
|
+
|
17
29
|
# `prop "whatever"` becomes `- viewlet_0.prop "whatever"`
|
18
|
-
def plain_with_viewlet_parent_tag(text, escape_html
|
30
|
+
def plain_with_viewlet_parent_tag(text, escape_html=nil)
|
19
31
|
if viewlet_definition?
|
20
|
-
|
32
|
+
silent_script_without_viewlet_parent_tag(" #{viewlet_peek}.#{text}")
|
21
33
|
else
|
22
34
|
plain_without_viewlet_parent_tag(text, escape_html)
|
23
35
|
end
|
@@ -28,18 +40,21 @@ Haml::Parser.class_eval do
|
|
28
40
|
|
29
41
|
def build_viewlet_node(node)
|
30
42
|
name = node.value[:name].gsub(/_viewlet$/, "")
|
31
|
-
attrs = attributes_string(node
|
43
|
+
attrs = attributes_string(node)
|
32
44
|
block = "do |#{viewlet_push}|" if block_opened?
|
33
45
|
script(" viewlet(:#{name} #{attrs}) #{block}")
|
34
46
|
end
|
35
47
|
|
36
|
-
def attributes_string(
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
48
|
+
def attributes_string(node)
|
49
|
+
return ", #{node.value[:attributes].inspect}" \
|
50
|
+
unless node.value[:attributes].empty?
|
51
|
+
|
52
|
+
if (hashes = node.value[:attributes_hashes]).empty?
|
53
|
+
""
|
54
|
+
elsif hashes.size == 1
|
55
|
+
", #{hashes.first}"
|
41
56
|
else
|
42
|
-
|
57
|
+
", (#{hashes.join(").merge(")})"
|
43
58
|
end
|
44
59
|
end
|
45
60
|
|
data/lib/viewlet/template.rb
CHANGED
data/lib/viewlet/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
= text
|
@@ -0,0 +1 @@
|
|
1
|
+
= text
|
@@ -0,0 +1 @@
|
|
1
|
+
%div
|
@@ -0,0 +1,28 @@
|
|
1
|
+
%block_with_argument_viewlet
|
2
|
+
text { |arg| "block-curly-#{arg}" }
|
3
|
+
|
4
|
+
%block_with_argument_viewlet
|
5
|
+
- text { |arg| "block-curly-dash-#{arg}" }
|
6
|
+
|
7
|
+
%block_with_argument_viewlet
|
8
|
+
text do |arg|
|
9
|
+
= "block-do-#{arg}"
|
10
|
+
|
11
|
+
%block_with_argument_viewlet
|
12
|
+
- text do |arg|
|
13
|
+
= "block-do-dash-#{arg}"
|
14
|
+
|
15
|
+
__END__
|
16
|
+
block-curly-method-argument
|
17
|
+
block-curly-capture-argument
|
18
|
+
block-curly-call-argument
|
19
|
+
block-curly-dash-method-argument
|
20
|
+
block-curly-dash-capture-argument
|
21
|
+
block-curly-dash-call-argument
|
22
|
+
block-do-method-argument
|
23
|
+
block-do-capture-argument
|
24
|
+
block-do-call-argument
|
25
|
+
|
26
|
+
block-do-dash-method-argument
|
27
|
+
block-do-dash-capture-argument
|
28
|
+
block-do-dash-call-argument
|
@@ -0,0 +1,34 @@
|
|
1
|
+
%block_without_argument_viewlet
|
2
|
+
text { "block-curly" }
|
3
|
+
|
4
|
+
%block_without_argument_viewlet
|
5
|
+
- text { "block-curly-dash" }
|
6
|
+
|
7
|
+
%block_without_argument_viewlet
|
8
|
+
text do
|
9
|
+
block-do
|
10
|
+
|
11
|
+
%block_without_argument_viewlet
|
12
|
+
- text do
|
13
|
+
block-do-dash
|
14
|
+
|
15
|
+
%block_without_argument_viewlet
|
16
|
+
text do
|
17
|
+
- block_var = "block-var" # use dash!
|
18
|
+
= block_var
|
19
|
+
|
20
|
+
%block_without_argument_viewlet
|
21
|
+
- text do
|
22
|
+
- block_var = "block-var-dash" # use dash!
|
23
|
+
= block_var
|
24
|
+
|
25
|
+
__END__
|
26
|
+
block-curly
|
27
|
+
block-curly-dash
|
28
|
+
block-do
|
29
|
+
|
30
|
+
block-do-dash
|
31
|
+
|
32
|
+
block-var
|
33
|
+
|
34
|
+
block-var-dash
|
@@ -0,0 +1,24 @@
|
|
1
|
+
%content_viewlet
|
2
|
+
text "simple"
|
3
|
+
|
4
|
+
%content_viewlet
|
5
|
+
- text "simple-dash"
|
6
|
+
|
7
|
+
- outside_local_var = "outside-local-var"
|
8
|
+
%content_viewlet
|
9
|
+
- text outside_local_var
|
10
|
+
|
11
|
+
- @outside_instance_var = "outside-instance-var"
|
12
|
+
%content_viewlet
|
13
|
+
- text @outside_instance_var
|
14
|
+
|
15
|
+
- extend Module.new(){ def outside_method; "outside-method"; end }
|
16
|
+
%content_viewlet
|
17
|
+
- text outside_method
|
18
|
+
|
19
|
+
__END__
|
20
|
+
simple
|
21
|
+
simple-dash
|
22
|
+
outside-local-var
|
23
|
+
outside-instance-var
|
24
|
+
outside-method
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "haml"
|
3
|
+
require "viewlet"
|
4
|
+
require "viewlet/haml"
|
5
|
+
|
6
|
+
describe "HAML syntax" do
|
7
|
+
let(:view) { SimpleView.new }
|
8
|
+
|
9
|
+
Dir[File.dirname(__FILE__) + "/haml/*.haml"].each do |f|
|
10
|
+
it("is able to render #{f}") { check_template(f) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def check_template(file_path)
|
14
|
+
output = Haml::Engine.new(File.read(file_path)).render(view, {})
|
15
|
+
template_piece(output, :first).should == template_piece(File.read(file_path), :last)
|
16
|
+
end
|
17
|
+
|
18
|
+
def template_piece(output, piece=:first)
|
19
|
+
output.split("__END__").send(piece).strip
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require "viewlet"
|
5
|
+
|
6
|
+
require "haml"
|
7
|
+
require "haml/template/plugin" # registers HAML with ActionView
|
8
|
+
|
9
|
+
module Rails # stub
|
10
|
+
def self.root
|
11
|
+
Pathname.new(File.expand_path(File.dirname(__FILE__)))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class SimpleView < ActionView::Base
|
16
|
+
include ActionView::Helpers::CaptureHelper
|
17
|
+
include Viewlet::Helpers
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.backtrace_clean_patterns = []
|
22
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "viewlet helper" do
|
4
|
+
let(:view) { SimpleView.new }
|
5
|
+
|
6
|
+
it "does not require a block" do
|
7
|
+
output = view.viewlet(:div)
|
8
|
+
output.should == "<div></div>\n"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "accepts a argument-less block" do
|
12
|
+
output = view.viewlet(:div) {}
|
13
|
+
output.should == "<div></div>\n"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "accepts a block with argument" do
|
17
|
+
output = view.viewlet(:div) { |v| }
|
18
|
+
output.should == "<div></div>\n"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "raises error when variable used in the template is not provided" do
|
22
|
+
expect {
|
23
|
+
view.viewlet(:content)
|
24
|
+
}.to raise_error(ActionView::Template::Error, /undefined local variable or method `text'/)
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "variable setting" do
|
28
|
+
it "allows to pass variables by hash" do
|
29
|
+
output = view.viewlet(:content, :text => "by-hash")
|
30
|
+
output.should == "by-hash\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "allows to pass variables by block" do
|
34
|
+
output = view.viewlet(:content) do |v|
|
35
|
+
v.text "by-block"
|
36
|
+
end
|
37
|
+
|
38
|
+
output.should == "by-block\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "overrides variables set by hash when set again by block" do
|
42
|
+
output = view.viewlet(:content, :text => "by-hash") do |v|
|
43
|
+
v.text "by-block"
|
44
|
+
end
|
45
|
+
|
46
|
+
output.should == "by-block\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "allows to set variable as a block" do
|
50
|
+
output = view.viewlet(:block_without_argument) do |v|
|
51
|
+
v.text { "no-arg" }
|
52
|
+
end
|
53
|
+
|
54
|
+
output.should == "no-arg\n"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "allows to set variable as a block that takes arguments" do
|
58
|
+
output = view.viewlet(:block_with_argument) do |v|
|
59
|
+
v.text { |arg| arg }
|
60
|
+
end
|
61
|
+
|
62
|
+
output.should == "method-argument\ncapture-argument\ncall-argument\n"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/viewlet.gemspec
CHANGED
@@ -16,7 +16,9 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
s.add_runtime_dependency "activesupport"
|
20
|
+
s.add_runtime_dependency "actionpack"
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "haml"
|
22
24
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viewlet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dmitriy Kalinin
|
@@ -15,10 +15,64 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-07-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
date: 2012-07-28 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: actionpack
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: haml
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
22
76
|
description: Rails view components
|
23
77
|
email:
|
24
78
|
- cppforlife@gmail.com
|
@@ -30,6 +84,7 @@ extra_rdoc_files: []
|
|
30
84
|
|
31
85
|
files:
|
32
86
|
- .gitignore
|
87
|
+
- .rspec
|
33
88
|
- Gemfile
|
34
89
|
- MIT.LICENSE
|
35
90
|
- README.md
|
@@ -46,8 +101,21 @@ files:
|
|
46
101
|
- lib/viewlet/railtie.rb
|
47
102
|
- lib/viewlet/template.rb
|
48
103
|
- lib/viewlet/version.rb
|
104
|
+
- spec/app/viewlets/attributes.html.haml
|
105
|
+
- spec/app/viewlets/block_with_argument.html.haml
|
106
|
+
- spec/app/viewlets/block_without_argument.html.haml
|
107
|
+
- spec/app/viewlets/content.html.haml
|
108
|
+
- spec/app/viewlets/div.html.haml
|
109
|
+
- spec/haml/block_variables_with_arguments.html.haml
|
110
|
+
- spec/haml/block_variables_without_arguments.html.haml
|
111
|
+
- spec/haml/nested_viewlet.html.haml
|
112
|
+
- spec/haml/tag.html.haml
|
113
|
+
- spec/haml/variables_by_attributes.html.haml
|
114
|
+
- spec/haml/variables_by_block.html.haml
|
115
|
+
- spec/haml_syntax_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/viewlet_helper_spec.rb
|
49
118
|
- viewlet.gemspec
|
50
|
-
has_rdoc: true
|
51
119
|
homepage: https://github.com/cppforlife/viewlet
|
52
120
|
licenses: []
|
53
121
|
|
@@ -77,9 +145,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
145
|
requirements: []
|
78
146
|
|
79
147
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.
|
148
|
+
rubygems_version: 1.8.24
|
81
149
|
signing_key:
|
82
150
|
specification_version: 3
|
83
151
|
summary: Rails view components
|
84
|
-
test_files:
|
85
|
-
|
152
|
+
test_files:
|
153
|
+
- spec/app/viewlets/attributes.html.haml
|
154
|
+
- spec/app/viewlets/block_with_argument.html.haml
|
155
|
+
- spec/app/viewlets/block_without_argument.html.haml
|
156
|
+
- spec/app/viewlets/content.html.haml
|
157
|
+
- spec/app/viewlets/div.html.haml
|
158
|
+
- spec/haml/block_variables_with_arguments.html.haml
|
159
|
+
- spec/haml/block_variables_without_arguments.html.haml
|
160
|
+
- spec/haml/nested_viewlet.html.haml
|
161
|
+
- spec/haml/tag.html.haml
|
162
|
+
- spec/haml/variables_by_attributes.html.haml
|
163
|
+
- spec/haml/variables_by_block.html.haml
|
164
|
+
- spec/haml_syntax_spec.rb
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/viewlet_helper_spec.rb
|