Class: Lookbook::SourceInspector

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/lookbook/source_inspector.rb

Constant Summary

Constants included from Utils

Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code_object, eval_scope: nil) ⇒ SourceInspector

Returns a new instance of SourceInspector.



8
9
10
11
# File 'lib/lookbook/source_inspector.rb', line 8

def initialize(code_object, eval_scope: nil)
  @code_object = code_object
  @eval_scope = eval_scope
end

Instance Attribute Details

#code_objectObject (readonly)

Returns the value of attribute code_object.



5
6
7
# File 'lib/lookbook/source_inspector.rb', line 5

def code_object
  @code_object
end

Instance Method Details

#componentsObject



37
38
39
# File 'lib/lookbook/source_inspector.rb', line 37

def components
  @components ||= Array(code_object.tags(:component)).map(&:value)
end

#display_optionsObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lookbook/source_inspector.rb', line 46

def display_options
  return @display_options unless @display_options.nil?

  # Dynamic params set at the entity level are
  # not (yet?) supported so filter them out.
  display_tags = Array(code_object.tags(:display)).select do |tag|
    !(tag.value.is_a?(Array) || tag.value.is_a?(Hash))
  end

  display_tags.map { |tag| [tag.key.to_sym, tag.value] }.to_h
end

#groupObject



29
30
31
# File 'lib/lookbook/source_inspector.rb', line 29

def group
  @group ||= code_object.group
end

#hidden?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lookbook/source_inspector.rb', line 13

def hidden?
  @hidden ||= tag_value(:hidden) || false
end

#idObject



17
18
19
# File 'lib/lookbook/source_inspector.rb', line 17

def id
  @id ||= tag_value(:id)
end

#labelObject



21
22
23
# File 'lib/lookbook/source_inspector.rb', line 21

def label
  @label ||= tag_value(:label)
end

#logical_pathObject



41
42
43
44
# File 'lib/lookbook/source_inspector.rb', line 41

def logical_path
  path = tag_value(:logical_path)
  path&.delete_prefix("/")&.delete_suffix("/")
end

#methodsObject



58
59
60
# File 'lib/lookbook/source_inspector.rb', line 58

def methods
  @methods ||= code_object.meths
end

#notesObject



25
26
27
# File 'lib/lookbook/source_inspector.rb', line 25

def notes
  @notes ||= code_object.docstring&.to_s&.strip
end

#positionObject



33
34
35
# File 'lib/lookbook/source_inspector.rb', line 33

def position
  @position ||= tag_value(:position) || 10000
end

#tag(name = nil) ⇒ Object



66
67
68
# File 'lib/lookbook/source_inspector.rb', line 66

def tag(name = nil)
  tags(name).first
end

#tags(name = nil) ⇒ Object



62
63
64
# File 'lib/lookbook/source_inspector.rb', line 62

def tags(name = nil)
  code_object.tags(name)
end