Class: ESM::Command::Arguments

Inherits:
Hash
  • Object
show all
Defined in:
lib/esm/command/arguments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil, templates: {}, values: {}) ⇒ Arguments

Returns a new instance of Arguments.



8
9
10
11
12
13
14
15
16
# File 'lib/esm/command/arguments.rb', line 8

def initialize(command = nil, templates: {}, values: {})
  @command_instance = command
  @templates = templates.symbolize_keys

  # Map the display name to the name itself
  @display_name_mapping = templates.values.each_with_object({}) { |a, hash| hash[a.display_name] = a.name }

  prepare(values.symbolize_keys)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Allows referencing arguments that may not exist on the current command, but does on others



89
90
91
# File 'lib/esm/command/arguments.rb', line 89

def method_missing(method_name, *arguments, &block)
  self[method_name.to_sym]
end

Instance Attribute Details

#command_instanceObject (readonly)

Returns the value of attribute command_instance.



6
7
8
# File 'lib/esm/command/arguments.rb', line 6

def command_instance
  @command_instance
end

#display_name_mappingObject (readonly)

Returns the value of attribute display_name_mapping.



6
7
8
# File 'lib/esm/command/arguments.rb', line 6

def display_name_mapping
  @display_name_mapping
end

#templatesObject (readonly)

Returns the value of attribute templates.



6
7
8
# File 'lib/esm/command/arguments.rb', line 6

def templates
  @templates
end

Instance Method Details

#inspectObject



66
67
68
69
70
71
72
# File 'lib/esm/command/arguments.rb', line 66

def inspect
  ESM::JSON.pretty_generate(
    values: self,
    command: command_instance&.command_name,
    templates: templates.map(&:to_h)
  )
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/esm/command/arguments.rb', line 93

def respond_to_missing?(method_name, _include_private = false)
  key?(method_name.to_sym) || super
end

#template(name) ⇒ Object



80
81
82
83
84
85
# File 'lib/esm/command/arguments.rb', line 80

def template(name)
  name = name.to_sym
  mapping = @display_name_mapping[name] || name

  templates[mapping]
end

#validate!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/esm/command/arguments.rb', line 18

def validate!
  # This collects all of the invalid arguments together and sends one message instead of breaking at the first invalid argument
  invalid_arguments =
    templates.filter_map do |(name, template)|
      # Apply pre-defined transformations and then validate the content
      self[name] = template.transform_and_validate!(self[name], command_instance)

      nil
    rescue ESM::Exception::InvalidArgument => e
      self[name] = nil
      e.data
    end

  # All the arguments are valid
  return if invalid_arguments.empty?

  embed =
    ESM::Embed.build do |e|
      help_documentation = invalid_arguments.join_map("\n\n", &:help_documentation)

      help_usage = ESM::Command.get(:help).usage(
        with_args: true,
        arguments: {with: command_instance.usage(with_slash: false, with_args: false)}
      )

      argument_word = "argument".pluralize(invalid_arguments.size)

      e.title = "**Invalid #{argument_word}**"
      e.description = <<~STRING
        ```#{command_instance.usage(with_args: true, use_placeholders: true, arguments: self)}```
        **Please read the following and correct any errors before trying again.**

        **Missing #{argument_word}**
        #{help_documentation}

        For more information, use the following command:
        ```#{help_usage}```
      STRING

      e.add_field(
        name: I18n.t("commands.help.command.examples"),
        value: command_instance.examples
      )
    end

  raise ESM::Exception::CheckFailure, embed
end

#with_templatesObject



74
75
76
77
78
# File 'lib/esm/command/arguments.rb', line 74

def with_templates
  each_with_object({}) do |(name, value), hash|
    hash[name] = [templates[name], value]
  end
end