Skip to content

Commit

Permalink
Ruby is as ruby does
Browse files Browse the repository at this point in the history
I like relying on `to_s` for the implicit generation of content. Now
every `eval-ruby` can be a bespoke snowflake that only gets passed data
if it absolutely needs to. Looking up data in `Build.data` is fine by
me. It might not be the most testable thing...but I haven't written any
tests for these yet, so YAGNI.
  • Loading branch information
danott committed Jan 15, 2024
1 parent 7817567 commit 37d5fee
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 57 deletions.
18 changes: 18 additions & 0 deletions lib/includable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Includable
PAGE_DEFAULTS = <<~HTML
<include-in-header>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="The personal website of Dan Ott.">
<meta name="author" content="Dan Ott">
<link rel="stylesheet" href="/stylesheets/style.css">
<link rel="stylesheet" href="/stylesheets/pygments-default.css" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="/stylesheets/pygments-monokai.css" media="(prefers-color-scheme: dark)">
<link rel="alternate" type="application/rss+xml" href="/feed.xml" title="Dan Ott's RSS Feed">
<script src="/javascripts/script.js"></script>
</include-in-header>
HTML
end
6 changes: 3 additions & 3 deletions lib/includable/data_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module Includable
class DataDump
attr_reader :everything

def initialize(string:, data:)
def initialize
@everything =
data.all.merge(
Build.data.all.merge(
posts_tagged_climbing: Build.data.posts_tagged("climbing")
)
end

def render
def to_s
%Q[<pre timestamp="#{Time.now.to_i}">#{CGI.escapeHTML(JSON.pretty_generate(everything))}</pre>]
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/includable/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ class Index

Post = Struct.new(:title, :url, :date)

def initialize(string:, data:)
def initialize
@posts =
data
Build
.data
.posts
.reject { |p| p.fetch("tags", []).include?("noIndex") }
.map do |p|
Post.new(p.fetch("title"), p.fetch("url"), p.fetch("date"))
end
end

def render
def to_s
ERB.new(template).result_with_hash({ "posts" => posts })
end

Expand Down
25 changes: 0 additions & 25 deletions lib/includable/page_defaults.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/includable/post_footer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Includable
class PostFooter
attr_reader :data

def initialize(string:, data:)
def initialize(data:)
@data = data
end

def render
def to_s
ERB.new(template).result_with_hash(date: data.current_page.fetch("date"))
end

Expand Down
7 changes: 4 additions & 3 deletions lib/includable/six_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ class SixWords

Entry = Struct.new(:date, :words)

def initialize(string:, data:)
def initialize
@entries =
data
Build
.data
.sets
.fetch("six_words")
.map { |e| Entry.new(e.fetch("date"), e.fetch("words")) }
.sort_by(&:date)
.reverse
end

def render
def to_s
ERB.new(template).result_with_hash({ entries: entries })
end

Expand Down
13 changes: 0 additions & 13 deletions lib/includable/text.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/magic_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def initialize(path, original_content)
def content
if path.match(%r{site/\d\d\d\d/})
<<~HTML
<eval-ruby>Includable::PageDefaults.new(string: nil, data: nil).render</eval-ruby>
<eval-ruby>Includable::PAGE_DEFAULTS</eval-ruby>
#{original_content}
<eval-ruby>Includable::PostFooter.new(string: nil, data: self.data).render</eval-ruby>
<eval-ruby>Includable::PostFooter.new(data: self.data)</eval-ruby>
HTML
else
original_content
Expand Down
2 changes: 1 addition & 1 deletion site/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
This is my page for debugging my build

<eval-ruby>
Includable::DataDump.new(string: nil, data: Build.data).render
Includable::DataDump.new
</eval-ruby>
4 changes: 2 additions & 2 deletions site/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<eval-ruby>Includable::PageDefaults.new(string: nil, data: nil).render</eval-ruby>
<eval-ruby>Includable::PAGE_DEFAULTS</eval-ruby>

# 👨‍💻 danott.website

Expand All @@ -10,5 +10,5 @@ You can subscribe to [Dan Ott's Website RSS Feed](/feed.xml).
Here's a list of posts I've written in reverse chronological order.

<eval-ruby>
Includable::Index.new(string: nil, data: Build.data).render
Includable::Index.new
</eval-ruby>
4 changes: 2 additions & 2 deletions site/six-words.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<eval-ruby>Includable::PageDefaults.new(string: nil, data: nil).render</eval-ruby>
<eval-ruby>Includable::PAGE_DEFAULTS</eval-ruby>

# Six Words

Expand All @@ -10,5 +10,5 @@ I've decided to try it again.
I'll share when I feel compelled.

<eval-ruby>
Includable::SixWords.new(string: nil, data: Build.data).render
Includable::SixWords.new
</eval-ruby>
2 changes: 1 addition & 1 deletion test/magic_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def test_path_with_magic
refute_equal content, magic_content
assert_operator magic_content,
:starts_with?,
"<eval-ruby>Includable::PageDefaults.new(string: nil, data: nil).render</eval-ruby>"
"<eval-ruby>Includable::PAGE_DEFAULTS</eval-ruby>"
end
end

0 comments on commit 37d5fee

Please sign in to comment.