syaso 0.0.1 → 0.0.2
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 +14 -0
- data/lib/syaso/base.rb +66 -11
- data/lib/syaso/list.rb +4 -5
- data/lib/syaso/list_item.rb +12 -0
- data/lib/syaso/version.rb +1 -1
- data/test/units/syaso/test_list.rb +3 -3
- data/test/units/syaso/test_list_item.rb +38 -0
- data/test/units/test_syaso.rb +59 -1
- metadata +4 -2
data/README.md
CHANGED
@@ -37,7 +37,21 @@ in app/views/SOME_VIEW.html.erb
|
|
37
37
|
<% end
|
38
38
|
<% end %>
|
39
39
|
|
40
|
+
HTML "data-*" attributes.
|
40
41
|
|
42
|
+
<%= Syaso.Base.new(self).render("test", :data => {:view => :syaso, :role => :view}) %>
|
43
|
+
<%#
|
44
|
+
#=> <div data-role="view" data-view="syaso">test</div>
|
45
|
+
%>
|
46
|
+
|
47
|
+
# or write simply,
|
48
|
+
|
49
|
+
<%= Syaso.Base.new(self).render("test", :_view => :syaso, :_role => :view) %>
|
50
|
+
<%#
|
51
|
+
#=> <div data-role="view" data-view="syaso">test</div>
|
52
|
+
%>
|
53
|
+
|
54
|
+
|
41
55
|
|
42
56
|
## Contributing
|
43
57
|
|
data/lib/syaso/base.rb
CHANGED
@@ -4,6 +4,8 @@ class Syaso::Base
|
|
4
4
|
#--------------------#
|
5
5
|
# constants
|
6
6
|
|
7
|
+
BLOCK_BUFFER = ""
|
8
|
+
|
7
9
|
#--------------------#
|
8
10
|
# attributes
|
9
11
|
|
@@ -21,12 +23,14 @@ class Syaso::Base
|
|
21
23
|
def initialize(context)
|
22
24
|
super()
|
23
25
|
self.context = context
|
24
|
-
#@@default_html_tag ||= :div
|
25
|
-
#@@default_html_class ||= []
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
# render content.
|
29
|
+
#
|
30
|
+
# @parma [Object] content
|
31
|
+
# @param [Hash] options
|
32
|
+
def render(content = nil, options = {}, &block)
|
33
|
+
self._render(content, options, &block)
|
30
34
|
end
|
31
35
|
#--------------------#
|
32
36
|
protected
|
@@ -36,22 +40,42 @@ class Syaso::Base
|
|
36
40
|
|
37
41
|
# render method
|
38
42
|
#
|
43
|
+
# @parma [Object] content
|
39
44
|
# @param [Hash] options
|
40
|
-
def _render(options = {}, &block)
|
41
|
-
ops =
|
45
|
+
def _render(content = nil, options = {}, &block)
|
46
|
+
ops = filter_args(content, options)
|
47
|
+
ops = filter_options(ops)
|
42
48
|
self.buffer do
|
43
49
|
self.context.content_tag(self.tag, ops) do
|
44
|
-
|
45
|
-
|
50
|
+
if block_given?
|
51
|
+
yield(self)
|
52
|
+
else
|
53
|
+
self.buffer do
|
54
|
+
self.content
|
55
|
+
end
|
46
56
|
end
|
47
57
|
end # context.content_tag
|
48
58
|
end # buffer
|
49
|
-
end #
|
59
|
+
end # _render
|
60
|
+
|
61
|
+
# block unexpected buffer
|
62
|
+
def block_buffer(&block)
|
63
|
+
yield if block_given?
|
64
|
+
BLOCK_BUFFER
|
65
|
+
end
|
50
66
|
|
51
67
|
# buffer contents
|
52
68
|
def buffer(&block)
|
53
|
-
|
54
|
-
|
69
|
+
block_buffer do
|
70
|
+
self.context.concat(yield)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# default html data-* attibutes
|
75
|
+
#
|
76
|
+
# @param [Hash]
|
77
|
+
def default_data_options
|
78
|
+
{}
|
55
79
|
end
|
56
80
|
|
57
81
|
# default tag
|
@@ -71,6 +95,20 @@ class Syaso::Base
|
|
71
95
|
#--------------------#
|
72
96
|
private
|
73
97
|
|
98
|
+
# filter render arguments
|
99
|
+
#
|
100
|
+
# @param [Object] content
|
101
|
+
# @param [Hash] options
|
102
|
+
def filter_args(content = nil, options = {})
|
103
|
+
if (content.is_a?(Hash)) && (options.empty?)
|
104
|
+
ops = content
|
105
|
+
else
|
106
|
+
self.content = ((content.nil?) || (content.empty?)) ? self.content : content
|
107
|
+
ops = options
|
108
|
+
end
|
109
|
+
ops
|
110
|
+
end
|
111
|
+
|
74
112
|
# filter html options
|
75
113
|
#
|
76
114
|
# @param [Hash] options
|
@@ -84,6 +122,23 @@ class Syaso::Base
|
|
84
122
|
|
85
123
|
self.tag = ops.delete(:tag)||self.default_html_tag
|
86
124
|
ops = filter_html_class_options(ops)
|
125
|
+
ops = filter_data_options(ops)
|
126
|
+
ops
|
127
|
+
end
|
128
|
+
|
129
|
+
# filter data options
|
130
|
+
#
|
131
|
+
# @param [Hash] options
|
132
|
+
def filter_data_options(options = {})
|
133
|
+
ops = self.default_data_options.merge(options)
|
134
|
+
|
135
|
+
options.each do |k, v|
|
136
|
+
if k.to_s.match(/^_/)
|
137
|
+
key = "data-#{k.to_s.sub(/^_/, "").gsub(/_/, "-")}"
|
138
|
+
ops[key] = v
|
139
|
+
ops.delete(k)
|
140
|
+
end
|
141
|
+
end
|
87
142
|
ops
|
88
143
|
end
|
89
144
|
|
data/lib/syaso/list.rb
CHANGED
@@ -24,9 +24,7 @@ class Syaso::List < Syaso::Base
|
|
24
24
|
def render(options = {}, &block)
|
25
25
|
ops = options.delete(:item)||{}
|
26
26
|
self.each(options) do |i|
|
27
|
-
|
28
|
-
i.render(ops, &block)
|
29
|
-
end
|
27
|
+
i.render(ops, &block)
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
@@ -34,11 +32,10 @@ class Syaso::List < Syaso::Base
|
|
34
32
|
#
|
35
33
|
# @param [Hash] options
|
36
34
|
def each(options = {}, &block)
|
37
|
-
self._render(options) do |
|
35
|
+
self._render(options) do |v|
|
38
36
|
self.content.each do |i|
|
39
37
|
yield(self.item_view(i))
|
40
38
|
end
|
41
|
-
""
|
42
39
|
end
|
43
40
|
end
|
44
41
|
|
@@ -53,6 +50,8 @@ class Syaso::List < Syaso::Base
|
|
53
50
|
end
|
54
51
|
|
55
52
|
# default html tag
|
53
|
+
#
|
54
|
+
# @return [Symbol]
|
56
55
|
def default_html_tag
|
57
56
|
:ul
|
58
57
|
end
|
data/lib/syaso/list_item.rb
CHANGED
@@ -3,12 +3,20 @@
|
|
3
3
|
require "syaso/base"
|
4
4
|
|
5
5
|
class Syaso::ListItem < Syaso::Base
|
6
|
+
# initialize instance
|
7
|
+
#
|
8
|
+
# @param [ActionView] context
|
9
|
+
# @param [Syaso::Base] holder
|
10
|
+
# @param [Object] content
|
6
11
|
def initialize(context, holder, content)
|
7
12
|
super(context)
|
8
13
|
self.holder = holder
|
9
14
|
self.content = content
|
10
15
|
end
|
11
16
|
|
17
|
+
# index of this content in list's content.
|
18
|
+
#
|
19
|
+
# @return [Integer]
|
12
20
|
def index
|
13
21
|
self.holder.index(self.content)
|
14
22
|
end
|
@@ -16,8 +24,12 @@ class Syaso::ListItem < Syaso::Base
|
|
16
24
|
#--------------------#
|
17
25
|
protected
|
18
26
|
|
27
|
+
# ListItem holder
|
19
28
|
attr_writer :holder
|
20
29
|
|
30
|
+
# default html tag
|
31
|
+
#
|
32
|
+
# @return [Symbol]
|
21
33
|
def default_html_tag
|
22
34
|
:li
|
23
35
|
end
|
data/lib/syaso/version.rb
CHANGED
@@ -19,16 +19,16 @@ class TestSyasoList < Test::Unit::TestCase
|
|
19
19
|
should "respond to render items" do
|
20
20
|
assert_nothing_raised(Exception) do
|
21
21
|
@list.render(:class => [:container]) do |item|
|
22
|
-
item.content
|
22
|
+
@context.concat(item.content)
|
23
23
|
end # list.render
|
24
24
|
end # assert
|
25
25
|
end # should "respond to render items"
|
26
26
|
|
27
27
|
should "respond to render item" do
|
28
28
|
assert_nothing_raised(Exception) do
|
29
|
-
@list.each
|
29
|
+
@list.each do |item|
|
30
30
|
item.render(:tag => :p, :id => lambda {|i| "item_#{i.content}"}) do |i|
|
31
|
-
i.content
|
31
|
+
@context.concat(i.content)
|
32
32
|
end # item.render
|
33
33
|
end # list.each
|
34
34
|
end # assert
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
class TestSyasoListItem < Test::Unit::TestCase
|
6
|
+
context "Syaso::ListItem" do
|
7
|
+
setup do
|
8
|
+
setup_view
|
9
|
+
@item = 1
|
10
|
+
@list = Syaso::List.new(@context, [@item])
|
11
|
+
@view = Syaso::ListItem.new(@context, @list, @item)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "render" do
|
15
|
+
@view.render do |i|
|
16
|
+
@context.concat(i.content)
|
17
|
+
end
|
18
|
+
|
19
|
+
assert(output.match(/^<li[^<]*>.*<\/li>$/))
|
20
|
+
end # should "render"
|
21
|
+
|
22
|
+
should "render attributes" do
|
23
|
+
@context.concat(
|
24
|
+
@view.render(:class => [:item]) do |v|
|
25
|
+
@context.concat v.content
|
26
|
+
end
|
27
|
+
)
|
28
|
+
|
29
|
+
assert(output.match(/class=\"item\"/))
|
30
|
+
end # should "render attributes"
|
31
|
+
|
32
|
+
teardown do
|
33
|
+
puts output
|
34
|
+
end # teardown
|
35
|
+
|
36
|
+
end # contect "Syaso::ListItem"
|
37
|
+
|
38
|
+
end
|
data/test/units/test_syaso.rb
CHANGED
@@ -6,9 +6,67 @@ class TestSyaso < Test::Unit::TestCase
|
|
6
6
|
context "Syaso" do
|
7
7
|
setup do
|
8
8
|
setup_view
|
9
|
+
@content = "content"
|
10
|
+
@html_class = [:test, :class]
|
11
|
+
@syaso = Syaso::Base.new(@context)
|
9
12
|
end # setup
|
10
13
|
|
11
|
-
should "
|
14
|
+
should "process content" do
|
15
|
+
@syaso.render(@content, :class => @html_class)
|
16
|
+
assert_match(/#{@content}/, output)
|
12
17
|
end
|
18
|
+
|
19
|
+
should "process no content" do
|
20
|
+
@syaso.render(:class => @html_class)
|
21
|
+
assert_not_match(/#{@content}/, output)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "process html class attributes" do
|
25
|
+
@syaso.render(:class => @html_class)
|
26
|
+
@html_class.to_s.split(" ").each do |c|
|
27
|
+
assert_match(/class="[^"]*#{@c}[^"]*"/, output)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
should "process no html class attributes" do
|
32
|
+
@syaso.render
|
33
|
+
assert_not_match(/class/, output)
|
34
|
+
end
|
35
|
+
|
36
|
+
context "data-* attribtues" do
|
37
|
+
setup do
|
38
|
+
@options = {:class => @html_class}
|
39
|
+
@data = {:view => :syaso, :role => :view}
|
40
|
+
|
41
|
+
@expect = lambda { |k,v| /data-#{k}="[^"]*#{v}[^"]*"/ }
|
42
|
+
end
|
43
|
+
|
44
|
+
should "render" do
|
45
|
+
options = @options
|
46
|
+
options[:data] = @data
|
47
|
+
@syaso.render(@content, options)
|
48
|
+
|
49
|
+
@data.each do |k, v|
|
50
|
+
assert_match(@expect.call(k, v), output)
|
51
|
+
end
|
52
|
+
end # should "render"
|
53
|
+
|
54
|
+
should "render by '_*' instead of 'data-*'" do
|
55
|
+
@data.each do |k, v|
|
56
|
+
@options["_#{k}"] = v
|
57
|
+
end
|
58
|
+
@syaso.render(@content, @options)
|
59
|
+
|
60
|
+
@data.each do |k, v|
|
61
|
+
assert_match(@expect.call(k, v), output)
|
62
|
+
end
|
63
|
+
end # should "render by '_*' instead of 'data-*'"
|
64
|
+
|
65
|
+
end # context "data-* attribtues"
|
66
|
+
|
67
|
+
|
68
|
+
teardown do
|
69
|
+
puts output
|
70
|
+
end # teardown
|
13
71
|
end # context "Syaso"
|
14
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syaso
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- syaso.gemspec
|
65
65
|
- test/test_helper.rb
|
66
66
|
- test/units/syaso/test_list.rb
|
67
|
+
- test/units/syaso/test_list_item.rb
|
67
68
|
- test/units/test_syaso.rb
|
68
69
|
homepage: ''
|
69
70
|
licenses:
|
@@ -93,5 +94,6 @@ summary: helper for ActionView
|
|
93
94
|
test_files:
|
94
95
|
- test/test_helper.rb
|
95
96
|
- test/units/syaso/test_list.rb
|
97
|
+
- test/units/syaso/test_list_item.rb
|
96
98
|
- test/units/test_syaso.rb
|
97
99
|
has_rdoc:
|