berns 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/berns.rb +43 -30
  3. metadata +32 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d86db169c443fbe77f5f22560d9e5dabec886e9dc4092258d0baf124a0d1cc0
4
- data.tar.gz: 11c1d828b8f26628dd813fdd3df8cd39a0e42e83b11f5b1df64f87716e8afdb4
3
+ metadata.gz: 1f544945cd9c43afe1ce6d3bdf6e7a3c026b6386925cbe7ac9a6ba36ba228b76
4
+ data.tar.gz: 21add20e6f88c5c56ba48a5b60d48c80e4f4be0b9b351540345479f2a5f94896
5
5
  SHA512:
6
- metadata.gz: a5d58daef750ba78d776c8ce34bc5ddcdb90701718b8f6110871363c05926219c60dddfbc1eba14a6decf5f2645b6c75f9a3d9f014b8c024a831e8fa13997b2e
7
- data.tar.gz: e184ef843bb995eaf0008c551e7f7d7bf86906d0c6a51f9f7b1c7ff7a4af1bea1c28cc2fde5e88c01d367cb6ec4e9d717d70ae0d8618331f3c6e479d48890c5d
6
+ metadata.gz: 40ac56793d022c1191c775aba8020221e6251b9c9be9f61485bd4e0ad3b90e3b46e2e1f4aa1070a3fadce6026b2f48f4b31da156bdba89930fe7c3d557fff06f
7
+ data.tar.gz: 1af75731f2ce07e6978653b45f077ff20784d950cecbfce74970185c3c2a487c306187d821fed30edba3f4e920a5a7741c9bd009c584f86c7e1c6a0a3d9272fe
@@ -3,6 +3,7 @@ require 'cgi/escape'
3
3
 
4
4
  # A utility library for generating HTML strings
5
5
  module Berns
6
+ HASH = {}.freeze
6
7
  SPACE = ' '
7
8
  EMPTY = ''
8
9
 
@@ -17,15 +18,15 @@ module Berns
17
18
 
18
19
  # Dynamically defined methods that are simple proxies to {#element}.
19
20
  STANDARD.each do |elm|
20
- define_singleton_method(elm) do |arguments = {}, &block|
21
- element(elm, arguments, &block)
21
+ define_singleton_method(elm) do |attributes = HASH, &block|
22
+ element(elm, attributes, &block)
22
23
  end
23
24
  end
24
25
 
25
26
  # Dynamically defined methods that are simple proxies to {#void}.
26
27
  VOID.each do |elm|
27
- define_singleton_method(elm) do |arguments = {}|
28
- void(elm, arguments)
28
+ define_singleton_method(elm) do |attributes = HASH|
29
+ void(elm, attributes)
29
30
  end
30
31
  end
31
32
 
@@ -54,14 +55,10 @@ module Berns
54
55
  # @yieldreturn [String]
55
56
  # The textual content of the element. May be HTML or plain text.
56
57
  # @return [String]
57
- def self.element(tag, attributes = {})
58
- content = yield if block_given?
58
+ def self.element(tag, attributes = HASH)
59
+ return "<#{ tag }>#{ yield if block_given? }</#{ tag }>" if attributes.empty?
59
60
 
60
- # Move stuff around unless the attributes are empty.
61
- attrs = to_attributes(attributes)
62
- attrs = " #{ attrs }" unless attrs.empty?
63
-
64
- "<#{ tag }#{ attrs }>#{ content }</#{ tag }>"
61
+ "<#{ tag } #{ to_attributes(attributes) }>#{ yield if block_given? }</#{ tag }>"
65
62
  end
66
63
 
67
64
  # Same as above, but generates void elements i.e. ones without any textual
@@ -71,12 +68,10 @@ module Berns
71
68
  # void(:br) # => "<br>"
72
69
  #
73
70
  # @return [String]
74
- def self.void(tag, attributes = {})
75
- # Move stuff around unless the attributes are empty.
76
- attrs = to_attributes(attributes)
77
- attrs = " #{ attrs }" unless attrs.empty?
71
+ def self.void(tag, attributes = HASH)
72
+ return "<#{ tag }>" if attributes.empty?
78
73
 
79
- "<#{ tag }#{ attrs }>"
74
+ "<#{ tag } #{ to_attributes(attributes) }>"
80
75
  end
81
76
 
82
77
  # Converts a hash into HTML attributes by mapping each key/value combination
@@ -90,12 +85,22 @@ module Berns
90
85
  #
91
86
  # @param attributes [Hash]
92
87
  # The hash to convert to HTML attributes.
88
+ #
93
89
  # @return [String]
94
90
  # The space-joined string containing HTML attributes.
95
91
  def self.to_attributes(attributes)
96
- attributes.map do |attribute, value|
97
- to_attribute(attribute, value)
98
- end.join(SPACE).chomp(SPACE)
92
+ return EMPTY if attributes.empty?
93
+
94
+ string = +''
95
+
96
+ attributes.each do |attr, value|
97
+ string << SPACE
98
+
99
+ to_attribute(attr, value, string)
100
+ end
101
+
102
+ string.strip!
103
+ string
99
104
  end
100
105
 
101
106
  # Converts a single attribute and value into an HTML attribute string.
@@ -107,20 +112,28 @@ module Berns
107
112
  # The attribute key.
108
113
  # @param value [String, Boolean, Hash]
109
114
  # The value to assign to the attribute.
115
+ # @param string [String, nil]
116
+ # The string modify in place with attributes and values.
117
+ #
110
118
  # @return [String]
111
119
  # A single HTML attribute.
112
- def self.to_attribute(attribute, value)
113
- case value
114
- when TrueClass
115
- attribute.to_s
116
- when Hash
117
- value.map do |subattribute, subvalue|
118
- to_attribute(subattribute ? "#{ attribute }-#{ subattribute }" : attribute, subvalue)
119
- end.join(SPACE)
120
- when FalseClass
121
- EMPTY
120
+ def self.to_attribute(attribute, value, string = +'')
121
+ if value.is_a?(FalseClass) # rubocop:disable Style/CaseLikeIf
122
+ # noop
123
+ elsif value.is_a?(TrueClass)
124
+ string << attribute.to_s
125
+ elsif value.is_a?(Hash)
126
+ value.each do |attr, subval|
127
+ string << SPACE
128
+
129
+ to_attribute(attr.nil? ? attribute : "#{ attribute }-#{ attr }", subval, string)
130
+ end
131
+
132
+ string.strip!
122
133
  else
123
- %(#{ attribute }="#{ CGI.escapeHTML(value.to_s) }")
134
+ string << %(#{ attribute }="#{ CGI.escapeHTML(value.to_s) }")
124
135
  end
136
+
137
+ string
125
138
  end
126
139
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berns
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-01-08 00:00:00.000000000 Z
12
+ date: 2020-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -53,6 +53,34 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop-performance
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
56
84
  description: A utility library for generating HTML strings.
57
85
  email:
58
86
  - beck.taylorg@gmail.com
@@ -76,14 +104,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
104
  requirements:
77
105
  - - ">="
78
106
  - !ruby/object:Gem::Version
79
- version: 2.3.0
107
+ version: 2.5.0
80
108
  required_rubygems_version: !ruby/object:Gem::Requirement
81
109
  requirements:
82
110
  - - ">="
83
111
  - !ruby/object:Gem::Version
84
112
  version: '0'
85
113
  requirements: []
86
- rubygems_version: 3.1.2
114
+ rubygems_version: 3.1.4
87
115
  signing_key:
88
116
  specification_version: 4
89
117
  summary: A utility library for generating HTML strings.