html-native 0.2.2 → 0.2.3

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/html-native/builder.rb +49 -4
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4688be25684c101d4edf8c6ee05d061525a08c5fd4e9671b0ebf8f7d286ac11c
4
- data.tar.gz: cf500e5cd56ec00233eac3719f647432a75ea8aab6e955fcf6e9140e13cede51
3
+ metadata.gz: 176be6b557435093c4163f66b0322073f986282895571dfd34fedceeb2b02ec8
4
+ data.tar.gz: 836e4f3e0a94e1ea70da575b900f10fed0e4da232c87e0ac83f0eaa45b3612da
5
5
  SHA512:
6
- metadata.gz: 9a786002324a79d8d1f5a5f62662364a74caac0557be563c06fd2c95610df4b84827c83fdb20a68ffdbd75637ca8da1535f81f2c488d075b5a34b7fc77afd31d
7
- data.tar.gz: 5aa97fd17361de4c685833fbbcbacdfe2f6769abec058a7729e22287eb3185b703a38ba8b291b69fbe02f74f9f43227380c6963610ce73efa02242c2c51fecf9
6
+ metadata.gz: 4346fd63e18a2187312c94b5d311467b1489c084ff23a80591aa726afe40a5ca5fb24aa160cb36f591eefb941fb8725b51c8a4d146d7b0ba82649edfdc475ba3
7
+ data.tar.gz: 761266f6ea9dd4340dfd8287e94e21721b6411cbcf3f4a37ac6fade2a12bbb1c00af9bde691ada4292255157039ddb26118738644cb71f9ce51c1a3b85d20b47
@@ -1,14 +1,29 @@
1
1
  require "html-native"
2
2
  module HTMLComponent
3
+ # Represents a String being constructed, and can be more or less treated
4
+ # as a String. Builder creates a String from whatever is put into it, but
5
+ # it delays construction until it's absolutely necessary, then it caches the
6
+ # result.
3
7
  class Builder
8
+ # Build a new string builder instance, immediately constructing and caching
9
+ # the initial value.
4
10
  def initialize(strings = [])
5
- if strings.kind_of? String
6
- @strings = [strings]
11
+ @strings = []
12
+ @cache = case strings.class
13
+ when Array
14
+ strings.join
15
+ when Enumerable
16
+ strings.to_a.join
7
17
  else
8
- @strings = strings.dup
18
+ strings.to_s
9
19
  end
20
+ @cached = true
10
21
  end
11
22
 
23
+ # Appends a value to the Builder instance. If it is another builder, it is
24
+ # added, but not converted to a String yet. If it is an HTMLComponent, it is
25
+ # rendered. If it is anything else, it is converted to a String. This
26
+ # invalidates the cache.
12
27
  def +(string)
13
28
  if string.kind_of? Builder
14
29
  @strings << string
@@ -17,14 +32,44 @@ module HTMLComponent
17
32
  else
18
33
  @strings << string.to_s
19
34
  end
35
+ @cached = false
20
36
  self
21
37
  end
22
38
 
39
+ alias_method :<<, :+
40
+
41
+ # Same as +, but allows multiple values to be appended.
42
+ def concat(*strings)
43
+ strings.each do |s|
44
+ self + s
45
+ end
46
+ self
47
+ end
48
+
49
+ # Converts the Builder to a String. If the cache is valid, it is returned.
50
+ # Otherwise, the new result is created, cached, and returned.
23
51
  def to_s
24
- @strings.join
52
+ unless @cached
53
+ @cache << @strings.join
54
+ @strings.clear
55
+ @cached = true
56
+ end
57
+ @cache
25
58
  end
26
59
 
27
60
  alias to_str to_s
61
+
62
+ # If the method does not exist on Builder, it is sent to String, by way
63
+ # of the rendered Builder result. Modify-in-place methods will affect the
64
+ # underlying String.
65
+ def method_missing(method, *args, &block)
66
+ to_s.send(method, *args, &block)
67
+ end
68
+
69
+ # If String responds to the method, then Builder also responds to it.
70
+ def respond_to_missing?(method)
71
+ "".respond_to?(method)
72
+ end
28
73
  end
29
74
  end
30
75
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kellen Watt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-30 00:00:00.000000000 Z
11
+ date: 2021-02-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An html generation DSL designed for fluid code creation.
14
14
  email: