8000 Conditionally memoize certain private methods in EditLinkTag (#163) · jekyll/github-metadata@bd3e22e · GitHub
[go: up one dir, main page]

Skip to content

Commit bd3e22e

Browse files
ashmarolijekyllbot
authored andcommitted
Conditionally memoize certain private methods in EditLinkTag (#163)
Merge pull request 163
1 parent 8dcdd2f commit bd3e22e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

appveyor.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ build: off
88

99
environment:
1010
matrix:
11-
- RUBY_FOLDER_VER: "25-x64"
11+
- RUBY_FOLDER_VER: "26"
12+
JEKYLL_VERSION: "3.7.0"
13+
- RUBY_FOLDER_VER: "26"
14+
JEKYLL_VERSION: "4.0.0.pre.alpha1"
15+
- RUBY_FOLDER_VER: "26"
1216
- RUBY_FOLDER_VER: "25"
1317
- RUBY_FOLDER_VER: "24"
1418
- RUBY_FOLDER_VER: "23"
1519

1620
install:
17-
- SET PATH=C:\Ruby%RUBY_FOLDER_VER%\bin;%PATH%
21+
- SET PATH=C:\Ruby%RUBY_FOLDER_VER%-x64\bin;%PATH%
1822
- bundle install --retry 5 --jobs=%NUMBER_OF_PROCESSORS% --clean --path vendor\bundle
1923

2024
test_script:

lib/jekyll-github-metadata/edit-link-tag.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,40 @@ def uri
6969
end
7070

7171
def parts
72-
@parts ||= [repository_url, "edit/", branch, source_path, page_path]
72+
memoize_conditionally { [repository_url, "edit/", branch, source_path, page_path] }
7373
end
7474

7575
def parts_normalized
76-
@parts_normalized ||= parts.map.with_index do |part, index|
77-
part = remove_leading_slash(part.to_s)
78-
part = ensure_trailing_slash(part) unless index == parts.length - 1
79-
ensure_not_just_a_slash(part)
76+
memoize_conditionally do
77+
parts.map.with_index do |part, index|
78+
part = remove_leading_slash(part.to_s)
79+
part = ensure_trailing_slash(part) unless index == parts.length - 1
80+
ensure_not_just_a_slash(part)
81+
end
8082
end
8183
end
8284

8385
def page
84-
@page ||= context.registers[:page]
86+
memoize_conditionally { context.registers[:page] }
87+
end
88+
89+
# Utility function for compatibility with Jekyll 4.0
90+
def memoize_conditionally
91+
if renderer_cached?
92+
yield
93+
else
94+
dispatcher = "@#{caller_locations(1..1).first.label}".to_sym
95+
if instance_variable_defined?(dispatcher)
96+
instance_variable_get(dispatcher)
97+
else
98+
instance_variable_set(dispatcher, yield)
99+
end
100+
end
101+
end
102+
103+
# Utility function to detect Jekyll 4.0
104+
def renderer_cached?
105+
@renderer_cached ||= context.registers[:site].liquid_renderer.respond_to?(:cache)
85106
end
86107

87108
def site

spec/edit_link_tag_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@
147147
expect(rendered).to eql("#{repository_url}/edit/#{branch}/page.md")
148148
end
149149

150+
context "is conditionally context-aware" do
151+
it "returns correct URL" do
152+
# create a static instance. `subject` creates a different instance on each call.
153+
tag = described_class.parse(tag_name, markup, tokenizer, parse_context)
154+
155+
expect(tag.render(render_context)).to eql("#{repository_url}/edit/#{branch< 80D7 /span>}/page.md")
156+
filename = site.liquid_renderer.respond_to?(:cache) ? "dummy_page.md" : "page.md"
157+
new_context = make_context(
158+
:page => make_page("path" => "dummy_page.md"),
159+
:site => site
160+
)
161+
expect(tag.render(new_context)).to eql("#{repository_url}/edit/#{branch}/#{filename}")
162+
end
163+
end
164+
150165
context "when passed markup" do
151166
let(:markup) { '"Improve this page"' }
152167

0 commit comments

Comments
 (0)
0