wrap_it 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0bff3652d6d84abb2e4b5fd7e0fcfd1b3077c97
4
- data.tar.gz: d7d2c2031a7f3329f7f22e23378e217c9a1248a9
3
+ metadata.gz: 452ea9b6986b74655e20c2b05043adfee92a6f3b
4
+ data.tar.gz: 5f9afcec73b2e3fcd904f918df595a3c884cce54
5
5
  SHA512:
6
- metadata.gz: 96022ed7bb7fff92f6c0ee5cea9dd0be8c0e53d670a66014a7685edaeb51910a72692e23d308ef236eb8898acffef37bbd5b97ee406695078acfe53c209d2db1
7
- data.tar.gz: d3b6e268405b869b07ac024a824f13589647e2e8d9ebc94edf295791ea4a0e2aea1bd04ad51ae538fbf7eb140d64cd0a59f3275fb2b0b409089c2dfcc2407803
6
+ metadata.gz: 4ced26c53ee1c80985c120ce7edde26b5a5a9306c8c37e25aee711bac374215f17a1b1dc9541bc279705cbd73664a6183e759c6c488b33ead6750a268c4b9c8a
7
+ data.tar.gz: d0565411c8abde8db3f6225adf5fe814a58ba100609573c3954887a3cab6e1b971c55e698a4b27cb2ea0a6a3ca10e0919aca8f422587cace395ec6171c423703
data/README.md CHANGED
@@ -273,6 +273,12 @@ Creates your own DSL method to create child items. In arguments, you should spec
273
273
 
274
274
  # Changes
275
275
 
276
+ `0.1.5`
277
+ * fixed: switches and enums can damage instance variables
278
+ * fixed: process helper_name option before initialize callbacks
279
+ * fixed: convert user defined tag to string
280
+ * added: Link class
281
+
276
282
  `0.1.4`
277
283
  * added: html_class_prefix
278
284
 
data/lib/wrap_it/base.rb CHANGED
@@ -38,15 +38,20 @@ module WrapIt
38
38
  def initialize(template, *args, &block)
39
39
  @template, @arguments, @block = template, args, block
40
40
  self.options = @arguments.extract_options!
41
+
42
+ @helper_name = @options.delete(:helper_name)
43
+ @helper_name.is_a?(String) && @helper_name = @helper_name.to_sym
44
+
41
45
  @arguments.extend ArgumentsArray
42
46
  add_default_classes
47
+
43
48
  run_callbacks :initialize do
44
49
  @tag = @options.delete(:tag) ||
45
50
  self.class.get_derived(:@default_tag) || 'div'
46
- @helper_name = @options.delete(:helper_name)
47
- @helper_name.is_a?(String) && @helper_name = @helper_name.to_sym
51
+ @tag = @tag.to_s
48
52
  end
49
- @argument = nil
53
+
54
+ @arguments = nil
50
55
  end
51
56
 
52
57
  def omit_content?
@@ -124,6 +129,10 @@ module WrapIt
124
129
  end
125
130
  end
126
131
 
132
+ def unwrap
133
+ @wrapper = nil
134
+ end
135
+
127
136
  protected
128
137
 
129
138
  #
@@ -132,10 +141,10 @@ module WrapIt
132
141
  # @param name [<Symbol, String>] Tag name. Converted to `String`.
133
142
  #
134
143
  # @return [void]
135
- def self.default_tag(name)
144
+ def self.default_tag(name, override = true)
136
145
  name.is_a?(String) || name.is_a?(Symbol) ||
137
146
  fail(ArgumentError, 'Tag name should be a String or Symbol')
138
- @default_tag = name.to_s
147
+ override ? @default_tag = name.to_s : @default_tag ||= name.to_s
139
148
  end
140
149
 
141
150
  def self.omit_content
@@ -166,7 +175,9 @@ module WrapIt
166
175
 
167
176
  def do_render
168
177
  # cleanup options from empty values
169
- @options.select! { |k, v| !v.nil? && !v.empty? }
178
+ @options.select! do |k, v|
179
+ !v.nil? && (!v.respond_to?(:empty?) || !v.empty?)
180
+ end
170
181
  run_callbacks :render do
171
182
  @content = content_tag(tag, @content, options)
172
183
  end
@@ -5,8 +5,9 @@ module WrapIt
5
5
  # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
6
6
  #
7
7
  module Callbacks
8
+ extend DerivedAttributes
9
+
8
10
  def self.included(base)
9
- extend DerivedAttributes
10
11
  base.extend ClassMethods
11
12
  end
12
13
 
data/lib/wrap_it/enums.rb CHANGED
@@ -19,6 +19,7 @@ module WrapIt
19
19
  private
20
20
 
21
21
  def enums_init
22
+ @enums = {}
22
23
  opt_keys = @options.keys
23
24
  enums.each do |name, opts|
24
25
  value = nil
@@ -36,7 +37,7 @@ module WrapIt
36
37
  end
37
38
 
38
39
  def enums
