lib/lookbook/preview_example.rb in lookbook-0.6.1 vs lib/lookbook/preview_example.rb in lookbook-0.7.0

- old
+ new

@@ -1,40 +1,36 @@ module Lookbook class PreviewExample - include Taggable + include Utils - attr_reader :name + attr_reader :name, :preview + delegate :params, :position, :group, :notes, :hidden?, to: :@example_inspector def initialize(name, preview) @name = name @preview = preview + @example_inspector = CodeInspector.new("#{@preview.name}##{name}") end def id - path.underscore.tr("/", "-").tr("_", "-") + generate_id(@preview.id, name) end def path - "#{@preview.lookbook_path}/#{name}" + "#{@preview.path}/#{name}" end def label - lookbook_label.presence || name.titleize + @example_inspector.label.presence || name.titleize end def display_params - @preview.display_params.merge(lookbook_display_params) + @preview.display_params.merge(@example_inspector.display_params) end - def params - @params || code_object&.tags("param")&.map do |param| - Lookbook::Params.build_param(param, parameter_defaults[param.name]) - end - end - def method_source - code_object.source.split("\n")[1..-2].join("\n").strip_heredoc + @example_inspector.source.split("\n")[1..-2].join("\n").strip_heredoc end def source_lang Lookbook::Lang.find(:ruby) end @@ -50,36 +46,38 @@ def type :example end def matchers - [@preview.label, label].map { |m| m.gsub(/\s/, "").downcase } + normalize_matchers(@preview.label, label) end def hierarchy_depth - @preview.lookbook_hierarchy_depth + 1 + @preview.hierarchy_depth + 1 end - private + protected - def parameter_defaults - @parameter_defaults || code_object&.parameters&.map do |param_str| - Lookbook::Params.parse_method_param_str(param_str) - end&.compact&.to_h - end - - def taggable_object_path - "#{@preview.name}##{name}" - end - def full_template_path(template_path) base_path = Array(Lookbook.config.preview_paths).detect do |p| Dir["#{p}/#{template_path}.html.*"].first end Pathname.new(Dir["#{base_path}/#{template_path}.html.*"].first) end - alias_method :group, :lookbook_group - alias_method :notes, :lookbook_notes - alias_method :hidden?, :lookbook_hidden? + class << self + def all + Preview.all.map { |preview| preview.examples }.flatten + end + + def find(path) + all.find { |p| p.lookup_path == path } + end + + def exists?(path) + !!find(path) + end + end + + alias_method :lookup_path, :path end end