wrap_it 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.yardopts +3 -0
- data/README.md +67 -66
- data/lib/wrap_it.rb +16 -16
- data/lib/wrap_it/arguments.rb +368 -0
- data/lib/wrap_it/base.rb +56 -47
- data/lib/wrap_it/callbacks.rb +24 -5
- data/lib/wrap_it/capture_array.rb +140 -0
- data/lib/wrap_it/container.rb +69 -25
- data/lib/wrap_it/derived_attributes.rb +22 -6
- data/lib/wrap_it/enums.rb +44 -34
- data/lib/wrap_it/frameworks.rb +7 -3
- data/lib/wrap_it/helpers.rb +66 -1
- data/lib/wrap_it/html.rb +149 -0
- data/lib/wrap_it/html_class.rb +164 -183
- data/lib/wrap_it/html_data.rb +28 -15
- data/lib/wrap_it/link.rb +40 -17
- data/lib/wrap_it/sections.rb +90 -2
- data/lib/wrap_it/switches.rb +33 -29
- data/lib/wrap_it/text_container.rb +83 -10
- data/lib/wrap_it/version.rb +2 -1
- data/spec/frameworks/log/development.log +2108 -0
- data/spec/integration/base_spec.rb +2 -2
- data/spec/integration/container_spec.rb +3 -3
- data/spec/integration/examples_spec.rb +16 -15
- data/spec/integration/text_container_spec.rb +3 -3
- data/spec/lib/arguments_array_spec.rb +37 -27
- data/spec/lib/arguments_spec.rb +153 -0
- data/spec/lib/base_spec.rb +2 -25
- data/spec/lib/callbacks_spec.rb +1 -1
- data/spec/lib/container_spec.rb +1 -1
- data/spec/lib/derived_attributes_spec.rb +1 -1
- data/spec/lib/enums_spec.rb +2 -3
- data/spec/lib/html_class_spec.rb +269 -80
- data/spec/lib/html_data_spec.rb +18 -12
- data/spec/lib/html_spec.rb +124 -0
- data/spec/lib/link_spec.rb +2 -2
- data/spec/lib/sections_spec.rb +1 -1
- data/spec/lib/switches_spec.rb +3 -3
- data/spec/lib/text_container_spec.rb +2 -2
- data/spec/support/example_groups/{wrap_it_example_group.rb → wrapped_example_group.rb} +5 -5
- data/wrap_it.gemspec +2 -0
- metadata +15 -8
- data/lib/wrap_it/arguments_array.rb +0 -128
- data/lib/wrap_it/module_helpers.rb +0 -23
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe WrapIt::Base do
|
3
|
+
describe WrapIt::Base, type: :wrapped do
|
4
4
|
it 'includes framework-specific methods' do
|
5
5
|
methods = successor_class.protected_instance_methods(true)
|
6
6
|
expect(methods).to include :concat, :capture, :output_buffer
|
@@ -58,7 +58,7 @@ describe WrapIt::Base do
|
|
58
58
|
it 'wraps with block' do
|
59
59
|
render <<-EOL
|
60
60
|
<%= helper(tag: :p) { |s| s.wrap do
|
61
|
-
|w| w.
|
61
|
+
|w| w.html_class << 'nav'
|
62
62
|
end } %>'
|
63
63
|
EOL
|
64
64
|
expect(rendered).to have_tag 'div.nav > p'
|
@@ -85,8 +85,8 @@ describe WrapIt::Container do
|
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'extracts one child from optioins' do
|
88
|
-
successor_class.class_eval {
|
89
|
-
render '<%= helper
|
90
|
-
expect(rendered).to have_tag 'ul > li.
|
88
|
+
successor_class.class_eval { child :item1, tag: 'li', option: true }
|
89
|
+
render '<%= helper item1: {class: "listitem"} %>'
|
90
|
+
expect(rendered).to have_tag 'ul > li.listitem', count: 1
|
91
91
|
end
|
92
92
|
end
|
@@ -8,25 +8,26 @@ describe WrapIt::Base do
|
|
8
8
|
section :append, :prepend
|
9
9
|
place :prepend, before: :content
|
10
10
|
place :append, after: :content
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
|
12
|
+
option :prepend do |_, value|
|
13
|
+
self[:prepend] = content_tag('span', value,
|
14
|
+
class: 'input-group-addon')
|
15
|
+
end
|
16
|
+
|
17
|
+
option :append do |_, value|
|
18
|
+
self[:append] = content_tag('span', value,
|
19
|
+
class: 'input-group-addon')
|
14
20
|
end
|
21
|
+
|
15
22
|
after_capture do
|
16
|
-
unless @prepend.nil?
|
17
|
-
self[:prepend] = content_tag('span', @prepend,
|
18
|
-
class: 'input-group-addon')
|
19
|
-
end
|
20
|
-
unless @append.nil?
|
21
|
-
self[:append] = content_tag('span', @append,
|
22
|
-
class: 'input-group-addon')
|
23
|
-
end
|
24
23
|
if self[:content].empty?
|
25
|
-
|
26
|
-
|
24
|
+
html_attr[:type] = 'text'
|
25
|
+
html_class << 'form-control'
|
26
|
+
options = html_attr
|
27
|
+
.merge(class: html_class.to_html)
|
28
|
+
.merge(html_data)
|
27
29
|
self[:content] = content_tag('input', '', options)
|
28
|
-
|
29
|
-
add_html_class 'input-group'
|
30
|
+
self.html_class = 'input-group'
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
@@ -6,8 +6,8 @@ describe WrapIt::TextContainer do
|
|
6
6
|
expect(rendered).to have_tag 'p', text: 'text'
|
7
7
|
end
|
8
8
|
|
9
|
-
it 'places body from options
|
10
|
-
render '<%= helper "text" do %>
|
11
|
-
expect(rendered).to have_tag 'p', text: '
|
9
|
+
it 'don\'t places body from options if content present' do
|
10
|
+
render '<%= helper "text" do %>with content<% end %>'
|
11
|
+
expect(rendered).to have_tag 'p', text: 'with content'
|
12
12
|
end
|
13
13
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe WrapIt::
|
3
|
+
describe WrapIt::CaptureArray do
|
4
4
|
it 'avoid of include these module into non-array like class' do
|
5
5
|
expect do
|
6
|
-
Class.new(String) { include WrapIt::
|
6
|
+
Class.new(String) { include WrapIt::CaptureArray }
|
7
7
|
end.to raise_error TypeError
|
8
8
|
end
|
9
9
|
|
@@ -14,69 +14,79 @@ describe WrapIt::ArgumentsArray do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'extracts by class name' do
|
17
|
-
expect(array.
|
17
|
+
expect(array.capture!(Fixnum)).to eq [10]
|
18
18
|
expect(array).to_not include 10
|
19
19
|
end
|
20
20
|
|
21
|
-
it '
|
22
|
-
expect(array.
|
21
|
+
it 'captures by equality' do
|
22
|
+
expect(array.capture!(:symbol, 'string')).to eq [:symbol, 'string']
|
23
23
|
expect(array).to_not include :symbol, 'string'
|
24
24
|
end
|
25
25
|
|
26
|
-
it '
|
26
|
+
it 'captures by regexp' do
|
27
27
|
expect(
|
28
|
-
array.
|
28
|
+
array.capture!(/\Aanother/)
|
29
29
|
).to eq [:another_symbol, 'another string']
|
30
30
|
expect(array).to_not include :another_symbol, 'another string'
|
31
31
|
end
|
32
32
|
|
33
|
-
it '
|
34
|
-
expect(array.
|
33
|
+
it 'captures by array includance' do
|
34
|
+
expect(array.capture!([10, :symbol])).to eq [10, :symbol]
|
35
35
|
expect(array).to_not include 10, :symbol
|
36
36
|
end
|
37
37
|
|
38
|
-
it '
|
39
|
-
expect(array.
|
38
|
+
it 'captures by block' do
|
39
|
+
expect(array.capture! { |x| x == 10 }).to eq [10]
|
40
40
|
expect(array).to_not include 10
|
41
41
|
end
|
42
42
|
|
43
|
-
it '
|
44
|
-
expect(array.
|
43
|
+
it 'captures first by class name' do
|
44
|
+
expect(array.capture_first!(Symbol)).to eq :symbol
|
45
45
|
expect(array).to_not include :symbol
|
46
46
|
end
|
47
47
|
|
48
|
-
it '
|
49
|
-
expect(array.
|
48
|
+
it 'captures first by equality' do
|
49
|
+
expect(array.capture_first!(:symbol, 'string')).to eq :symbol
|
50
50
|
expect(array).to_not include :symbol
|
51
51
|
end
|
52
52
|
|
53
|
-
it '
|
54
|
-
expect(array.
|
53
|
+
it 'captures first by regexp' do
|
54
|
+
expect(array.capture_first!(/\Aanother/)).to eq :another_symbol
|
55
55
|
expect(array).to_not include :another_symbol
|
56
56
|
end
|
57
57
|
|
58
|
-
it '
|
59
|
-
expect(array.
|
58
|
+
it 'captures first by array includance' do
|
59
|
+
expect(array.capture_first!([10, :symbol])).to eq 10
|
60
60
|
expect(array).to_not include 10
|
61
61
|
end
|
62
62
|
|
63
|
-
it '
|
64
|
-
expect(array.
|
63
|
+
it 'captures first by block' do
|
64
|
+
expect(array.capture_first! { |x| x.is_a?(Symbol) }).to eq :symbol
|
65
65
|
expect(array).to_not include :symbol
|
66
66
|
end
|
67
67
|
|
68
|
-
it '#
|
69
|
-
expect(array.
|
68
|
+
it '#capture_first! returns nil if no matches' do
|
69
|
+
expect(array.capture_first!(20)).to be_nil
|
70
70
|
expect(array.size).to eq 5
|
71
71
|
end
|
72
72
|
|
73
|
-
it '#
|
74
|
-
expect(array.
|
73
|
+
it '#capture! returns [] if no matches' do
|
74
|
+
expect(array.capture!(20)).to eq []
|
75
75
|
expect(array.size).to eq 5
|
76
76
|
end
|
77
77
|
|
78
|
-
it 'works with and conditions' do
|
79
|
-
expect(array.
|
78
|
+
it 'works fine with met `and` conditions' do
|
79
|
+
expect(array.capture!(Symbol, and: [:symbol])).to eq [:symbol]
|
80
|
+
expect(array).to_not include :symbol
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'works fine with unmet `and` conditions ' do
|
84
|
+
expect(array.capture!(Symbol, and: [:sym])).to eq []
|
85
|
+
expect(array).to include :symbol
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'calls lambdas before comparison' do
|
89
|
+
expect(array.capture!(-> {:symbol})).to eq [:symbol]
|
80
90
|
expect(array).to_not include :symbol
|
81
91
|
end
|
82
92
|
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::Arguments, type: :wrapped do
|
4
|
+
# DSL
|
5
|
+
%i(argument option).each do |m|
|
6
|
+
describe ".#{m}" do
|
7
|
+
let(:params) { wrapper_class.instance_variable_get("@#{m}s") }
|
8
|
+
|
9
|
+
describe 'attribute parsing' do
|
10
|
+
it "rejects non-string name" do
|
11
|
+
expect { wrapper_class.send(m, 0) }.to raise_error ArgumentError
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'symbolizes name' do
|
15
|
+
wrapper_class.send(m, 'name')
|
16
|
+
expect(params).to include :name
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'makes conditions from name' do
|
20
|
+
wrapper_class.send(m, :name)
|
21
|
+
expect(params[:name][:conditions]).to eq [:name]
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'wraps string-like conditions in array' do
|
25
|
+
wrapper_class.send(m, :name, if: [:one, 'two'])
|
26
|
+
expect(params[:name][:conditions]).to eq [[:one, 'two']]
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'adds and option to conditions' do
|
30
|
+
wrapper_class.send(m, :name, if: 0, and: 1)
|
31
|
+
expect(params[:name][:conditions]).to eq [0, and: [1]]
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'wraps string-like and in array' do
|
35
|
+
wrapper_class.send(m, :name, if: 0, and: [:one, 'two'])
|
36
|
+
expect(params[:name][:conditions]).to eq [0, and: [[:one, 'two']]]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if m == :argument
|
41
|
+
it 'permits :after_options' do
|
42
|
+
wrapper_class.argument :name, after_options: true
|
43
|
+
expect(params[:name][:after_options]).to be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '.extract_for_class' do
|
50
|
+
let(:args) { wrapper_class.send :provided_arguments }
|
51
|
+
let(:opts) { wrapper_class.send :provided_options }
|
52
|
+
|
53
|
+
it 'extracts arguments' do
|
54
|
+
wrapper_class.argument :name
|
55
|
+
a = [:name, :name, :test]
|
56
|
+
wrapper_class.send(:extract_for_class, a, {})
|
57
|
+
expect(args[:name]).to match_array [:name, :name]
|
58
|
+
expect(a).to match_array [:test]
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'extracts options' do
|
62
|
+
wrapper_class.option :name
|
63
|
+
o = {name: :opt}
|
64
|
+
wrapper_class.send(:extract_for_class, [], o)
|
65
|
+
expect(opts[:name]).to eq(name: :opt)
|
66
|
+
expect(o).to be_empty
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
before do
|
71
|
+
wrapper_class.class_eval { attr_accessor :test }
|
72
|
+
end
|
73
|
+
=begin
|
74
|
+
it 'extracts arguments by conditions' do
|
75
|
+
wrapper_class.class_eval do
|
76
|
+
attr_accessor :i
|
77
|
+
argument(:arg, if: Symbol) { |_| self.i = (i || 0) + 1 }
|
78
|
+
end
|
79
|
+
expect_any_instance_of(WrapIt::CaptureArray)
|
80
|
+
.to receive(:capture!).with(Symbol).and_call_original
|
81
|
+
expect(wrapper(:test, :multiple, :args).i).to eq 3
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'extracts arguments to setters' do
|
85
|
+
wrapper_class.class_eval { argument :test }
|
86
|
+
expect(wrapper(:test).test).to be_true
|
87
|
+
end
|
88
|
+
=end
|
89
|
+
|
90
|
+
# it 'extracts only first arguments with first_only: true' do
|
91
|
+
# wrapper_class.class_eval do
|
92
|
+
# attr_accessor :i
|
93
|
+
# argument(:arg, if: Symbol, first_only: true) do |_|
|
94
|
+
# self.i = (i || 0) + 1
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
# expect_any_instance_of(WrapIt::CaptureArray)
|
98
|
+
# .to receive(:capture_first!).and_call_original
|
99
|
+
# expect(wrapper(:test, :multiple, :args).i).to eq 1
|
100
|
+
# end
|
101
|
+
|
102
|
+
=begin
|
103
|
+
# class methods
|
104
|
+
describe '.capture_arguments!' do
|
105
|
+
it 'extracts argument' do
|
106
|
+
wrapper_class.class_eval { argument :test }
|
107
|
+
args = %i(test args)
|
108
|
+
extracted = wrapper_class.capture_arguments!(args)
|
109
|
+
expect(extracted).to match_array %i(test)
|
110
|
+
expect(args).to match_array %i(args)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'extracts option' do
|
114
|
+
wrapper_class.class_eval { option :test_option }
|
115
|
+
args = [:test, :arg, test_option: 1]
|
116
|
+
extracted = wrapper_class.capture_arguments!(args)
|
117
|
+
expect(extracted).to match_array [test_option: 1]
|
118
|
+
expect(args).to match_array [:test, :arg]
|
119
|
+
args = [:test, :arg, test_option: 1, another: 0]
|
120
|
+
extracted = wrapper_class.capture_arguments!(args)
|
121
|
+
expect(extracted).to match_array [test_option: 1]
|
122
|
+
expect(args).to match_array [:test, :arg, another: 0]
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'calls arguments extarct! method' do
|
126
|
+
wrapper_class.class_eval { argument :test }
|
127
|
+
args = double(%i(test args))
|
128
|
+
expect(args).to receive(:is_a?).with(Array).and_return(true)
|
129
|
+
expect(args).to receive(:extract_options!).and_return({})
|
130
|
+
expect(args).to receive(:extract!).with(:test).and_return([])
|
131
|
+
wrapper_class.extract_arguments!(args)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe '#capture_arguments!' do
|
136
|
+
it 'calls block for finded arguments in instance context' do
|
137
|
+
wrapper_class.class_eval do
|
138
|
+
argument(:test) { html_class << 'test' }
|
139
|
+
end
|
140
|
+
wrapper.send(:capture_arguments!, %i(test args))
|
141
|
+
expect(wrapper.html_class).to include 'test'
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'calls block for finded options in instance context' do
|
145
|
+
wrapper_class.class_eval do
|
146
|
+
option(:test_me) { html_class.push 'test' }
|
147
|
+
end
|
148
|
+
wrapper.send(:capture_arguments!, [test_me: :args])
|
149
|
+
expect(wrapper.html_class).to include 'test'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
=end
|
153
|
+
end
|
data/spec/lib/base_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe WrapIt::Base do
|
3
|
+
describe WrapIt::Base, type: :wrapped do
|
4
4
|
it 'have #tag getter' do
|
5
5
|
expect(successor.tag).to eq 'div'
|
6
6
|
end
|
@@ -14,25 +14,8 @@ describe WrapIt::Base do
|
|
14
14
|
expect(successor(tag: 'p').tag).to eq 'p'
|
15
15
|
end
|
16
16
|
|
17
|
-
it 'extends @arguments with ArgumentsArray module' do
|
18
|
-
args = nil
|
19
|
-
successor_class.class_eval { after_initialize { args = @arguments } }
|
20
|
-
successor
|
21
|
-
expect(args).to be_kind_of WrapIt::ArgumentsArray
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'symbolizes options hash' do
|
25
|
-
successor.send :options=, 'my' => 'value'
|
26
|
-
expect(successor.options).to eq(my: 'value', class: [])
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'sanitizes options class' do
|
30
|
-
successor.send :options=, class: [:one, :two, :two]
|
31
|
-
expect(successor.options[:class]).to eq %w(one two)
|
32
|
-
end
|
33
|
-
|
34
17
|
it 'calls after_initialize' do
|
35
|
-
successor_class.class_eval { after_initialize {
|
18
|
+
successor_class.class_eval { after_initialize { html_class << :a } }
|
36
19
|
expect(successor.html_class).to eq %w(a)
|
37
20
|
end
|
38
21
|
|
@@ -44,10 +27,4 @@ describe WrapIt::Base do
|
|
44
27
|
successor_class.class_eval { omit_content }
|
45
28
|
expect(successor.omit_content?).to be_true
|
46
29
|
end
|
47
|
-
|
48
|
-
it 'removes `helper_name` from options' do
|
49
|
-
successor(helper_name: 'test')
|
50
|
-
expect(successor.options).to_not include :helper_name
|
51
|
-
expect(successor.instance_variable_get(:@helper_name)).to eq :test
|
52
|
-
end
|
53
30
|
end
|
data/spec/lib/callbacks_spec.rb
CHANGED
data/spec/lib/container_spec.rb
CHANGED
data/spec/lib/enums_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe WrapIt::Enums do
|
3
|
+
describe WrapIt::Enums, type: :wrapped do
|
4
4
|
it_behaves_like 'Base module'
|
5
5
|
|
6
6
|
context 'wrapper have `kind` enum' do
|
@@ -26,7 +26,6 @@ describe WrapIt::Enums do
|
|
26
26
|
expect(wrapper(kind: :black).kind).to eq :black
|
27
27
|
@wrapper = nil
|
28
28
|
expect(wrapper(kind: false).kind).to be_nil
|
29
|
-
expect(wrapper.options).to_not include :kind
|
30
29
|
end
|
31
30
|
|
32
31
|
it 'runs block' do
|
@@ -83,7 +82,7 @@ describe WrapIt::Enums do
|
|
83
82
|
@wrapper = nil
|
84
83
|
expect(wrapper(kind: :no).kind).to eq :white
|
85
84
|
@wrapper = nil
|
86
|
-
expect(wrapper(:black, kind: :no).kind).to eq :
|
85
|
+
expect(wrapper(:black, kind: :no).kind).to eq :white
|
87
86
|
end
|
88
87
|
end
|
89
88
|
end
|