39
- @nums ||= self.class.collect_derived(:@enums, {}, :merge)
40
+ @enums_hash ||= self.class.collect_derived(:@enums, {}, :merge)
40
41
  end
41
42
 
42
43
  #
@@ -89,8 +90,7 @@ module WrapIt
89
90
  opts[:regexp] = /\A#{prefix}(?:#{values.join('|')})\z/
90
91
  opts[:html_class_prefix] = prefix
91
92
  end
92
- var = "@#{name}".to_sym
93
- define_method("#{name}") { instance_variable_get(var) }
93
+ define_method("#{name}") { @enums[name] }
94
94
  define_method("#{name}=", &Enums.setter(name, &block))
95
95
  @enums ||= {}
96
96
  @enums[name] = opts
@@ -104,7 +104,7 @@ module WrapIt
104
104
  opts = enums[name]
105
105
  v = value if opts[:values].include?(value)
106
106
  v ||= opts[:default] if opts.key?(:default)
107
- instance_variable_set("@#{name}", v)
107
+ @enums[name] = v
108
108
  block.nil? || instance_exec(v, &block)
109
109
  if opts.key?(:regexp)
110
110
  remove_html_class(opts[:regexp])
@@ -46,7 +46,7 @@ module WrapIt
46
46
  #
47
47
  # @return [String] HTML class prefix.
48
48
  def html_class_prefix
49
- self.class.html_class_prefix
49
+ @html_class_prefix ||= self.class.html_class_prefix
50
50
  end
51
51
 
52
52
  #
@@ -222,7 +222,7 @@ module WrapIt
222
222
  #
223
223
  # @return [void]
224
224
  def html_class_prefix(prefix = nil)
225
- return get_derived(:@html_class_prefix) || '' if prefix.nil?
225
+ return(get_derived(:@html_class_prefix) || '') if prefix.nil?
226
226
  prefix.is_a?(String) || prefix.is_a?(Symbol) || fail(
227
227
  ArgumentError, 'prefix should be a String or Symbol'
228
228
  )
@@ -0,0 +1,41 @@
1
+ module WrapIt
2
+ #
3
+ # Link
4
+ #
5
+ # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
6
+ #
7
+ class Link < Base
8
+ include TextContainer
9
+
10
+ default_tag 'a'
11
+
12
+ def href
13
+ @options[:href]
14
+ end
15
+
16
+ def href=(value)
17
+ if value.is_a?(Hash)
18
+ defined?(Rails) || fail(
19
+ ArgumentError,
20
+ 'Hash links supported only in Rails env'
21
+ )
22
+ value = @template.url_for(value)
23
+ end
24
+ value.is_a?(String) || fail(ArgumentError, 'Wrong link type')
25
+ @options[:href] = value
26
+ end
27
+
28
+ before_initialize do
29
+ link = @options[:link] || @options[:href] || @options[:url]
30
+ @options.delete(:link)
31
+ @options.delete(:href)
32
+ @options.delete(:url)
33
+ unless link.is_a?(String) || link.is_a?(Hash)
34
+ @block.nil? && tmp = @arguments.extract_first!(String)
35
+ link = @arguments.extract_first!(String)
36
+ tmp.nil? || @arguments.unshift(tmp)
37
+ end
38
+ link.nil? || self.href = link
39
+ end
40
+ end
41
+ end
@@ -19,8 +19,9 @@ module WrapIt
19
19
  private
20
20
 
21
21
  def switches_init
22
+ @switches = {}
22
23
  keys = switches.keys
23
- keys.each { |switch| instance_variable_set("@#{switch}", false) }
24
+ # keys.each { |switch| @switches[switch] = false }
24
25
  @options.keys.select { |o| keys.include?(o) }.each do |switch|
25
26
  send("#{switches[switch][:name]}=", @options.delete(switch) == true)
26
27
  end
@@ -30,7 +31,7 @@ module WrapIt
30
31
  end
31
32
 
32
33
  def switches
33
- @switches ||= self.class.collect_derived(:@switches, {}, :merge)
34
+ @switches_hash ||= self.class.collect_derived(:@switches, {}, :merge)
34
35
  end
35
36
 
36
37
  #
@@ -83,8 +84,7 @@ module WrapIt
83
84
  end
84
85
  end
85
86
  names = [name] + [[options[:aliases]] || []].flatten.compact
86
- var = "@#{name}".to_sym
87
- define_method("#{name}?") { instance_variable_get(var) == true }
87
+ define_method("#{name}?") { @switches[name] == true }
88
88
  define_method("#{name}=", &Switches.setter(name, &block))
89
89
  @switches ||= {}
90
90
  names.each { |n| @switches[n] = options }
@@ -102,7 +102,7 @@ module WrapIt
102
102
  def self.setter(name, &block)
103
103
  proc do |value|
104
104
  opts = switches[name]
105
- instance_variable_set("@#{name}", value == true)
105
+ @switches[name] = value == true
106
106
  if value == true
107
107
  opts.key?(:html_class) && add_html_class(*opts[:html_class])
108
108
  block.nil? || instance_exec(true, &block)
