wrap_it 0.1.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05664633d5a6ca78537354a368c2240cef45996e
4
- data.tar.gz: 1872c2bcbf2038d3174ce42709ccff0040549789
3
+ metadata.gz: 3759c791d02ed6b869c3cdf07885e5d86a7874fd
4
+ data.tar.gz: f7e262fcbf4959d240e4f28e5b548d300419cf4d
5
5
  SHA512:
6
- metadata.gz: f041b82c008a526fe1aff1ce404da32c586ae4c7f2a79bdcc11cf0be1f819db9a2e08777729a9bd5412e287a719300de0fcbe7aecd89155cea91ee266b640d3f
7
- data.tar.gz: 7b46861af7625ff2e10b1f768bd07635ba25e6dac5a33ee98aadb604dba81c81656a1127bd8ba7903011dd546e0a4aa57dcd86f99911f7ff4dc4e77c7c34a84b
6
+ metadata.gz: 66c55ca42c817b786051694bfa5f0eb4b8efa8b327a318665cde8534dc4aa3d4e8ee3da8145bbec4afbeae7c392b16cd4f6e41ebe8bc342ef44b954672e59f19
7
+ data.tar.gz: b1c067ff3b920291ef2dc1bc5d5dcf95231a5dca556c59c78287d145d96935df53285ec77ef45a552745b27721094249396ab6bf5a211c55f59ef9e05b0392f7
data/README.md CHANGED
@@ -180,6 +180,14 @@ If block given, it will be called each time enum changes its value in context of
180
180
 
181
181
  ### Instance methods
182
182
 
183
+ #### wrap(*args, &block)
184
+
185
+ Wraps element with another.
186
+
187
+ You can provide wrapper directly or specify wrapper class as first argument. In this case wrapper will created with specified set of arguments and options. If wrapper class ommited, WrapIt::Base will be used.
188
+
189
+ If block present, it will be called when wrapper will rendered.
190
+
183
191
  #### html_class
184
192
 
185
193
  Returns array of html classes
@@ -236,6 +244,14 @@ element.html_class? { |x| x == 'a' } #=> true
236
244
 
237
245
  Determines whether element doesn't contains class, satisfied by conditions, specified in method arguments. See `html_class?`.
238
246
 
247
+ #### set_html_data(name, value)
248
+
249
+ Sets HTML data attribute named `name` to `value`.
250
+
251
+ #### remove_html_data(name)
252
+
253
+ Removes HTML data attribute named `name`.
254
+
239
255
  ## WrapIt::Container
240
256
 
241
257
  ### DSL methods
@@ -253,6 +269,12 @@ Creates your own DSL method to create child items. In arguments, you should spec
253
269
 
254
270
  # Changes
255
271
 
272
+ `0.1.2`
273
+ * fixed: double callbacks inclusion issue
274
+ * added: Base#wrap
275
+ * added: HTML data attribute processing
276
+ * test improvements
277
+
256
278
  `0.1.1`
257
279
  * WrapIt.helpers fails if no helper registered
258
280
 
data/lib/wrap_it/base.rb CHANGED
@@ -2,6 +2,20 @@ module WrapIt
2
2
  #
3
3
  # Base class for all HTML helper classes
4
4
  #
5
+ # @example Prevent user from changing element tag
6
+ # class Helper < WrapIt::Base
7
+ # after_initialize { @tag = 'table' }
8
+ # end
9
+ # @example Including some simple HTML into content
10
+ # class Helper < WrapIt::Base
11
+ # after_initialize do
12
+ # @icon = optioins.delete(:icon)
13
+ # end
14
+ # after_capture do
15
+ # unless @icon.nil?
16
+ # @content = html_safe("<i class=\"#{@icon}\"></i>") + @content
17
+ # end
18
+ # end
5
19
  # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
6
20
  #
7
21
  class Base
@@ -11,6 +25,7 @@ module WrapIt
11
25
  callback :initialize, :capture, :render
12
26
 
13
27
  include HTMLClass
28
+ include HTMLData
14
29
  include Switches
15
30
  include Enums
16
31
  include Renderer
@@ -38,25 +53,35 @@ module WrapIt
38
53
  self.class.get_derived(:@omit_content)
39
54
  end
40
55
 
56
+ #
57
+ # Renders element to template
58
+ #
59
+ # @override render([content, ...])
60
+ # @param content [String] additional content that will be appended
61
+ # to element content
62
+ # @yield [element] Runs block after capturing element content and before
63
+ # rendering it. Returned value appended to content.
64
+ # @yieldparam element [Base] rendering element.
65
+ # @yieldreturn [String, nil] content to append to HTML
66
+ #
67
+ # @return [String] rendered HTML for element
41
68
  def render(*args, &render_block)
42
69
  # return cached copy if it available
43
70
  return @content unless @content.nil?
44
71
  @content = empty_html
45
72
 
46
- # capture content from block
47
73
  do_capture
74
+
48
75
  # add to content string args and block result if its present
49
76
  args.flatten.each { |a| @content << a if a.is_a? String }
