wrap_it 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rubocop.yml +5 -0
- data/Gemfile +7 -0
- data/README.md +277 -0
- data/Rakefile +14 -0
- data/config.ru +4 -0
- data/lib/wrap_it/arguments_array.rb +128 -0
- data/lib/wrap_it/base.rb +108 -0
- data/lib/wrap_it/callbacks.rb +63 -0
- data/lib/wrap_it/container.rb +84 -0
- data/lib/wrap_it/derived_attributes.rb +37 -0
- data/lib/wrap_it/enums.rb +89 -0
- data/lib/wrap_it/html_class.rb +193 -0
- data/lib/wrap_it/no_rails.rb +66 -0
- data/lib/wrap_it/rails.rb +51 -0
- data/lib/wrap_it/switches.rb +82 -0
- data/lib/wrap_it/text_container.rb +25 -0
- data/lib/wrap_it/version.rb +3 -0
- data/lib/wrap_it.rb +74 -0
- data/spec/internal/log/test.log +619 -0
- data/spec/lib/arguments_array_spec.rb +83 -0
- data/spec/lib/base_spec.rb +48 -0
- data/spec/lib/callbacks_spec.rb +5 -0
- data/spec/lib/container_spec.rb +9 -0
- data/spec/lib/derived_attributes_spec.rb +51 -0
- data/spec/lib/enums_spec.rb +79 -0
- data/spec/lib/html_class_spec.rb +98 -0
- data/spec/lib/switches_spec.rb +66 -0
- data/spec/rails/base_spec.rb +20 -0
- data/spec/rails/container_spec.rb +30 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/example_groups/rails_example_group.rb +79 -0
- data/spec/support/example_groups/wrap_it_example_group.rb +39 -0
- data/wrap_it.gemspec +30 -0
- metadata +179 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::ArgumentsArray do
|
4
|
+
it 'avoid of include these module into non-array like class' do
|
5
|
+
expect do
|
6
|
+
Class.new(String) { include WrapIt::ArgumentsArray }
|
7
|
+
end.to raise_error TypeError
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'having test array' do
|
11
|
+
let :array do
|
12
|
+
arr = [10, :symbol, :another_symbol, 'string', 'another string']
|
13
|
+
arr.extend described_class
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'extracts by class name' do
|
17
|
+
expect(array.extract!(Fixnum)).to eq [10]
|
18
|
+
expect(array).to_not include 10
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'extracts by equality' do
|
22
|
+
expect(array.extract!(:symbol, 'string')).to eq [:symbol, 'string']
|
23
|
+
expect(array).to_not include :symbol, 'string'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'extracts by regexp' do
|
27
|
+
expect(
|
28
|
+
array.extract!(/\Aanother/)
|
29
|
+
).to eq [:another_symbol, 'another string']
|
30
|
+
expect(array).to_not include :another_symbol, 'another string'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'extracts by array includance' do
|
34
|
+
expect(array.extract!([10, :symbol])).to eq [10, :symbol]
|
35
|
+
expect(array).to_not include 10, :symbol
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'extracts by block' do
|
39
|
+
expect(array.extract! { |x| x == 10 }).to eq [10]
|
40
|
+
expect(array).to_not include 10
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'extracts first by class name' do
|
44
|
+
expect(array.extract_first!(Symbol)).to eq :symbol
|
45
|
+
expect(array).to_not include :symbol
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'extracts first by equality' do
|
49
|
+
expect(array.extract_first!(:symbol, 'string')).to eq :symbol
|
50
|
+
expect(array).to_not include :symbol
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'extracts first by regexp' do
|
54
|
+
expect(array.extract_first!(/\Aanother/)).to eq :another_symbol
|
55
|
+
expect(array).to_not include :another_symbol
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'extracts first by array includance' do
|
59
|
+
expect(array.extract_first!([10, :symbol])).to eq 10
|
60
|
+
expect(array).to_not include 10
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'extracts first by block' do
|
64
|
+
expect(array.extract_first! { |x| x.is_a?(Symbol) }).to eq :symbol
|
65
|
+
expect(array).to_not include :symbol
|
66
|
+
end
|
67
|
+
|
68
|
+
it '#extract_first! returns nil if no matches' do
|
69
|
+
expect(array.extract_first!(20)).to be_nil
|
70
|
+
expect(array.size).to eq 5
|
71
|
+
end
|
72
|
+
|
73
|
+
it '#extract! returns [] if no matches' do
|
74
|
+
expect(array.extract!(20)).to eq []
|
75
|
+
expect(array.size).to eq 5
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'works with and conditions' do
|
79
|
+
expect(array.extract!(Symbol, and: [:symbol])).to eq [:symbol]
|
80
|
+
expect(array).to_not include :symbol
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::Base do
|
4
|
+
it 'have #tag getter' do
|
5
|
+
expect(successor.tag).to eq 'div'
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'have default_tag class method' do
|
9
|
+
successor_class.class_eval { default_tag 'a' }
|
10
|
+
expect(successor.tag).to eq 'a'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'extends @arguments with ArgumentsArray module' do
|
14
|
+
expect(
|
15
|
+
successor.instance_variable_get(:@arguments)
|
16
|
+
).to be_kind_of WrapIt::ArgumentsArray
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'symbolizes options hash' do
|
20
|
+
successor.send :options=, 'my' => 'value'
|
21
|
+
expect(successor.options).to eq(my: 'value', class: [])
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'sanitizes options class' do
|
25
|
+
successor.send :options=, class: [:one, :two, :two]
|
26
|
+
expect(successor.options[:class]).to eq %w(one two)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'calls after_initialize' do
|
30
|
+
successor_class.class_eval { after_initialize { add_html_class :a } }
|
31
|
+
expect(successor.html_class).to eq %w(a)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'not omits content by default' do
|
35
|
+
expect(successor.omit_content?).to be_false
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'provides way to omit content in subclasses' do
|
39
|
+
successor_class.class_eval { omit_content }
|
40
|
+
expect(successor.omit_content?).to be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'removes `helper_name` from options' do
|
44
|
+
successor(helper_name: 'test')
|
45
|
+
expect(successor.options).to_not include :helper_name
|
46
|
+
expect(successor.instance_variable_get(:@helper_name)).to eq :test
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::DerivedAttributes do
|
4
|
+
describe '::get_derived' do
|
5
|
+
it 'retrieves nil by default' do
|
6
|
+
expect(wrapper_class.get_derived(:@var)).to be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'retrieves var, defined in class' do
|
10
|
+
wrapper_class.class_eval { @var = 1 }
|
11
|
+
sub1 = Class.new(wrapper_class) { @var = 2 }
|
12
|
+
sub2 = Class.new(sub1)
|
13
|
+
expect(wrapper_class.get_derived(:@var)).to eq 1
|
14
|
+
expect(sub1.get_derived(:@var)).to eq 2
|
15
|
+
expect(sub2.get_derived(:@var)).to eq 2
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '::collect_derived' do
|
20
|
+
it 'collects in array by default' do
|
21
|
+
expect(wrapper_class.collect_derived(:@var)).to eq []
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'collects vars in correct order' do
|
25
|
+
wrapper_class.class_eval { @var = '1' }
|
26
|
+
sub1 = Class.new(wrapper_class) { @var = '2' }
|
27
|
+
sub2 = Class.new(sub1)
|
28
|
+
expect(wrapper_class.collect_derived(:@var, [], :push)).to eq %w(1)
|
29
|
+
expect(sub1.collect_derived(:@var, [], :push)).to eq %w(2 1)
|
30
|
+
expect(sub2.collect_derived(:@var, [], :push)).to eq %w(2 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'collects arrays by default' do
|
34
|
+
wrapper_class.class_eval { @var = %w(1 2) }
|
35
|
+
sub1 = Class.new(wrapper_class) { @var = %w(2 3) }
|
36
|
+
expect(wrapper_class.collect_derived(:@var)).to eq %w(1 2)
|
37
|
+
expect(sub1.collect_derived(:@var)).to eq %w(2 3 1 2)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'collects hashes' do
|
41
|
+
wrapper_class.class_eval { @var = { one: 1, two: 2 } }
|
42
|
+
sub1 = Class.new(wrapper_class) { @var = { three: 3 } }
|
43
|
+
expect(
|
44
|
+
wrapper_class.collect_derived(:@var, {}, :merge)
|
45
|
+
).to eq(one: 1, two: 2)
|
46
|
+
expect(
|
47
|
+
sub1.collect_derived(:@var, {}, :merge)
|
48
|
+
).to eq(one: 1, two: 2, three: 3)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::Enums do
|
4
|
+
context 'wrapper have `kind` enum' do
|
5
|
+
before { wrapper_class.class_eval { enum :kind, [:white, :black] } }
|
6
|
+
|
7
|
+
it 'adds getters' do
|
8
|
+
expect(wrapper.kind).to be_nil
|
9
|
+
wrapper.kind = true
|
10
|
+
expect(wrapper.kind).to be_nil
|
11
|
+
wrapper.kind = :white
|
12
|
+
expect(wrapper.kind).to eq :white
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'gets enum value from arguments' do
|
16
|
+
expect(wrapper(:white).kind).to eq :white
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'string arguments are ignored' do
|
20
|
+
expect(wrapper('white').kind).to be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'gets enum from options' do
|
24
|
+
expect(wrapper(kind: :black).kind).to eq :black
|
25
|
+
@wrapper = nil
|
26
|
+
expect(wrapper(kind: false).kind).to be_nil
|
27
|
+
expect(wrapper.options).to_not include :kind
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'runs block' do
|
31
|
+
wrapper_class.class_eval do
|
32
|
+
enum(:kind, [:white, :black]) { |x| self.html_class = x.to_s }
|
33
|
+
end
|
34
|
+
expect(wrapper(:white).html_class).to include 'white'
|
35
|
+
@wrapper = nil
|
36
|
+
expect(wrapper(kind: :black).html_class).to include 'black'
|
37
|
+
wrapper.kind = :white
|
38
|
+
expect(wrapper.html_class).to include 'white'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'adds and removes html class' do
|
42
|
+
wrapper_class.class_eval do
|
43
|
+
enum :kind, [:white, :black], html_class_prefix: 'test-'
|
44
|
+
end
|
45
|
+
expect(wrapper(:white).html_class).to include 'test-white'
|
46
|
+
@wrapper = nil
|
47
|
+
expect(wrapper(kind: :black).html_class).to include 'test-black'
|
48
|
+
@wrapper = nil
|
49
|
+
expect(wrapper(kind: :no).html_class).to be_empty
|
50
|
+
wrapper.kind = :white
|
51
|
+
expect(wrapper.html_class).to include 'test-white'
|
52
|
+
wrapper.kind = :black
|
53
|
+
expect(wrapper.html_class).to include 'test-black'
|
54
|
+
expect(wrapper.html_class).to_not include 'test-white'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'detects aliases' do
|
58
|
+
wrapper_class.class_eval do
|
59
|
+
enum :kind, [:white, :black], aliases: [:appearence]
|
60
|
+
end
|
61
|
+
expect(wrapper(appearence: :white).kind).to eq :white
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'supports default values' do
|
65
|
+
wrapper_class.class_eval do
|
66
|
+
enum :kind, [:white, :black], default: :white
|
67
|
+
end
|
68
|
+
expect(wrapper.kind).to eq :white
|
69
|
+
wrapper.kind = :black
|
70
|
+
expect(wrapper.kind).to eq :black
|
71
|
+
wrapper.kind = nil
|
72
|
+
expect(wrapper.kind).to eq :white
|
73
|
+
@wrapper = nil
|
74
|
+
expect(wrapper(kind: :no).kind).to eq :white
|
75
|
+
@wrapper = nil
|
76
|
+
expect(wrapper(:black, kind: :no).kind).to eq :black
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::HTMLClass do
|
4
|
+
it 'have html_class class method' do
|
5
|
+
wrapper_class.class_eval { html_class :a, [:b, 'c'] }
|
6
|
+
expect(wrapper.html_class).to eq %w(a b c)
|
7
|
+
sub_class = Class.new(wrapper_class) { html_class :a, [:d, 'e'] }
|
8
|
+
expect(sub_class.new(template).html_class).to eq %w(a d e b c)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'has #add_html_class with chaining' do
|
12
|
+
expect(wrapper.add_html_class(:test).options[:class]).to eq %w(test)
|
13
|
+
@wrapper = nil
|
14
|
+
expect(wrapper.add_html_class(:a, 'b').options[:class]).to eq %w(a b)
|
15
|
+
@wrapper = nil
|
16
|
+
expect(
|
17
|
+
wrapper.add_html_class(:a, [:b, :c]).options[:class]
|
18
|
+
).to eq %w(a b c)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has #remove_html_class with chaining' do
|
22
|
+
expect(
|
23
|
+
wrapper.add_html_class(:a, :b).remove_html_class('a').options[:class]
|
24
|
+
).to eq %w(b)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has #html_class? method' do
|
28
|
+
expect(wrapper.add_html_class(:a1, :b1).html_class?('a1')).to be_true
|
29
|
+
expect(wrapper.html_class?(:a1, :b1)).to be_true
|
30
|
+
expect(wrapper.html_class?(:a1, :b2)).to be_false
|
31
|
+
expect(wrapper.html_class?(:a2)).to be_false
|
32
|
+
expect(wrapper.html_class?(/\d+/)).to be_true
|
33
|
+
expect(wrapper.html_class?(%w(a1 c1))).to be_true
|
34
|
+
expect(wrapper.html_class? { |x| x[0] == 'a' }).to be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'has #html_class? method' do
|
38
|
+
expect(wrapper.add_html_class(:a1, :b1).no_html_class?('a2')).to be_true
|
39
|
+
expect(wrapper.no_html_class?(:a2, :b2)).to be_true
|
40
|
+
expect(wrapper.no_html_class?(:a1, :b2)).to be_false
|
41
|
+
expect(wrapper.no_html_class?(:a1)).to be_false
|
42
|
+
expect(wrapper.no_html_class?(/\d+./)).to be_true
|
43
|
+
expect(wrapper.no_html_class?(%w(c1 d1))).to be_true
|
44
|
+
expect(wrapper.no_html_class? { |x| x[0] == 'c' }).to be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'has html_class setter' do
|
48
|
+
wrapper.html_class = [:a, :b]
|
49
|
+
expect(wrapper.options[:class]).to eq %w(a b)
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'documentation examples' do
|
53
|
+
let(:element) { wrapper }
|
54
|
+
|
55
|
+
it 'html_class=' do
|
56
|
+
element.html_class = [:a, 'b', ['c', :d, 'a']]
|
57
|
+
expect(element.html_class).to eq %w(a b c d)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'add_html_class' do
|
61
|
+
element.html_class = 'a'
|
62
|
+
element.add_html_class :b, :c, ['d', :c, :e, 'a']
|
63
|
+
expect(element.html_class).to eq %w(a b c d e)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'remove_html_class' do
|
67
|
+
element.add_html_class %w(a b c d e)
|
68
|
+
element.remove_html_class :b, ['c', :e]
|
69
|
+
expect(element.html_class).to eq %w(a d)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'html_class? with Symbol or String' do
|
73
|
+
element.html_class = [:a, :b, :c]
|
74
|
+
expect(element.html_class?(:a)).to be_true
|
75
|
+
expect(element.html_class?(:d)).to be_false
|
76
|
+
expect(element.html_class?(:a, 'b')).to be_true
|
77
|
+
expect(element.html_class?(:a, :d)).to be_false
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'html_class? with Regexp' do
|
81
|
+
element.html_class = [:some, :test]
|
82
|
+
expect(element.html_class?(/some/)).to be_true
|
83
|
+
expect(element.html_class?(/some/, /bad/)).to be_false
|
84
|
+
expect(element.html_class?(/some/, :test)).to be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'html_class? with Array' do
|
88
|
+
element.html_class = [:a, :b, :c]
|
89
|
+
expect(element.html_class?(%w(a d))).to be_true
|
90
|
+
expect(element.html_class?(%w(e d))).to be_false
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'html_class? with block' do
|
94
|
+
element.html_class = [:a, :b, :c]
|
95
|
+
expect(element.html_class? { |x| x == 'a' }).to be_true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::Switches do
|
4
|
+
context 'wrapper have `active` switch' do
|
5
|
+
before { wrapper_class.class_eval { switch :active } }
|
6
|
+
|
7
|
+
it 'adds getters' do
|
8
|
+
expect(wrapper.active?).to be_false
|
9
|
+
wrapper.active = true
|
10
|
+
expect(wrapper.active?).to be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'gets switch from arguments' do
|
14
|
+
expect(wrapper(:active).active?).to be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'string arguments are ignored' do
|
18
|
+
expect(wrapper('active').active?).to be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'gets switch from options' do
|
22
|
+
expect(wrapper(active: true).active?).to be_true
|
23
|
+
@wrapper = nil
|
24
|
+
expect(wrapper(active: false).active?).to be_false
|
25
|
+
expect(wrapper.options).to_not include :active
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'runs block' do
|
29
|
+
wrapper_class.class_eval do
|
30
|
+
switch(:active) { |x| add_html_class(x.to_s) }
|
31
|
+
end
|
32
|
+
expect(wrapper(:active).html_class).to include 'true'
|
33
|
+
@wrapper = nil
|
34
|
+
expect(wrapper(active: true).html_class).to include 'true'
|
35
|
+
@wrapper = nil
|
36
|
+
expect(wrapper(active: false).html_class).to include 'false'
|
37
|
+
wrapper.active = true
|
38
|
+
expect(wrapper.html_class).to include 'true'
|
39
|
+
wrapper.active = false
|
40
|
+
expect(wrapper.html_class).to include 'false'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'adds and removes html class' do
|
44
|
+
wrapper_class.class_eval { switch :active, html_class: 'active' }
|
45
|
+
expect(wrapper(:active).html_class).to include 'active'
|
46
|
+
@wrapper = nil
|
47
|
+
expect(wrapper(active: true).html_class).to include 'active'
|
48
|
+
@wrapper = nil
|
49
|
+
expect(wrapper(active: false).html_class).to_not include 'active'
|
50
|
+
wrapper.active = true
|
51
|
+
expect(wrapper.html_class).to include 'active'
|
52
|
+
wrapper.active = false
|
53
|
+
expect(wrapper.html_class).to_not include 'active'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'detects aliases' do
|
57
|
+
wrapper_class.class_eval { switch :active, aliases: :act }
|
58
|
+
expect(wrapper(:act).active?).to be_true
|
59
|
+
@wrapper = nil
|
60
|
+
expect(wrapper(act: true).active?).to be_true
|
61
|
+
@wrapper = nil
|
62
|
+
expect(wrapper(act: false).active?).to be_false
|
63
|
+
expect(wrapper).to_not respond_to :act?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
=begin
|
4
|
+
class UsersHelperTest < ActionView::TestCase
|
5
|
+
it 'includes framework-specific methods' do
|
6
|
+
methods = successor_class.protected_instance_methods(true)
|
7
|
+
expect(methods).to include :concat, :capture, :output_buffer
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'renders base as div' do
|
11
|
+
render '<%= successor %>'
|
12
|
+
expect(rendered).to have_tag 'div', count: 1
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'captures block content' do
|
16
|
+
render '<%= successor do %>Some text<% end %>'
|
17
|
+
expect(rendered).to have_tag 'div', count: 1, text: /Some text/
|
18
|
+
end
|
19
|
+
end
|
20
|
+
=end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WrapIt::Container do
|
4
|
+
before do
|
5
|
+
successor_class.class_eval do
|
6
|
+
default_tag 'ul'
|
7
|
+
child :item, [tag: 'li']
|
8
|
+
end
|
9
|
+
Object.send(:const_set, :Successor, successor_class)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'renders as ul' do
|
13
|
+
render '<%= successor_helper %>'
|
14
|
+
expect(rendered).to have_tag 'ul', count: 1
|
15
|
+
expect(rendered).to_not have_tag 'ul > *'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'renders child items' do
|
19
|
+
render <<-EOL
|
20
|
+
<%= successor_helper do |s| %>
|
21
|
+
<li class="c1">item 1</li>
|
22
|
+
<% 2.upto 5 do |i| %>
|
23
|
+
<%= s.item class: "c\#{i}" do |_| %>item <%= i %><% end %>
|
24
|
+
<% end %>
|
25
|
+
<li class="c6">item 6</li>
|
26
|
+
<% end %>
|
27
|
+
EOL
|
28
|
+
expect(rendered).to have_tag 'ul > li', count: 6
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler.require :default, :development
|
5
|
+
|
6
|
+
Dir[File.join(File.dirname(__FILE__), 'support', '**', '*.rb')].each do |file|
|
7
|
+
require file
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
config.order = 'random'
|
15
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#
|
2
|
+
# Helpers for Rails-specific testing
|
3
|
+
#
|
4
|
+
# @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
|
5
|
+
#
|
6
|
+
module RailsExampleGroup
|
7
|
+
def self.included(base)
|
8
|
+
base.instance_eval do
|
9
|
+
metadata[:type] = :rails
|
10
|
+
|
11
|
+
before(:all) { RailsExampleGroup.init_rails }
|
12
|
+
after(:all) do
|
13
|
+
Object.send(:remove_const, :Rails) if Object.const_defined?(:Rails)
|
14
|
+
end
|
15
|
+
|
16
|
+
after :each do
|
17
|
+
WrapIt.unregister :wrapper, :successor
|
18
|
+
Object.send(:remove_const, :Wrapper) if Object.const_defined?(:Wrapper)
|
19
|
+
if Object.const_defined?(:Successor)
|
20
|
+
Object.send(:remove_const, :Successor)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:template) do
|
25
|
+
instance_eval(&RailsExampleGroup.register_helper)
|
26
|
+
template_class.new.extend WrapIt.helpers
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:template_class) { Class.new(ActionView::Base) }
|
30
|
+
|
31
|
+
let(:rendered) { @rendered }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.init_rails
|
36
|
+
require 'rails'
|
37
|
+
require 'active_support/dependencies'
|
38
|
+
require 'action_controller/railtie'
|
39
|
+
require 'action_view/railtie'
|
40
|
+
I18n.enforce_available_locales = true
|
41
|
+
path = File.expand_path(
|
42
|
+
File.join('..', '..', '..', '..', 'lib', 'wrap_it'),
|
43
|
+
__FILE__
|
44
|
+
)
|
45
|
+
WrapIt.send(:remove_const, :Renderer)
|
46
|
+
load File.join(path, 'rails.rb')
|
47
|
+
WrapIt::Base.send(:include, WrapIt::Renderer)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.register_helper
|
51
|
+
proc do
|
52
|
+
if described_class.is_a?(Class)
|
53
|
+
if Object.const_defined?(:Successor)
|
54
|
+
Object.send(:remove_const, :Successor)
|
55
|
+
end
|
56
|
+
Object.const_set(:Successor, successor_class)
|
57
|
+
WrapIt.unregister :successor_helper
|
58
|
+
WrapIt.register :successor_helper, 'Successor'
|
59
|
+
else
|
60
|
+
Object.sned(:remove_const, :Wrapper) if Object.const_defined?(:Wrapper)
|
61
|
+
Object.const_set(:Wrapper, wrapper_class)
|
62
|
+
WrapIt.unregister :wrapper_helper
|
63
|
+
WrapIt.register :wrapper_helper, 'Wrapper'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def render(code)
|
69
|
+
@rendered = template.render(inline: code)
|
70
|
+
end
|
71
|
+
|
72
|
+
RSpec.configure do |config|
|
73
|
+
config.include(
|
74
|
+
self,
|
75
|
+
type: :rails,
|
76
|
+
example_group: { file_path: /spec\/rails/ }
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#
|
2
|
+
# Helpers for WrapIt testing
|
3
|
+
#
|
4
|
+
# @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
|
5
|
+
#
|
6
|
+
module WrapItExampleGroup
|
7
|
+
def self.included(base)
|
8
|
+
base.instance_eval do
|
9
|
+
metadata[:type] = :wrap_it
|
10
|
+
|
11
|
+
after { @successor = nil }
|
12
|
+
|
13
|
+
let(:template) { Object.new }
|
14
|
+
|
15
|
+
let(:successor_class) { Class.new described_class }
|
16
|
+
|
17
|
+
let(:wrapper_class) do
|
18
|
+
mod = described_class
|
19
|
+
Class.new(WrapIt::Base) { include mod }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def successor(*args, &block)
|
25
|
+
@successor ||= successor_class.new(template, *args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def wrapper(*args, &block)
|
29
|
+
@wrapper ||= wrapper_class.new(template, *args, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
RSpec.configure do |config|
|
33
|
+
config.include(
|
34
|
+
self,
|
35
|
+
type: :wrap_it,
|
36
|
+
example_group: { file_path: /spec/ }
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
data/wrap_it.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'wrap_it/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'wrap_it'
|
7
|
+
spec.version = WrapIt::VERSION
|
8
|
+
spec.authors = ['Alexey Ovchinnikov']
|
9
|
+
spec.email = ['alexiss@cybernetlab.ru']
|
10
|
+
spec.description = %q{Set of classes and modules for creating HTML helpers}
|
11
|
+
spec.summary = <<-EOL.gsub(/^\s+\|/, '')
|
12
|
+
|This library provides set of classes and modules with simple DSL for quick
|
13
|
+
|and easy creating html helpers with your own DSL. It's usefull for
|
14
|
+
|implementing CSS frameworks, or making your own.
|
15
|
+
EOL
|
16
|
+
spec.homepage = 'https://github.com/cybernetlab/wrap_it'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'redcarpet', '~> 1.17'
|
27
|
+
spec.add_development_dependency 'yard', '~> 0.7.5'
|
28
|
+
spec.add_development_dependency 'rspec'
|
29
|
+
spec.add_development_dependency 'rspec-html-matchers'
|
30
|
+
end
|