@@ -7,7 +7,7 @@ module WrapIt
7
7
  module TextContainer
8
8
  def self.included(base)
9
9
  base.class_eval do
10
- default_tag 'p'
10
+ default_tag 'p', false
11
11
 
12
12
  after_initialize do
13
13
  @body = @arguments.extract_first!(String) || empty_html
@@ -1,4 +1,4 @@
1
1
  #
2
2
  module WrapIt
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
data/lib/wrap_it.rb CHANGED
@@ -74,3 +74,4 @@ end
74
74
  require 'wrap_it/base'
75
75
  require 'wrap_it/container'
76
76
  require 'wrap_it/text_container'
77
+ require 'wrap_it/link'
@@ -14,10 +14,17 @@ 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
17
+ it 'drops @arguments after init' do
18
18
  expect(
19
19
  successor.instance_variable_get(:@arguments)
20
- ).to be_kind_of WrapIt::ArgumentsArray
20
+ ).to be_nil
21
+ end
22
+
23
+ it 'extends @arguments with ArgumentsArray module' do
24
+ args = nil
25
+ successor_class.class_eval { after_initialize { args = @arguments } }
26
+ successor
27
+ expect(args).to be_kind_of WrapIt::ArgumentsArray
21
28
  end
22
29
 
23
30
  it 'symbolizes options hash' do
@@ -6,4 +6,6 @@ describe WrapIt::Container do
6
6
  # successor.test
7
7
  # expect(successor.instance_variable_get(:@children).size).to eq 1
8
8
  # end
9
+
10
+ pending 'adds safe helpers'
9
11
  end
@@ -10,12 +10,21 @@ describe WrapIt::HTMLClass do
10
10
  expect(sub_class.new(template).html_class).to eq %w(a d e b c)
11
11
  end
12
12
 
13
- it 'has self.html_class_prefix and #html_class_prefix methods' do
14
- expect(wrapper.html_class_prefix).to eq ''
15
- wrapper_class.class_eval { html_class_prefix 'e-' }
16
- expect(wrapper.html_class_prefix).to eq 'e-'
17
- sub_class = Class.new(wrapper_class)
18
- expect(sub_class.new(template).html_class_prefix).to eq 'e-'
13
+ describe '#html_class_prefix' do
14
+ it 'returns empty string by default' do
15
+ expect(wrapper.html_class_prefix).to eq ''
16
+ end
17
+
18
+ it 'returns value setted by class method' do
19
+ wrapper_class.class_eval { html_class_prefix 'e-' }
20
+ expect(wrapper.html_class_prefix).to eq 'e-'
21
+ end
22
+
23
+ it 'returns derived value' do
24
+ wrapper_class.class_eval { html_class_prefix 'e-' }
25
+ sub_class = Class.new(wrapper_class)
26
+ expect(sub_class.new(template).html_class_prefix).to eq 'e-'
27
+ end
19
28
  end
20
29
 
21
30
  it 'has #add_html_class with chaining' do
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe WrapIt::Link do
4
+ it 'has `a` tag by default' do
5
+ expect(successor.tag).to eq 'a'
6
+ end
7
+
8
+ it { expect(successor).to be_kind_of WrapIt::TextContainer }
9
+
10
+ it 'takes href from options' do
11
+ [:link, :href, :url].each do |key|
12
+ @successor = nil
13
+ expect(successor(key => 'url').href).to eq 'url'
14
+ end
15
+ end
16
+
17
+ it 'takes href from first string arg if block present' do
18
+ expect(successor('url') { 'text' }.href).to eq 'url'
19
+ end
20
+
21
+ it 'takes href from second string arg if no block given' do
22
+ expect(successor('text', 'url').href).to eq 'url'
23
+ expect(successor.instance_variable_get(:@body)).to eq 'text'
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrap_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Ovchinnikov
@@ -116,6 +116,7 @@ files:
116
116
  - lib/wrap_it/enums.rb
117
117
  - lib/wrap_it/html_class.rb
118
118
  - lib/wrap_it/html_data.rb
119
+ - lib/wrap_it/link.rb
119
120
  - lib/wrap_it/no_rails.rb
120
121
  - lib/wrap_it/rails.rb
121
122
  - lib/wrap_it/switches.rb
@@ -130,6 +131,7 @@ files:
130
131
  - spec/lib/enums_spec.rb
131
132
  - spec/lib/html_class_spec.rb
132
133
  - spec/lib/html_data_spec.rb
134
+ - spec/lib/link_spec.rb
133
135
  - spec/lib/switches_spec.rb
134
136
  - spec/rails/base_spec.rb
135
137
  - spec/rails/container_spec.rb
@@ -174,6 +176,7 @@ test_files:
174
176
  - spec/lib/enums_spec.rb
175
177
  - spec/lib/html_class_spec.rb
176
178
  - spec/lib/html_data_spec.rb
179
+ - spec/lib/link_spec.rb
177
180
  - spec/lib/switches_spec.rb
178
181
  - spec/rails/base_spec.rb
179
182
  - spec/rails/container_spec.rb