html_slice 0.2.6 → 0.2.7

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
  SHA256:
3
- metadata.gz: cc11400cb9377b56446b53449e22298145f8ae75f1815236f273c5cd48790350
4
- data.tar.gz: ea3bdc2af9e052d6310e146da37f2653cbdeb13aee3859acf24f2a32366f632e
3
+ metadata.gz: a79c8996b475562f9dda72bed310544562e53a291e1cee0d539a3c3280665fb6
4
+ data.tar.gz: b17fe1d34ce2faa82441654496e38d92b28e001ef079b74d6b8e1d481de5fd85
5
5
  SHA512:
6
- metadata.gz: cc47edc7352f2224d0aea43d271e5ef453eceb4b0d74039562a20ec75714c93c03f4b20f62e4f349526975665257753348520879699bb650d6b6bc6bf58855a2
7
- data.tar.gz: dc524fdfbca74a3efb70af174bd4860c7f4ee5df377e680dfd3cdbe2467a932183e2ab5faafc9498b95f7b94e4597fd138b3ba098b62cba58ffe44b9da62503d
6
+ metadata.gz: 87e6f121b744b3c39b9150b582079455b682e3607190042bc75b987ff4d3a692614a408ed493a4ef1a40fdf008a3548a21e3220c2ab4962516793daa975b59d3
7
+ data.tar.gz: 1db761b53c2bd886cc644fb192a23f6d6e0816683f3527320044b739acba2868a5cfccb7503f4f9ea2e365fa5d7e5ed175e996aabadd743793d34ec621928346
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HtmlSlice
4
- VERSION = "0.2.6"
4
+ VERSION = "0.2.7"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -22,9 +22,25 @@ module HtmlSlice
22
22
  ].freeze
23
23
 
24
24
  DEFAULT_SLICE = :default
25
- ATTRIBUTE_CACHE = {}
26
25
 
27
- def self.slice(slice_id = DEFAULT_SLICE, wrap: ["", ""], &block)
26
+ ATTRIBUTE_KEY_CACHE = {}
27
+
28
+ STR = {
29
+ empty: "",
30
+ space: " ",
31
+ underline: "_",
32
+ dash: "-",
33
+ attr_equal: "='",
34
+ single_quote: "'",
35
+ doctype_html: "<!DOCTYPE html><html>",
36
+ end_html: "</html>",
37
+ gt: ">",
38
+ gt_slash: "/>",
39
+ lt: "<",
40
+ lt_slash: "</",
41
+ }.freeze
42
+
43
+ def self.slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block)
28
44
  @class ||= Class.new { include HtmlSlice }.new
29
45
  @class.html_slice(slice_id, wrap: wrap, &block)
30
46
  end
@@ -35,15 +51,15 @@ module HtmlSlice
35
51
  end
36
52
 
37
53
  def html_layout(slice_id = DEFAULT_SLICE, &block)
38
- html_slice(slice_id, wrap: ["<!DOCTYPE html><html>", "</html>"], &block)
54
+ html_slice(slice_id, wrap: [STR[:doctype_html], STR[:end_html]], &block)
39
55
  end
40
56
 
41
- def html_slice(slice_id = DEFAULT_SLICE, wrap: ["", ""], &block)
57
+ def html_slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block)
42
58
  @html_slice_current_id = slice_id
43
59
  @html_slice ||= {}
44
60
 
45
61
  if block_given?
46
- buffer = +""
62
+ buffer = +STR[:empty]
47
63
  buffer << wrap[0]
48
64
  @html_slice[slice_id] = buffer
49
65
  instance_eval(&block)
@@ -81,7 +97,7 @@ module HtmlSlice
81
97
  private
82
98
 
83
99
  def parse_args(args, escape: true)
84
- content = ""
100
+ content = STR[:empty]
85
101
  attributes = {}
86
102
  first = args[0]
87
103
  if first.is_a?(String)
@@ -96,26 +112,25 @@ module HtmlSlice
96
112
  def render_tag(name_str, tag_sym, content, attributes, &block)
97
113
  @html_slice ||= {}
98
114
  @html_slice_current_id ||= DEFAULT_SLICE
99
- @html_slice[@html_slice_current_id] ||= +""
115
+ @html_slice[@html_slice_current_id] ||= +STR[:empty]
100
116
  buffer = @html_slice[@html_slice_current_id]
101
117
 
102
- buffer << "<" << name_str
118
+ buffer << STR[:lt] << name_str
103
119
  unless attributes.empty?
104
- attributes_string = ATTRIBUTE_CACHE[attributes.dup] ||= attributes.map do |key, value|
105
- " #{key.to_s.tr("_", "-")}='#{value}'"
106
- end.join
107
-
108
- buffer << attributes_string
120
+ attributes.each do |key, value|
121
+ cached_key = ATTRIBUTE_KEY_CACHE[key] ||= key.to_s.tr(STR[:underline], STR[:dash]).freeze
122
+ buffer << STR[:space] << cached_key << STR[:attr_equal] << value.to_s << STR[:single_quote]
123
+ end
109
124
  end
110
125
 
111
126
  if block_given?
112
- buffer << ">"
127
+ buffer << STR[:gt]
113
128
  instance_exec(&block)
114
- buffer << "</#{name_str}>"
129
+ buffer << [STR[:lt_slash], name_str, STR[:gt]].join
115
130
  elsif content.empty? && EMPTY_TAGS.include?(tag_sym)
116
- buffer << "/>"
131
+ buffer << STR[:gt_slash]
117
132
  else
118
- buffer << ">" << content << "</#{name_str}>"
133
+ buffer << STR[:gt] << content << [STR[:lt_slash], name_str, STR[:gt]].join
119
134
  end
120
135
  end
121
136
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_slice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - henrique-ft