Class: Lookbook::SourceInspector
- Inherits:
-
Object
- Object
- Lookbook::SourceInspector
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
Returns a new instance of SourceInspector.
8
9
10
|
# File 'lib/lookbook/source_inspector.rb', line 8
def initialize(code_object)
@code_object = code_object
end
|
Instance Attribute Details
#code_object ⇒ Object
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
#components ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/lookbook/source_inspector.rb', line 44
def components
@components ||= if code_object&.tags(:component).present?
code_object.tags(:component).map do |component|
component.text.constantize
end
else
[]
end
end
|
#display_params ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/lookbook/source_inspector.rb', line 54
def display_params
return @display_params unless @display_params.nil?
@display_params = {}.with_indifferent_access
if code_object&.tags(:display).present?
code_object.tags(:display).each do |tag|
parts = tag.text.strip.match(/^([^\s]*)\s?(.*)$/)
if parts.present?
begin
display_params[parts[1]] = YAML.safe_load(parts[2] || "~")
rescue SyntaxError => err
Lookbook.logger.error("\nš [Lookbook] Invalid JSON in @display tag.\nš [Lookbook] (#{err})\n")
end
end
end
end
@display_params
end
|
#group ⇒ Object
36
37
38
|
# File 'lib/lookbook/source_inspector.rb', line 36
def group
@group ||= code_object&.group
end
|
#hidden? ⇒ Boolean
12
13
14
15
16
17
18
|
# File 'lib/lookbook/source_inspector.rb', line 12
def hidden?
@hidden ||= if code_object&.tag(:hidden)
code_object.tag(:hidden).text.strip != "false"
else
false
end
end
|
#id ⇒ Object
20
21
22
23
24
|
# File 'lib/lookbook/source_inspector.rb', line 20
def id
@id ||= if code_object&.tag(:id)&.text&.present?
generate_id(code_object&.tag(:id)&.text)
end
end
|
#label ⇒ Object
26
27
28
|
# File 'lib/lookbook/source_inspector.rb', line 26
def label
@label ||= code_object&.tag(:label)&.text
end
|
#methods ⇒ Object
82
83
84
|
# File 'lib/lookbook/source_inspector.rb', line 82
def methods
@methods ||= code_object&.meths
end
|
#notes ⇒ Object
30
31
32
33
34
|
# File 'lib/lookbook/source_inspector.rb', line 30
def notes
@notes ||= if code_object&.docstring
code_object.docstring.to_s.strip
end
end
|
#parameter_defaults ⇒ Object
72
73
74
|
# File 'lib/lookbook/source_inspector.rb', line 72
def parameter_defaults
@param_defaults ||= code_object&.parameters&.map { |str| Params.parse_method_param_str(str) }&.compact&.to_h
end
|
#params ⇒ Object
76
77
78
79
80
|
# File 'lib/lookbook/source_inspector.rb', line 76
def params
code_object&.tags("param")&.map do |param|
Lookbook::Params.build_param(param, default: parameter_defaults[param.name])
end
end
|
#position ⇒ Object
40
41
42
|
# File 'lib/lookbook/source_inspector.rb', line 40
def position
@position ||= code_object&.tag(:position)&.text&.to_i || 10000
end
|
#tag(name = nil) ⇒ Object
91
92
93
|
# File 'lib/lookbook/source_inspector.rb', line 91
def tag(name = nil)
tags(name).first
end
|
86
87
88
89
|
# File 'lib/lookbook/source_inspector.rb', line 86
def tags(name = nil)
tag_objects = code_object&.tags(name).presence || []
Lookbook::Tags.process_tags(tag_objects)
end
|