50
- block_given? && @content << instance_exec(self, &render_block)
51
-
52
- # cleanup options from empty values
53
- @options.select! { |k, v| !v.nil? && !v.empty? }
54
- # render element
55
- run_callbacks :render do
56
- @content = content_tag(@tag, @content, @options)
77
+ if block_given?
78
+ result = instance_exec(self, &render_block) || empty_html
79
+ result.is_a?(String) && @content << result
57
80
  end
58
81
 
59
- # @content = @wrapper.render(@content.html_safe) if @wrapper.is_a?(Base)
82
+ do_render
83
+ do_wrap
84
+
60
85
  if @template.output_buffer.nil?
61
86
  # when render called from code, just return content as a String
62
87
  @content
@@ -67,6 +92,38 @@ module WrapIt
67
92
  end
68
93
  end
69
94
 
95
+ #
96
+ # Wraps element with another.
97
+ #
98
+ # You can provide wrapper directly or specify wrapper class as first
99
+ # argument. In this case wrapper will created with specified set of
100
+ # arguments and options. If wrapper class ommited, WrapIt::Base will
101
+ # be used.
102
+ #
103
+ # If block present, it will be called when wrapper will rendered.
104
+ #
105
+ # @override wrap(wrapper)
106
+ # @param wrapper [Base] wrapper instance.
107
+ #
108
+ # @override wrap(wrapper_class, [arg, ...], options = {})
109
+ # @param wrapper_class [Class] WrapIt::Base subclass for wrapper.
110
+ # @param arg [String, Symbol] wrapper creation arguments.
111
+ # @param options [Hash] wrapper creation options.
112
+ #
113
+ # @override wrap([arg, ...], options = {})
114
+ # @param arg [String, Symbol] wrapper creation arguments.
115
+ # @param options [Hash] wrapper creation options.
116
+ #
117
+ # @return [void]
118
+ def wrap(*args, &block)
119
+ if args.first.is_a?(Base)
120
+ @wrapper = args.shift
121
+ else
122
+ wrapper_class = args.first.is_a?(Class) ? args.shift : Base
123
+ @wrapper = wrapper_class.new(@template, *args, &block)
124
+ end
125
+ end
126
+
70
127
  protected
71
128
 
72
129
  #
@@ -96,6 +153,8 @@ module WrapIt
96
153
  @options = hash
97
154
  end
98
155
 
156
+ private
157
+
99
158
  def do_capture
100
159
  run_callbacks :capture do
101
160
  @content ||= empty_html
@@ -104,5 +163,17 @@ module WrapIt
104
163
  end
105
164
  end
106
165
  end
166
+
167
+ def do_render
168
+ # cleanup options from empty values
169
+ @options.select! { |k, v| !v.nil? && !v.empty? }
170
+ run_callbacks :render do
171
+ @content = content_tag(tag, @content, options)
172
+ end
173
+ end
174
+
175
+ def do_wrap
176
+ @wrapper.is_a?(Base) && @content = @wrapper.render(html_safe(@content))
177
+ end
107
178
  end
108
179
  end
@@ -16,7 +16,7 @@ module WrapIt
16
16
  def get_derived(name)
17
17
  return instance_variable_get(name) if instance_variable_defined?(name)
18
18
  ancestors.each do |ancestor|
19
- break if ancestor == Base
19
+ break if ancestor == Object
20
20
  next unless ancestor.instance_variable_defined?(name)
21
21
  return ancestor.instance_variable_get(name)
22
22
  end
@@ -25,8 +25,11 @@ module WrapIt
25
25
 
26
26
  def collect_derived(name, initial = [], method = :concat)
27
27
  result = initial
28
+ # if instance_variable_defined?(name)
29
+ # result = result.send(method, instance_variable_get(name))
30
+ # end
28
31
  ancestors.each do |ancestor|
29
- break if ancestor == Base
32
+ break if ancestor == Object
30
33
  next unless ancestor.instance_variable_defined?(name)
31
34
  result = result.send(method, ancestor.instance_variable_get(name))
32
35
  end
data/lib/wrap_it/enums.rb CHANGED
@@ -12,7 +12,8 @@ module WrapIt
12
12
  )
13
13
  extend DerivedAttributes
14
14
  base.extend ClassMethods
15
- base.after_initialize :enums_init
15
+ # include :after_initialize callback only once
16
+ base.after_initialize :enums_init if base == Base
16
17
  end
17
18
 
18
19
  private
@@ -32,6 +32,8 @@ module WrapIt
32
32
  # Non rails render implementation
33
33
  #
34
34
  module Renderer
35
+ protected
36
+
35
37
  def empty_html
36
38
  ''
37
39
  end
data/lib/wrap_it/rails.rb CHANGED
@@ -5,6 +5,8 @@ module WrapIt
5
5
  # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
6
6
  #
7
7
  module Renderer
8
+ protected
9
+
8
10
  def empty_html
9
11
  ''.html_safe
10
12
  end
@@ -12,7 +12,8 @@ module WrapIt
12
12
  )
13
13
  extend DerivedAttributes
14
14
  base.extend ClassMethods
