Class: CommandMapper::Types::KeyValue

Inherits:
Type
  • Object
show all
Defined in:
lib/command_mapper/types/key_value.rb

Overview

Represents a key-value type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separator: '=', key: Str.new, value: Str.new) ⇒ KeyValue

Initializes the key-value value type.

Parameters:

  • separator (String) (defaults to: '=')

    The key-value separator.

  • key (Type, Hash) (defaults to: Str.new)

    The key's value type.

  • value (Type, Hash) (defaults to: Str.new)

    The value's value type.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/command_mapper/types/key_value.rb', line 44

def initialize(separator: '=', key: Str.new, value: Str.new)
  @separator = separator

  if key.nil?
    raise(ArgumentError,"key: keyword cannot be nil")
  end

  if value.nil?
    raise(ArgumentError,"value: keyword cannot be nil")
  end

  @key   = Types::Type(key)
  @value = Types::Type(value)
end

Instance Attribute Details

#keyType (readonly)

The key's type.

Returns:



23
24
25
# File 'lib/command_mapper/types/key_value.rb', line 23

def key
  @key
end

#separatorString (readonly)

The separator String between the key and value.

Returns:

  • (String)


16
17
18
# File 'lib/command_mapper/types/key_value.rb', line 16

def separator
  @separator
end

#valueType (readonly)

The value's type.

Returns:



30
31
32
# File 'lib/command_mapper/types/key_value.rb', line 30

def value
  @value
end

Instance Method Details

#format(value) ⇒ String

Formats a value into a key-value pair.

Parameters:

  • value (Hash, Array, #to_s)

    The given value to format.

Returns:

  • (String)

    The formatted key-value pair.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/command_mapper/types/key_value.rb', line 123

def format(value)
  case value
  when Hash, Array
    case value
    when Hash
      key, value = value.first
    when Array
      key, value = value
    end

    "#{@key.format(key)}#{@separator}#{@value.format(value)}"
  else
    super(value)
  end
end

#validate(value) ⇒ true, (false, String)

Valides the given value.

Parameters:

  • value (Object)

    The given value to validate.

Returns:

  • (true, (false, String))

    Returns true if the value is valid, or false and a validation error message if the value is not compatible.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/command_mapper/types/key_value.rb', line 71

def validate(value)
  case value
  when Hash
    if value.length < 1
      return [false, "cannot be empty"]
    end

    if value.length > 1
      return [false, "cannot contain multiple key:value pairs (#{value.inspect})"]
    end

    key, value = value.first
  when Array
    if value.length < 2
      return [false, "must contain two elements (#{value.inspect})"]
    end

    if value.length > 2
      return [false, "cannot contain more than two elements (#{value.inspect})"]
    end

    key, value = value
  else
    return [false, "must be a Hash or an Array (#{value.inspect})"]
  end

  valid, message = @key.validate(key)

  unless valid
    return [false, "key #{message}"]
  end

  valid, message = @value.validate(value)

  unless valid
    return [false, "value #{message}"]
  end

  return true
end