Class: CommandMapper::Types::List
- Defined in:
- lib/command_mapper/types/list.rb
Overview
Represents a list type.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#separator ⇒ String
readonly
The seperator character.
-
#type ⇒ Type
readonly
The list element type.
Instance Method Summary collapse
-
#allow_empty? ⇒ Boolean
Specifies whether the option's value may accept empty values.
-
#format(value) ⇒ String
Formats the value into a list.
-
#initialize(separator: ',', type: Str.new, allow_empty: false) ⇒ List
constructor
Initializes the list type.
-
#validate(value) ⇒ true, (false, String)
Validates the value.
Constructor Details
#initialize(separator: ',', type: Str.new, allow_empty: false) ⇒ List
Initializes the list type.
37 38 39 40 41 42 43 44 45 |
# File 'lib/command_mapper/types/list.rb', line 37 def initialize(separator: ',', type: Str.new, allow_empty: false) if type.nil? raise(ArgumentError,"type: keyword cannot be nil") end @separator = separator @type = Types::Type(type) @allow_empty = allow_empty end |
Instance Attribute Details
#separator ⇒ String (readonly)
The seperator character.
16 17 18 |
# File 'lib/command_mapper/types/list.rb', line 16 def separator @separator end |
#type ⇒ Type (readonly)
The list element type.
23 24 25 |
# File 'lib/command_mapper/types/list.rb', line 23 def type @type end |
Instance Method Details
#allow_empty? ⇒ Boolean
Specifies whether the option's value may accept empty values.
54 55 56 |
# File 'lib/command_mapper/types/list.rb', line 54 def allow_empty? @allow_empty end |
#format(value) ⇒ String
Formats the value into a list.
101 102 103 |
# File 'lib/command_mapper/types/list.rb', line 101 def format(value) Array(value).map(&@type.method(:format)).join(@separator) end |
#validate(value) ⇒ true, (false, String)
Validates the value.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/command_mapper/types/list.rb', line 70 def validate(value) values = Array(value) if values.empty? unless allow_empty? return [false, "cannot be empty"] end end values.each do |element| valid, = @type.validate(element) unless valid return [false, "element #{}"] end end return true end |