-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
class MagicElementHydrator | ||
attr_reader :content | ||
attr_reader :data | ||
|
||
def initialize(content:, data:) | ||
@content = content | ||
@data = data | ||
end | ||
|
||
def hydrate | ||
next_magic_element.present? ? self.next.hydrate : self | ||
end | ||
|
||
def next_magic_element | ||
IncludeInHeader.recognize(content) || EvalRuby.recognize(content, self) | ||
end | ||
|
||
def next | ||
fail "No more hydrating to do!" if next_magic_element.nil? | ||
return self.class.new(content: next_magic_element.result, data: data) | ||
end | ||
|
||
private | ||
|
||
class IncludeInHeader | ||
attr_reader :content | ||
attr_reader :match | ||
|
||
def self.recognize(content) | ||
match = content.match(%r{<include-in-header>(.*?)</include-in-header>}m) | ||
new(content:, match:) if match | ||
end | ||
|
||
def initialize(content:, match:) | ||
@content = content | ||
@match = match | ||
end | ||
|
||
def end_of_head | ||
content.index("</head>") | ||
end | ||
|
||
def entire_magic_element | ||
match[0] | ||
end | ||
|
||
def children | ||
match[1] | ||
end | ||
|
||
def result | ||
content.sub(entire_magic_element, "").insert(end_of_head, children) | ||
end | ||
end | ||
|
||
class EvalRuby | ||
attr_reader :content | ||
attr_reader :hydrator | ||
attr_reader :match | ||
|
||
def self.recognize(content, hydrator) | ||
match = content.match(%r{<eval-ruby>(.*?)</eval-ruby>}m) | ||
new(content:, match:, hydrator:) if match | ||
end | ||
|
||
def initialize(content:, match:, hydrator:) | ||
@content = content | ||
@match = match | ||
@hydrator = hydrator | ||
end | ||
|
||
def entire_magic_element | ||
match[0] | ||
end | ||
|
||
def ruby_code | ||
match[1] | ||
end | ||
|
||
# Eval in the context of hydrator to have access to data | ||
def replacement | ||
hydrator.instance_eval(ruby_code).to_s | ||
end | ||
|
||
def result | ||
position = content.index(entire_magic_element) | ||
content.sub(entire_magic_element, "").insert(position, replacement) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class MagicElementHydratorTest < Minitest::Test | ||
CONTENT = <<~HTML | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<h1>Test</h1> | ||
<h2><eval-ruby>2 + 2</eval-ruby></h2> | ||
<h2><eval-ruby>self.data.fetch("Fetch this!")</eval-ruby></h2> | ||
<include-in-header> | ||
<meta name="include-in-header"> | ||
<eval-ruby> | ||
%Q[<meta name="\#{data.fetch("Fetch this!")}">] | ||
</eval-ruby> | ||
</include-in-header> | ||
</body> | ||
</html> | ||
HTML | ||
|
||
def test_everything | ||
hydrator = | ||
MagicElementHydrator.new( | ||
content: CONTENT, | ||
data: { | ||
"Fetch this!" => "Return that!" | ||
} | ||
) | ||
result = hydrator.hydrate | ||
|
||
refute_includes result.content, "Fetch this!" | ||
refute_includes result.content, "2 + 2" | ||
refute_includes result.content, "<eval-ruby>" | ||
refute_includes result.content, "</eval-ruby>" | ||
refute_includes result.content, "<include-in-header>" | ||
refute_includes result.content, "</include-in-header>" | ||
|
||
assert_body_includes result.content, "<h2>4</h2>" | ||
assert_body_includes result.content, "<h2>Return that!</h2>" | ||
|
||
assert_head_includes result.content, '<meta name="include-in-header">' | ||
assert_head_includes result.content, '<meta name="Return that!">' | ||
end | ||
|
||
def assert_body_includes(haystack, needle) | ||
body = haystack.split("<body>").last.split("</body>").first | ||
assert_includes body, needle | ||
end | ||
|
||
def assert_head_includes(haystack, needle) | ||
head = haystack.split("<head>").last.split("</head>").first | ||
assert_includes head, needle | ||
end | ||
end |