html-native 0.2.2 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/html-native.rb +5 -0
- data/lib/html-native/builder.rb +52 -7
- data/lib/html-native/logic.rb +38 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a2cde975232defdab37e17881582541235abf7448b690b3a2e7078623961e99
|
4
|
+
data.tar.gz: 140e37d1bb7e039a18e159565c35912d6a54cead980872fd9764ad427f4175cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20ebdb417720ef51aceba2c24878861966eba922981201031be892fb6b86390319e57586339d968fc9d77a3237955b8aa580eba3d53b8d1e26cd6446200450d4
|
7
|
+
data.tar.gz: 4b3d8f0fd9fab698b6fb85d8f270fdc015368497f330ac13c4c3124e499399a313540f159d75672d1b4374335532802d94fdac79677e88544e5325391e939b7c
|
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
|
data/lib/html-native/builder.rb
CHANGED
@@ -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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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,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
|
-
@
|
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_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
|
28
73
|
end
|
29
74
|
end
|
30
75
|
|
@@ -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
|
+
HTMLComponent::Builder.new(self)
|
25
|
+
else
|
26
|
+
HTMLComponent::Builder.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
define_method(:unless) do |bool|
|
31
|
+
# BAd form, purely for symmetry
|
32
|
+
unless bool
|
33
|
+
HTMLComponent::Builder.new(self)
|
34
|
+
else
|
35
|
+
HTMLComponent::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.
|
4
|
+
version: 0.3.1
|
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
|
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
|