Class: ERBLint::Linters::ArgumentMappers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yattho/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.

Direct Known Subclasses

Button, ClipboardCopy, CloseButton, Flash, Label

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/yattho/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/yattho/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.

Returns:

  • (classes: Array, ...)


82
83
84
# File 'lib/yattho/view_components/linters/argument_mappers/base.rb', line 82

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
75
76
77
# File 'lib/yattho/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| Yattho::Classify::Validation.invalid?(class_name) }

  if invalid_classes.present?
    raise ConversionError,
          "Cannot convert #{'class'.pluralize(invalid_classes.size)} #{invalid_classes.join(',')}"
  end

  # Using splat to order the arguments in Component's args -> System Args -> custom classes
  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



86
87
88
89
90
91
92
93
# File 'lib/yattho/view_components/linters/argument_mappers/base.rb', line 86

def system_arguments_to_args(classes)
  system_arguments = Yattho::Classify::Utilities.classes_to_hash(classes)

  # need to transform symbols to strings with leading `:`
  system_arguments.transform_values do |v|
    v.is_a?(Symbol) ? ":#{v}" : v
  end
end

#to_argsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yattho/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
      # Assume the attribute is a system argument.
      args.merge!(SystemArguments.new(attribute).to_args)
    end
  end

  args
end

#to_sObject



23
24
25
# File 'lib/yattho/view_components/linters/argument_mappers/base.rb', line 23

def to_s
  to_args.map { |k, v| "#{k}: #{v}" }.join(", ")
end