Sha256: 3fbd041bc5d5668222289b2b8103e22cb8129e7f533e2e1f0a9e0547c374ed2f
Contents?: true
Size: 1.99 KB
Versions: 3
Compression:
Stored size: 1.99 KB
Contents
require 'command_mapper/types/type' require 'command_mapper/types/str' module CommandMapper module Types # # Represents a list type. # class List < Type # The seperator character. # # @return [String] attr_reader :separator # The list element type. # # @return [Type] attr_reader :type # # Initializes the list type. # # @param [String] separator # The list separator character. # # @param [Type, Hash] value # The list's value type. # 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 # # Specifies whether the option's value may accept empty values. # # @return [Boolean] # def allow_empty? @allow_empty end # # Validates the value. # # @param [Object] value # The given value to validate. # # @return [true, (false, String)] # Returns true if the value is valid, or `false` and a validation error # message if the value is not compatible. # def validate(value) values = Array(value) if values.empty? unless allow_empty? return [false, "cannot be empty"] end end values.each do |element| valid, message = @type.validate(element) unless valid return [false, "element #{message}"] end end return true end # # Formats the value into a list. # # @param [Object] value # The given value to format. # # @return [String] # The formatted list. # def format(value) Array(value).map(&@type.method(:format)).join(@separator) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
command_mapper-0.2.0 | lib/command_mapper/types/list.rb |
command_mapper-0.1.2 | lib/command_mapper/types/list.rb |
command_mapper-0.1.1 | lib/command_mapper/types/list.rb |