Class: ERBLint::Linters::ArgumentMappers::Base
- Inherits:
-
Object
- Object
- ERBLint::Linters::ArgumentMappers::Base
show all
- Defined in:
- lib/primer/view_components/linters/argument_mappers/base.rb
Overview
Provides the base interface to implement an ‘ArgumentMapper`. Override attribute_to_args in a child class to customize its mapping behavior.
Constant Summary
collapse
- DEFAULT_TAG =
nil
- ATTRIBUTES =
[].freeze
Instance Method Summary
collapse
Constructor Details
#initialize(tag) ⇒ Base
Returns a new instance of Base.
19
20
21
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 19
def initialize(tag)
@tag = tag
end
|
Instance Method Details
#attribute_to_args(attribute) ⇒ Object
48
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 48
def attribute_to_args(attribute); end
|
#classes_to_args(classes) ⇒ classes: Array, ...
Override this with your component’s mappings, it should return a hash with the component’s arguments, including a ‘classes` key that will contain all classes that the mapper couldn’t handle.
79
80
81
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 79
def classes_to_args(classes)
{ classes: classes }
end
|
#map_classes(classes_node) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 50
def map_classes(classes_node)
erb_helper.raise_if_erb_block(classes_node)
system_arguments = system_arguments_to_args(classes_node.value)
args = classes_to_args(system_arguments[:classes]&.split || [])
invalid_classes = args[:classes].select { |class_name| ::Primer::Classify::Validation.invalid?(class_name) }
raise ConversionError, "Cannot convert #{'class'.pluralize(invalid_classes.size)} #{invalid_classes.join(',')}" if invalid_classes.present?
res = {
**args.except(:classes),
**system_arguments.except(:classes)
}
if args[:classes].present?
res = {
**res,
classes: args[:classes].join(" ").to_json
}
end
res
end
|
#system_arguments_to_args(classes) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 83
def system_arguments_to_args(classes)
system_arguments = ::Primer::Classify::Utilities.classes_to_hash(classes)
system_arguments.transform_values do |v|
v.is_a?(Symbol) ? ":#{v}" : v
end
end
|
#to_args ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 27
def to_args
args = {}
args[:tag] = ":#{@tag.name}" unless self.class::DEFAULT_TAG.nil? || @tag.name == self.class::DEFAULT_TAG
@tag.attributes.each do |attribute|
attr_name = attribute.name
if self.class::ATTRIBUTES.include?(attr_name)
args.merge!(attribute_to_args(attribute))
elsif attr_name == "class"
args.merge!(map_classes(attribute))
else
args.merge!(SystemArguments.new(attribute).to_args)
end
end
args
end
|
#to_s ⇒ Object
23
24
25
|
# File 'lib/primer/view_components/linters/argument_mappers/base.rb', line 23
def to_s
to_args.map { |k, v| "#{k}: #{v}" }.join(", ")
end
|