15
- base.after_initialize :switches_init
15
+ # include :after_initialize callback only once
16
+ base.after_initialize :switches_init if base == Base
16
17
  end
17
18
 
18
19
  private
@@ -60,7 +61,7 @@ module WrapIt
60
61
  options.symbolize_keys!
61
62
  name = name.to_sym
62
63
  options.merge!(block: block, name: name)
63
- names = [name] + [[options[:aliases]] || []].flatten
64
+ names = [name] + [[options[:aliases]] || []].flatten.compact
64
65
  var = "@#{name}".to_sym
65
66
  define_method("#{name}?") { instance_variable_get(var) == true }
66
67
  define_method("#{name}=") do |value|
@@ -1,4 +1,4 @@
1
1
  #
2
2
  module WrapIt
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
data/lib/wrap_it.rb CHANGED
@@ -64,6 +64,7 @@ require 'wrap_it/derived_attributes'
64
64
  require 'wrap_it/callbacks'
65
65
  require 'wrap_it/arguments_array'
66
66
  require 'wrap_it/html_class'
67
+ require 'wrap_it/html_data'
67
68
  require 'wrap_it/switches'
68
69
  require 'wrap_it/enums'
69
70
  if defined? Rails
@@ -10,6 +10,10 @@ describe WrapIt::Base do
10
10
  expect(successor.tag).to eq 'a'
11
11
  end
12
12
 
13
+ it 'gets tag name from options' do
14
+ expect(successor(tag: 'p').tag).to eq 'p'
15
+ end
16
+
13
17
  it 'extends @arguments with ArgumentsArray module' do
14
18
  expect(
15
19
  successor.instance_variable_get(:@arguments)
@@ -1,20 +1,67 @@
1
1
  require 'spec_helper'
2
2
 
3
- =begin
4
- class UsersHelperTest < ActionView::TestCase
3
+ describe WrapIt::Base do
5
4
  it 'includes framework-specific methods' do
6
5
  methods = successor_class.protected_instance_methods(true)
7
6
  expect(methods).to include :concat, :capture, :output_buffer
8
7
  end
9
8
 
10
9
  it 'renders base as div' do
11
- render '<%= successor %>'
10
+ render '<%= successor_helper %>'
12
11
  expect(rendered).to have_tag 'div', count: 1
13
12
  end
14
13
 
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/
14
+ describe 'self.omit_content' do
15
+ it 'captures block content' do
16
+ render '<%= successor_helper do %>Some text<% end %>'
17
+ expect(rendered).to have_tag 'div', count: 1, text: /Some text/
18
+ end
19
+
20
+ it 'omits block content with omit_content' do
21
+ successor_class.class_eval { omit_content }
22
+ render '<%= successor_helper do %>Some text<% end %>'
23
+ expect(rendered).to have_tag 'div', count: 1, text: ''
24
+ end
25
+ end
26
+
27
+ describe '#render' do
28
+ it 'adds content from arguments' do
29
+ expect(
30
+ successor.render('text', :and, ' from arguments')
31
+ ).to have_tag 'div', text: 'text from arguments'
32
+ end
33
+
34
+ it 'adds content from block' do
35
+ expect(successor.render { 'text' }).to have_tag 'div', text: 'text'
36
+ end
37
+ end
38
+
39
+ describe '#wrap' do
40
+ it 'wraps with WrapIt::Base by default' do
41
+ render '<%= successor_helper(tag: :p) { |s| s.wrap } %>'
42
+ expect(rendered).to have_tag 'div > p'
43
+ end
44
+
45
+ it 'wraps with WrapIt::Base and creating options' do
46
+ render '<%= successor_helper(tag: :p) { |s| s.wrap tag: :nav } %>'
47
+ expect(rendered).to have_tag 'nav > p'
48
+ end
49
+
50
+ it 'wraps with class and creating options' do
51
+ render <<-EOL
52
+ <% w = Class.new(WrapIt::Base) { switch :sw, html_class: 'act' } %>
53
+ <%= successor_helper(tag: :p) { |s| s.wrap w, :sw, tag: :nav } %>'
54
+ EOL
55
+ expect(rendered).to have_tag 'nav.act > p'
56
+ end
57
+
58
+ it 'wraps with block' do
59
+ render <<-EOL
60
+ <%= successor_helper(tag: :p) { |s| s.wrap do
61
+ |w| w.add_html_class 'nav'
62
+ end } %>'
63
+ EOL
64
+ expect(rendered).to have_tag 'div.nav > p'
65
+ end
18
66
  end
19
67
  end
20
- =end
data/wrap_it-0.1.1.gem ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrap_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Ovchinnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-16 00:00:00.000000000 Z
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,6 +135,7 @@ files:
135
135
  - spec/support/example_groups/rails_example_group.rb
136
136
  - spec/support/example_groups/wrap_it_example_group.rb
137
137
  - wrap_it-0.1.0.gem
138
+ - wrap_it-0.1.1.gem
138
139
  - wrap_it.gemspec
139
140
  homepage: https://github.com/cybernetlab/wrap_it
140
141
  licenses: