Class: Foobara::CommandConnectors::ShCliConnector::InputsParser::Option

Inherits:
Object
  • Object
show all
Defined in:
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option.rb,
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option/flag.rb,
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option/model.rb,
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option/on_flag.rb,
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option/off_flag.rb,
foobara-sh-cli-connector-1.1.3/src/sh_cli_connector/inputs_parser/option/attributes.rb

Direct Known Subclasses

Attributes, Flag, Model

Defined Under Namespace

Classes: Attributes, Flag, Model, OffFlag, OnFlag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs:) ⇒ Option

Returns a new instance of Option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 41

def initialize(
  attribute_name:,
  attribute_type:,
  prefix:,
  is_required:,
  default:,
  always_prefix_inputs:
)
  self.attribute_type = attribute_type
  self.attribute_name = attribute_name
  self.prefix = prefix
  self.is_required = is_required
  self.default = default
  self.always_prefix_inputs = always_prefix_inputs

  # TODO: support this
  # args << attributes_type.declaration_data[:one_of] if attributes_type.declaration_data.key?(:one_of)
end

Instance Attribute Details

#always_prefix_inputsObject

Returns the value of attribute always_prefix_inputs.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def always_prefix_inputs
  @always_prefix_inputs
end

#attribute_nameObject

Returns the value of attribute attribute_name.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def attribute_name
  @attribute_name
end

#attribute_typeObject

Returns the value of attribute attribute_type.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def attribute_type
  @attribute_type
end

#defaultObject

Returns the value of attribute default.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def default
  @default
end

#is_requiredObject

Returns the value of attribute is_required.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def is_required
  @is_required
end

#prefixObject

Returns the value of attribute prefix.



39
40
41
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 39

def prefix
  @prefix
end

Class Method Details

.attribute_to_options(attribute_name, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 7

def attribute_to_options(
  attribute_name,
  attribute_type:,
  prefix:,
  is_required:,
  default:,
  always_prefix_inputs:
)
  [Model, Attributes, Flag].each do |klass|
    if klass.applicable?(attribute_type)
      return klass.attribute_to_options(
        attribute_name,
        attribute_type:,
        prefix:,
        is_required:,
        default:,
        always_prefix_inputs:
      )
    end
  end

  new(
    attribute_name:,
    attribute_type:,
    prefix:,
    is_required:,
    default:,
    always_prefix_inputs:
  )
end

Instance Method Details

#_argument_text(prefixed_name) ⇒ Object



141
142
143
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 141

def _argument_text(prefixed_name)
  Util.constantify(prefixed_name)
end

#_long_option_name(prefixed_name) ⇒ Object



137
138
139
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 137

def _long_option_name(prefixed_name)
  Util.kebab_case(prefixed_name)
end

#_non_colliding_path(full_paths) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 68

def _non_colliding_path(full_paths)
  1.upto(full_path.length - 1) do |size|
    candidate_path = _truncated_path(full_path, size)
    match_count = _truncated_paths(full_paths, size).select { |path| path == candidate_path }.size

    if match_count == 1
      return candidate_path
    elsif match_count == 0
      # :nocov:
      raise "Not expecting to reach here"
      # :nocov:
    end
  end

  full_path
end

#_prefixed_name(full_paths) ⇒ Object



129
130
131
132
133
134
135
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 129

def _prefixed_name(full_paths)
  if always_prefix_inputs
    full_path.join("_")
  else
    _non_colliding_path(full_paths).join("_")
  end
end

#_truncated_path(path, size) ⇒ Object



91
92
93
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 91

def _truncated_path(path, size)
  path[(-size)..]
end

#_truncated_paths(paths, size) ⇒ Object



85
86
87
88
89
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 85

def _truncated_paths(paths, size)
  paths.map do |path|
    _truncated_path(path, size)
  end
end

#array?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 153

def array?
  attribute_type.extends?(BuiltinTypes[:array])
end

#cast_value(value) ⇒ Object



60
61
62
63
64
65
66
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 60

def cast_value(value)
  if array?
    [value]
  else
    value
  end
end

#descriptionObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 169

def description
  desc = []
  attributes_description = attribute_type.description

  if attributes_description && !BuiltinTypes.builtin?(attribute_type)
    desc << attributes_description
  end

  if required?
    desc << "Required"
  end

  unless primitive?
    one_of = attribute_type.declaration_data[:one_of]

    if one_of && !one_of.empty?
      desc << "One of: #{one_of.join(", ")}"
    end
  end

  if has_default? && show_default?
    displayable_str = to_bash_str(default)
    desc << "Default: #{displayable_str}"
  end

  unless desc.empty?
    desc.map!.with_index do |d, index|
      break if index == desc.size - 1

      if d =~ /[.!?\]}:]$/
        d
      else
        "#{d}."
      end
    end

    desc.join(" ")
  end
end

#full_pathObject



145
146
147
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 145

def full_path
  [*prefix, attribute_name]
end

#has_default?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 157

def has_default?
  !default.nil?
end

#primitive?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 165

def primitive?
  attribute_type.primitive?
end

#required?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 149

def required?
  is_required
end

#show_default?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 161

def show_default?
  true
end

#supports_short_option?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 125

def supports_short_option?
  true
end

#to_args(short_options_used, full_paths) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'src/sh_cli_connector/inputs_parser/option.rb', line 95

def to_args(short_options_used, full_paths)
  prefixed_name = _prefixed_name(full_paths)
  long_option_name = _long_option_name(prefixed_name)

  argument_text = _argument_text(prefixed_name)

  args = if argument_text
           ["--#{long_option_name} #{argument_text}"]
         else
           ["--#{long_option_name}"]
         end

  if supports_short_option?
    short_option = attribute_name[0]
    # TODO: we should prioritize required options to get the short name in case of collision?
    unless short_options_used.include?(short_option)
      short_options_used << short_option
      args << if argument_text
                "-#{short_option} #{argument_text}"
              else
                "-#{short_option}"
              end
    end
  end

  args << description if description

  args
end