fun_html 0.2.0 → 0.3.1

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: 3e5e644d2ee9775409d54d3340ee692e557614bb1e85ef0c290317c0d00e9432
4
- data.tar.gz: 4792b3bf1361a392a59221c43a937ce1922108d09b6f799a6c5a74466c41e760
3
+ metadata.gz: 5bf8ac3683a1e55c9178d17d85703ec445c26f8a544db4a778252fa3128eb333
4
+ data.tar.gz: 8213e1aec0fff6f4357721ed1df892a3b31069ad2c93a53b0a64c2c40c4a87ad
5
5
  SHA512:
6
- metadata.gz: 808d8bb9950923c28e12b81c39d7be030aa2cf315d73e4b28efe157398aeaaacc349983bb785e167e59c6cad284656f6e310fff215d8022e15eebf89c1f51b81
7
- data.tar.gz: a75a3249ea3253801467b1324b6da56c090ca919ea74309a900f80635d7a01dadfeab966844e98a20305a4b3cd0f96621ac929157309df861fddc3e511c424d4
6
+ metadata.gz: f7373307fa00fad6e75bf8db7b0c490dd1b808d37b05c3406c5a862c39b8170befef1057b01e1c9577966ed11b1d1bf4ada88741ac5bd213314fb241de656fea
7
+ data.tar.gz: 13b9377037fd8be26000f9bf746d858645f4326423139beee6a7032f1c37e19fc7290baf10572be84aa3d7c2a303086dc18b5697ec215b8c11c7d0796085bfb9
@@ -32,7 +32,9 @@ module FunHtml
32
32
 
33
33
  # only allow nil or objects that respond to `safe_attribute`
34
34
  def self.to_html(attr)
35
- attr&.safe_attribute.to_s
35
+ return '' if attr.nil?
36
+
37
+ attr.safe_attribute
36
38
  end
37
39
 
38
40
  # create a new Attribute object to create reuseable attributes
@@ -60,6 +62,16 @@ module FunHtml
60
62
  write(' class="', value)
61
63
  end
62
64
 
65
+ # classes takes a hash of class names and boolean values
66
+ # only the names with a true value will be printed
67
+ # classes({'a' => true, 'b' => false, 'c' => true}) -> ' class="a c"'
68
+ def classes(list)
69
+ klass list.select { |_k, v| v == true }.keys.join(' ')
70
+ end
71
+
72
+ # alias so klass has classes
73
+ def klasses(list) = classes(list)
74
+
63
75
  # Merge another Attribute to create a new, combined, Attribute.
64
76
  def merge(other)
65
77
  self.class.new(@__buffer.merge(other.instance_variable_get(:@__buffer)))
@@ -55,9 +55,26 @@ module FunHtml
55
55
  obj
56
56
  end
57
57
 
58
+ JOIN_ERROR = <<~TXT.freeze
59
+ You can not join templates which are created by the parent template.
60
+
61
+ This will fail:
62
+
63
+ FunHtml::Template.start { |t| t.join [t.text("hello")] }
64
+
65
+ Instead only join new templates
66
+ FunHtml::Template.start do |t|
67
+ t.join [FunHtml::Template.start { |x| x.text('hello') }]
68
+ end
69
+ TXT
70
+
58
71
  # join an array of other templates into this template.
59
72
  def join(templates)
60
- templates.each { @__buffer << _1.render }
73
+ templates.each do |t|
74
+ raise JOIN_ERROR if t == self
75
+
76
+ @__buffer << t.render
77
+ end
61
78
  self
62
79
  end
63
80
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FunHtml
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.1'
5
5
  end
@@ -10,15 +10,15 @@ module FunHtml
10
10
  include Kernel
11
11
 
12
12
  def initialize
13
- @__buffer = +''
13
+ @__buffer = String.new(capacity: 1024)
14
14
  end
15
15
 
16
16
  # Render produces the HTML string and clears the buffer.
17
17
  def render
18
- @__buffer
19
- ensure
18
+ result = @__buffer
20
19
  # empty the buffer to prevent double rendering
21
- @__buffer = +''
20
+ @__buffer = String.new(capacity: 1024)
21
+ result
22
22
  end
23
23
 
24
24
  private
@@ -33,9 +33,12 @@ module FunHtml
33
33
  CLOSE_VOID = '/>'
34
34
 
35
35
  def write(open, close, attr = nil, closing_char: CLOSE, &block)
36
- @__buffer << open << Attribute.to_html(attr)
36
+ if attr
37
+ @__buffer << open << Attribute.to_html(attr) << closing_char
38
+ else
39
+ @__buffer << open << closing_char
40
+ end
37
41
 
38
- @__buffer << closing_char
39
42
  yield self if block
40
43
  @__buffer << close
41
44
 
data/rbi/fun_html.rbi CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  module FunHtml
5
5
  class Template
6
+ JOIN_ERROR = T.let(T.unsafe(nil), String)
7
+
6
8
  include FunHtml::Writer
7
9
  include FunHtml::SpecElements::HTMLAllElements
8
10
 
@@ -76,6 +78,12 @@ module FunHtml
76
78
  sig { params(attr: T.nilable(FunHtml::Attribute)).returns(String) }
77
79
  def self.to_html(attr); end
78
80
 
81
+ sig { params(list: T::Hash[String, T::Boolean]).returns(FunHtml::Attribute) }
82
+ def classes(list); end
83
+
84
+ sig { params(list: T::Hash[String, T::Boolean]).returns(FunHtml::Attribute) }
85
+ def klasses(list); end
86
+
79
87
  sig { params(other: FunHtml::Attribute).returns(FunHtml::Attribute) }
80
88
  def merge(other); end
81
89
 
data/rbi/fun_html.rbx CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  module FunHtml
5
5
  class Template
6
+ JOIN_ERROR = T.let(T.unsafe(nil), String)
7
+
6
8
  include FunHtml::Writer
7
9
  include FunHtml::SpecElements::HTMLAllElements
8
10
 
@@ -76,6 +78,12 @@ module FunHtml
76
78
  sig { params(attr: T.nilable(FunHtml::Attribute)).returns(String) }
77
79
  def self.to_html(attr); end
78
80
 
81
+ sig { params(list: T::Hash[String, T::Boolean]).returns(FunHtml::Attribute) }
82
+ def classes(list); end
83
+
84
+ sig { params(list: T::Hash[String, T::Boolean]).returns(FunHtml::Attribute) }
85
+ def klasses(list); end
86
+
79
87
  sig { params(other: FunHtml::Attribute).returns(FunHtml::Attribute) }
80
88
  def merge(other); end
81
89
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fun_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Weir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-14 00:00:00.000000000 Z
11
+ date: 2025-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: It probably is not much fun, despite the name.
14
14
  email: