html-native 0.2.1 → 0.3.0

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: 6ba13af6f45f467e85f5880d5fd690a5e0cad66b6fbbe1a1a9e790c8d6c1f564
4
- data.tar.gz: 7f41524489dff7db2c4876bb761b1fc06fe7d3a61b30a4d4606bdc3a2ce03400
3
+ metadata.gz: 21997059907e655e0171cd63d1b7bac5be51d1c3474c87849b508463a1ab011b
4
+ data.tar.gz: c87c0911546412b8954dd674f22ccd128f0b2ce985df085e2f38cbce3c150686
5
5
  SHA512:
6
- metadata.gz: 989fbc6f4dd146d6eaaadeb5f34a42172163c6c1b786128377b806aa099e6a64fea71af58af27a56397ff16e30a66973f16a5c3c99229736ac8cb7b7f49fe737
7
- data.tar.gz: ea723f768aec7c753d26c6ffa47fb8077b747d8afd256668ee09838d593b0eb45dcd479381179ae4bbfc7a2433789fdaddc5801e0ded3c085a6a7c468e2b33b0
6
+ metadata.gz: 566d2c708f9c6829eb727f71f461d1499093ab995a35fdb5748f8b68dc157fb4bf6c650c02acc5cd6a6fc14c88224ea1c0921cbfded11244ebe6a7890ff4e3d7
7
+ data.tar.gz: ed6f3a80fea8a20e058770804255f87b93999dcf6d0b81cebaead9bbd2628750e2581549cbc536c16f8581b046e8f5fdfd0f6fc07fe86dc2165a10bf2a35cfdc
data/lib/html-native.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "html-native/constants"
2
2
  require "html-native/builder"
3
+ require "html-native/logic"
3
4
 
4
5
  module HTMLComponent
5
6
 
@@ -17,6 +18,10 @@ module HTMLComponent
17
18
  end
18
19
  end
19
20
 
21
+ def doctype(type)
22
+ Builder.new("<!DOCTYPE #{type}>")
23
+ end
24
+
20
25
  # Creates a module that encompasses the given block in an HTMLComponent
21
26
  # context. This gives access to methods in the block as though the block was
22
27
  # declared as the `render` function in a module extending HTMLComponent
@@ -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]
7
- else
8
- @strings = strings.dup
9
- end
11
+ @strings = []
12
+ @cache =
13
+ if strings.kind_of? Array
14
+ strings.join
15
+ elsif strings.kind_of? Enumerable
16
+ strings.to_a.join
17
+ else
18
+ strings.to_s
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,11 +32,43 @@ module HTMLComponent
17
32
  else
18
33
  @strings << string.to_s
19
34
  end
35
+ @cached = false
36
+ self
37
+ end
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
20
46
  self
21
47
  end
22
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
58
+ end
59
+
60
+ alias_method :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, include_all)
71
+ "".respond_to?(method, include_all)
25
72
  end
26
73
  end
27
74
  end
@@ -0,0 +1,38 @@
1
+ module HTMLComponent
2
+ class Builder
3
+ define_method(:if) do |bool|
4
+ if bool
5
+ self
6
+ else
7
+ Builder.new
8
+ end
9
+ end
10
+
11
+ define_method(:unless) do |bool|
12
+ if bool
13
+ Builder.new
14
+ else
15
+ self
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class String
22
+ define_method(:if) do |bool|
23
+ if bool
24
+ Builder.new(self)
25
+ else
26
+ Builder.new
27
+ end
28
+ end
29
+
30
+ define_method(:unless) do |bool|
31
+ # BAd form, purely for symmetry
32
+ unless bool
33
+ Builder.new(self)
34
+ else
35
+ Builder.new
36
+ end
37
+ end
38
+ end
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.1
4
+ version: 0.3.0
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:
@@ -20,6 +20,7 @@ files:
20
20
  - lib/html-native/builder.rb
21
21
  - lib/html-native/collections.rb
22
22
  - lib/html-native/constants.rb
23
+ - lib/html-native/logic.rb
23
24
  homepage: https://github.com/KellenWatt/html-native
24
25
  licenses:
25
26
  - MIT