Skip to content

Commit b1331ca

Browse files
committed
Add model spec for syntax highlight
1 parent 942cca5 commit b1331ca

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

app/models/settings/syntax_highlight.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,34 @@ def self.default
66
end
77

88
def self.find(name)
9-
attrs = curated_data.find { |attrs| attrs[:name] == name }
10-
if attrs.present?
11-
new(**attrs)
12-
else
13-
raise ActiveRecord::RecordNotFound, "Couldn't find SyntaxHighlight with name '#{name}'"
14-
end
9+
instance = curated.find { |obj| obj.name == name }
10+
instance.presence or raise ActiveRecord::RecordNotFound, "Couldn't find SyntaxHighlight with name '#{name}'"
1511
end
1612

13+
# This method lazily reifies and memoizes curated SyntaxHighlight instances. I may
14+
# want to consider a more sophisticated caching strategy.
1715
def self.curated
18-
curated_data.map { |attrs| new(**attrs) }
16+
@curated ||= curated_data.map { |attrs| new(**attrs) }
17+
end
18+
19+
def self.all
20+
curated
1921
end
2022

2123
def self.curated_data
2224
@curated_data ||= YAML.load_file(Rails.root.join("config", "syntax_highlights.yml"))
2325
end
2426

25-
attr_accessor :name, :mode
27+
attr_accessor :name, :mode, :background_color
2628

27-
def initialize(name:, mode:, **)
29+
def initialize(name:, mode:, background_color:, **)
2830
@name = name
2931
@mode = mode
32+
@background_color = background_color
3033
end
3134

35+
def id = name
36+
3237
def display_name
3338
name.titleize
3439
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Settings::SyntaxHighlight, type: :request do
4+
describe "#==" do
5+
it { expect(described_class.find("dracula")).to eq(described_class.find("dracula")) }
6+
it { expect(described_class.find("darcula")).not_to eq(described_class.find("dracula")) }
7+
it { expect(described_class.find("darcula")).not_to eq("darcula") }
8+
end
9+
10+
describe ".curated" do
11+
it "represents the files in the curated directory" do
12+
assets = Dir.glob(Rails.root.join("app", "assets", "stylesheets", "pygments", "*.css"))
13+
14+
expect(described_class.curated.map { |s| s.path.to_s }).to match_array(assets),
15+
"Expected the curated syntax highlights to match CSS files in pygments css directory. " \
16+
"You may need to run `rake syntax_highlights:seed` to update the curated syntax highlights."
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)