html-native 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4e5910254a0afad80152339f40b75051ac6045ab51b95f3a0cfd44c6d9d9c81
4
- data.tar.gz: a291bbadcb69cb44f067c7c17c01a39b542f8f9bfaebceb95f1b76731c6bbbef
3
+ metadata.gz: f7d9334c86a350e7f344e3c1ba7a959d2dbc2b480ae74f0690dc2e8df45c26d6
4
+ data.tar.gz: 8697f7d54be0f9d86df14c6ccd97b3a52d73b48803a7dce76ce50ea9526549ef
5
5
  SHA512:
6
- metadata.gz: 27da9d8f7a378e0a2eff7b989fae29f8ab04b000715542f87906ecda76cae0b4dd31a82fd3fab4e3dd50ca64954efd64727ac3d4bdbda13d497d6c3cffcba7d4
7
- data.tar.gz: 846abdea65fb7711610a9a22a5b80c85527fc67c34af386924af7b4092bd32e41a0b1587eef4ff26a8d13c1949801dd4e4e18274ba99f8a7b5aea046b06a1658
6
+ metadata.gz: 0bfa60e3c71b9e08e633be4a019922cc6761097e39a9be382f7cbabf2fc2a49ff44f2d4def80a68fd1e6619858ac7c72b08c48a5f6a0b63215bcc947c23ad510
7
+ data.tar.gz: 2d5bfb8a037ca4e6aa243a375b81d503ffa2069f02c2fd1d3c5ae05a59b7b2f24eb8e602516a7e3f22b082367ade7d8e4423deb4fa17abd8a60a90c60b0a2f04
data/lib/html-native.rb CHANGED
@@ -17,6 +17,10 @@ module HTMLComponent
17
17
  end
18
18
  end
19
19
 
20
+ def doctype(type)
21
+ Builder.new("<!DOCTYPE #{type}>")
22
+ end
23
+
20
24
  # Creates a module that encompasses the given block in an HTMLComponent
21
25
  # context. This gives access to methods in the block as though the block was
22
26
  # declared as the `render` function in a module extending HTMLComponent
@@ -52,6 +56,6 @@ module HTMLComponent
52
56
  "#{k}=\"#{v}\"" # render this appropriately for numeric fields (might already)
53
57
  end
54
58
  end.join(" ")
55
- formatted.empty? ? "" : " " + formatted
59
+ formatted.prepend(" ") unless formatted.empty?
56
60
  end
57
61
  end
@@ -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,11 +32,49 @@ 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
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)
72
+ end
73
+ end
74
+ end
75
+
76
+ class String
77
+ def component
78
+ HTMLComponent::Builder.new(self)
26
79
  end
27
80
  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.0
4
+ version: 0.2.5
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-26 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: