-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split ComponentDoc into ComponentDoc and ComponentDocResolver #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module GovukPublishingComponents | ||
class ComponentDocResolver | ||
def get(id) | ||
component = fetch_component_doc(id) | ||
build(component) | ||
end | ||
|
||
def all | ||
fetch_component_docs.map { |component| build(component) } | ||
end | ||
|
||
private | ||
|
||
def build(component) | ||
fixtures = component[:fixtures].map { |id, data| | ||
ComponentFixture.new(id.to_s, data) | ||
} | ||
|
||
ComponentDoc.new(component[:id], | ||
component[:name], | ||
component[:description], | ||
component[:body], | ||
component[:accessibility_criteria], | ||
fixtures) | ||
end | ||
|
||
def fetch_component_docs | ||
doc_files = Rails.root.join("app", "views", "components", "docs", "*.yml") | ||
Dir[doc_files].sort.map { |file| parse_documentation(file) } | ||
end | ||
|
||
def fetch_component_doc(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So every time we try to access documentation we have to perform multiple file operations? Feels a little bit icky; could potentially memoise this stuff at a static level? Although given that this is all just internal code used by developers, maybe we don't have to worry about it too much. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to make it faster, and memoising makes sense. As this is predominantly a tool for developing components we need to balance that with the problem of too much caching hiding local changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me. Not sure we'd notice much of a performance change, anyway. |
||
file = Rails.root.join("app", "views", "components", "docs", "#{id}.yml") | ||
parse_documentation(file) | ||
end | ||
|
||
def parse_documentation(file) | ||
{ id: File.basename(file, ".yml") }.merge(YAML::load_file(file)).with_indifferent_access | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="component-call code-block" contenteditable> | ||
<pre class="language-ruby"><code><%= render 'components/<%= component_doc.id %>'<% if fixture.data? %>, <%= fixture.pretty_data %><% end %> %></code></pre> | ||
<pre class="language-ruby"><code><%= render '<%= @component_doc.partial_path %>'<% if fixture.data? %>, <%= fixture.pretty_data %><% end %> %></code></pre> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="component-guide-preview"> | ||
<%= render "components/#{@component_doc[:id]}", fixture.data %> | ||
<%= render @component_doc.partial_path, fixture.data %> | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have we moved away from a
Struct
? It looks like we've just wound up with more boilerplate code. To make it immutable? We could use something likeimmutable-struct
?