Skip to content

Commit

Permalink
refactor(entities): rename position -> priority for better clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Dec 6, 2022
1 parent 145f2ac commit 0ad25a0
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 53 deletions.
1 change: 0 additions & 1 deletion app/components/lookbook/nav/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@
</div>
<% end %>
</div>

<% end %>
2 changes: 1 addition & 1 deletion app/components/lookbook/nav/item/component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Lookbook
class Nav::Item::Component < Lookbook::BaseComponent
delegate :label, :depth, to: :node
delegate :label, :depth, :priority, to: :node

attr_reader :node, :nav_id

Expand Down
4 changes: 2 additions & 2 deletions config/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ shared:
label: Display
opts: {}

position:
label: Position
priority:
label: Priority
opts: {}

id:
Expand Down
18 changes: 9 additions & 9 deletions lib/lookbook/entities/concerns/navigable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ def visible?
!hidden?
end

def position
return @_position if @_position
def priority
return @_priority if @_priority

pos = if @position_prefixes && respond_to?(:file_name)
PositionPrefixParser.call(file_name).first || default_position
pos = if @priority_prefixes && respond_to?(:file_name)
PriorityPrefixParser.call(file_name).first || fetch_config(:priority, default_priority)
else
fetch_config(:position, default_position)
fetch_config(:priority, default_priority)
end

@_position ||= pos.to_i
@_priority ||= pos.to_i
end

def depth
path.split("/").size
end

def default_position
@default_position || 10000
def default_priority
@default_priority || 10000
end

def <=>(other)
if respond_to?(:sort_handler, true)
sort_handler(other)
else
[position, label] <=> [other.position, other.label]
[priority, label] <=> [other.priority, other.label]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lookbook/entities/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(file_path)
@base_directories = Engine.page_paths
@lookup_path = PathUtils.to_lookup_path(relative_file_path)
@frontmatter, @content = FrontmatterExtractor.call(file_contents)
@position_prefixes = true
@priority_prefixes = true
@sections = []
end

Expand Down Expand Up @@ -51,7 +51,7 @@ def url_path

def add_section(section)
@sections << section
@sections.sort_by! { |section| [section.position, section.label] }
@sections.sort_by! { |section| [section.priority, section.label] }
end

def method_missing(method_name, *args, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/lookbook/entities/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def load_examples
def example_entities
public_methods = preview_class.public_instance_methods(false)
method_objects = code_object.meths.select { |m| public_methods.include?(m.name) }
method_objects.map.with_index { |code_object, i| PreviewExample.new(code_object, self, position: i) }
method_objects.map.with_index { |code_object, i| PreviewExample.new(code_object, self, priority: i) }
end
end
end
6 changes: 3 additions & 3 deletions lib/lookbook/entities/preview_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class PreviewExample < Entity

attr_reader :preview

def initialize(code_object, preview, position: nil)
def initialize(code_object, preview, priority: nil)
@code_object = code_object
@preview = preview
@default_position = position
@default_priority = priority
@lookup_path = "#{parent.lookup_path}/#{name}"
end

Expand Down Expand Up @@ -70,7 +70,7 @@ def sort_handler(other_entity)
if Lookbook.config.sort_examples
label <=> other_entity.label
else
[position, label] <=> [other_entity.position, other_entity.label]
[priority, label] <=> [other_entity.priority, other_entity.label]
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/lookbook/services/entities/entity_tree_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def call
current_node = root_node
path_segments = parse_segments(entity.logical_path)
path_segments.each.with_index(1) do |segment, i|
name, position_prefix = segment
name, priority_prefix = segment
content = entity if entity.depth == i # entities are always on the leaf nodes

current_node.add_child(name, content, position: position_prefix) unless current_node.has_child?(name)
current_node.add_child(name, content, priority: priority_prefix) unless current_node.has_child?(name)
current_node = current_node.get_child(name)

if content && content.type == :preview
Expand All @@ -32,8 +32,8 @@ def call
def parse_segments(path)
path.split("/").map do |segment|
unless segment.start_with?(".")
position, name = PositionPrefixParser.call(segment)
[name, position || 10000]
priority, name = PriorityPrefixParser.call(segment)
[name, priority || 10000]
end
end.compact
end
Expand Down
16 changes: 0 additions & 16 deletions lib/lookbook/services/position_prefix_parser.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/lookbook/services/priority_prefix_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Lookbook
class PriorityPrefixParser < Service
PRIORITY_PREFIX_REGEX = /^(\d+?)[-_]/

attr_reader :input

def initialize(input)
@input = String(input)
end

def call
matches = input.match(PRIORITY_PREFIX_REGEX)
matches ? [matches[1].to_i, input.gsub(PRIORITY_PREFIX_REGEX, "")] : [nil, input]
end
end
end
14 changes: 7 additions & 7 deletions lib/lookbook/support/tree_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class TreeNode
attr_accessor :path, :content
attr_reader :children

def initialize(path = nil, content = nil, position: 10000)
def initialize(path = nil, content = nil, priority: 10000)
@path = path.to_s
@content = content
@position = position
@priority = priority
@children = []
end

Expand All @@ -27,8 +27,8 @@ def label
content_value(:label, name.titleize)
end

def position
content_value(:position, @position)
def priority
content_value(:priority, @priority)
end

def type
Expand All @@ -39,8 +39,8 @@ def depth
path.split("/").size
end

def add_child(name, content = nil, position: 10000)
children << TreeNode.new("#{path}/#{name}", content, position: position)
def add_child(name, content = nil, priority: 10000)
children << TreeNode.new("#{path}/#{name}", content, priority: priority)
end

def has_child?(name)
Expand Down Expand Up @@ -69,7 +69,7 @@ def <=>(other)
if content?
content <=> (other.content? ? other.content : other)
else
[position, label] <=> [other.position, other.label]
[priority, label] <=> [other.priority, other.label]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lookbook/support/utils/path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def to_lookup_path(file_path)

segments = [*directory_path&.split("/"), file_name].compact
stripped_segments = segments.map! do |segment|
PositionPrefixParser.call(segment).last.tr("-", "_")
PriorityPrefixParser.call(segment).last.tr("-", "_")
end

to_path(stripped_segments)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Lookbook
class PositionTag < YardTag
DEFAULT_POSITION = 100000
class PriorityTag < YardTag
DEFAULT_PRIORITY = 100000

def value
if text.present?
int = text.to_i
(int == 0) ? DEFAULT_POSITION : int
(int == 0) ? DEFAULT_PRIORITY : int
else
DEFAULT_POSITION
DEFAULT_PRIORITY
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.describe Lookbook::PositionTag do
RSpec.describe Lookbook::PriorityTag do
it "extends Lookbook::YardTag" do
expect(described_class).to be < Lookbook::YardTag
end
Expand All @@ -16,7 +16,7 @@

it "returns the default value if the string cannot be converted into an integer" do
tag = described_class.new("foo")
expect(tag.value).to eq Lookbook::PositionTag::DEFAULT_POSITION
expect(tag.value).to eq Lookbook::PositionTag::DEFAULT_PRIORITY
end
end
end

0 comments on commit 0ad25a0

Please sign in to comment.