command_mapper 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,9 +14,11 @@ module CommandMapper
14
14
  # Specifies whether the hex value will start with `0x` or not.
15
15
  #
16
16
  # @param [Hash{Symbol => Object}] kwargs
17
- # Additional keyword arguments for {Type#initialize}.
17
+ # Additional keyword arguments for {Num#initialize}.
18
18
  #
19
- def initialize(leading_zero: false)
19
+ def initialize(leading_zero: false, **kwargs)
20
+ super(**kwargs)
21
+
20
22
  @leading_zero = leading_zero
21
23
  end
22
24
 
@@ -25,6 +27,8 @@ module CommandMapper
25
27
  #
26
28
  # @return [Boolean]
27
29
  #
30
+ # @api semipublic
31
+ #
28
32
  def leading_zero?
29
33
  @leading_zero
30
34
  end
@@ -39,6 +43,8 @@ module CommandMapper
39
43
  # Returns true if the value is valid, or `false` and a validation error
40
44
  # message if the value is not compatible.
41
45
  #
46
+ # @api semipublic
47
+ #
42
48
  def validate(value)
43
49
  case value
44
50
  when String
@@ -46,6 +52,12 @@ module CommandMapper
46
52
  return [false, "not in hex format (#{value.inspect})"]
47
53
  end
48
54
 
55
+ if @range
56
+ unless @range.include?(value.to_i(16))
57
+ return [false, "unacceptable value (#{value.inspect})"]
58
+ end
59
+ end
60
+
49
61
  return true
50
62
  else
51
63
  super(value)
@@ -61,6 +73,8 @@ module CommandMapper
61
73
  # @return [String]
62
74
  # The formatted numeric value.
63
75
  #
76
+ # @api semipublic
77
+ #
64
78
  def format(value)
65
79
  case value
66
80
  when String
@@ -17,6 +17,8 @@ module CommandMapper
17
17
  # Returns true if the value is valid, or `false` and a validation error
18
18
  # message if the value is not compatible.
19
19
  #
20
+ # @api semipublic
21
+ #
20
22
  def validate(value)
21
23
  valid, message = super(value)
22
24
 
@@ -17,6 +17,8 @@ module CommandMapper
17
17
  # Returns true if the value is valid, or `false` and a validation error
18
18
  # message if the value is not compatible.
19
19
  #
20
+ # @api semipublic
21
+ #
20
22
  def validate(value)
21
23
  valid, message = super(value)
22
24
 
@@ -17,6 +17,8 @@ module CommandMapper
17
17
  # Returns true if the value is valid, or `false` and a validation error
18
18
  # message if the value is not compatible.
19
19
  #
20
+ # @api semipublic
21
+ #
20
22
  def validate(value)
21
23
  unless value.empty?
22
24
  unless File.exists?(value)
@@ -11,16 +11,22 @@ module CommandMapper
11
11
  # The separator String between the key and value.
12
12
  #
13
13
  # @return [String]
14
+ #
15
+ # @api semipublic
14
16
  attr_reader :separator
15
17
 
16
18
  # The key's type.
17
19
  #
18
20
  # @return [Type]
21
+ #
22
+ # @api semipublic
19
23
  attr_reader :key
20
24
 
21
25
  # The value's type.
22
26
  #
23
27
  # @return [Type]
28
+ #
29
+ # @api semipublic
24
30
  attr_reader :value
25
31
 
26
32
  #
@@ -63,6 +69,8 @@ module CommandMapper
63
69
  # Returns true if the value is valid, or `false` and a validation error
64
70
  # message if the value is not compatible.
65
71
  #
72
+ # @api semipublic
73
+ #
66
74
  def validate(value)
67
75
  case value
68
76
  when Hash
@@ -113,6 +121,8 @@ module CommandMapper
113
121
  # @return [String]
114
122
  # The formatted key-value pair.
115
123
  #
124
+ # @api semipublic
125
+ #
116
126
  def format(value)
117
127
  case value
118
128
  when Hash, Array
@@ -36,6 +36,8 @@ module CommandMapper
36
36
  # @return [String]
37
37
  # The formatted key-value list.
38
38
  #
39
+ # @api semipublic
40
+ #
39
41
  def format(value)
40
42
  super(Array(value).map(&@type.method(:format)))
41
43
  end
@@ -11,11 +11,15 @@ module CommandMapper
11
11
  # The seperator character.
12
12
  #
13
13
  # @return [String]
14
+ #
15
+ # @api semipublic
14
16
  attr_reader :separator
15
17
 
16
18
  # The list element type.
17
19
  #
18
20
  # @return [Type]
21
+ #
22
+ # @api semipublic
19
23
  attr_reader :type
20
24
 
21
25
  #
@@ -43,6 +47,8 @@ module CommandMapper
43
47
  #
44
48
  # @return [Boolean]
45
49
  #
50
+ # @api semipublic
51
+ #
46
52
  def allow_empty?
47
53
  @allow_empty
48
54
  end
@@ -57,6 +63,8 @@ module CommandMapper
57
63
  # Returns true if the value is valid, or `false` and a validation error
58
64
  # message if the value is not compatible.
59
65
  #
66
+ # @api semipublic
67
+ #
60
68
  def validate(value)
61
69
  values = Array(value)
62
70
 
@@ -86,6 +94,8 @@ module CommandMapper
86
94
  # @return [String]
87
95
  # The formatted list.
88
96
  #
97
+ # @api semipublic
98
+ #
89
99
  def format(value)
90
100
  Array(value).map(&@type.method(:format)).join(@separator)
91
101
  end
@@ -2,9 +2,16 @@ require 'command_mapper/types/type'
2
2
 
3
3
  module CommandMapper
4
4
  module Types
5
+ #
6
+ # Represents a mapping of Ruby values to other String values.
7
+ #
5
8
  class Map < Type
6
9
 
10
+ # The map of values to Strings.
11
+ #
7
12
  # @return [Hash{Object => String}]
13
+ #
14
+ # @api semipublic
8
15
  attr_reader :map
9
16
 
10
17
  #
@@ -45,9 +52,11 @@ module CommandMapper
45
52
  # Returns true if the value is valid, or `false` and a validation error
46
53
  # message if the value is not compatible.
47
54
  #
55
+ # @api semipublic
56
+ #
48
57
  def validate(value)
49
58
  unless (@map.has_key?(value) || @map.has_value?(value))
50
- return [false, "unknown value (#{value.inspect})"]
59
+ return [false, "unknown value (#{value.inspect}) must be #{@map.keys.map(&:inspect).join(', ')}, or #{@map.values.map(&:inspect).join(', ')}"]
51
60
  end
52
61
 
53
62
  return true
@@ -65,6 +74,8 @@ module CommandMapper
65
74
  # @raise [KeyError]
66
75
  # The given value is not a key or value in the map.
67
76
  #
77
+ # @api semipublic
78
+ #
68
79
  def format(value)
69
80
  if @map.has_key?(value)
70
81
  super(@map[value])
@@ -7,6 +7,23 @@ module CommandMapper
7
7
  #
8
8
  class Num < Type
9
9
 
10
+ # The optional range of acceptable numbers.
11
+ #
12
+ # @return [Range, nil]
13
+ #
14
+ # @api semipublic
15
+ attr_reader :range
16
+
17
+ #
18
+ # Initializes the numeric value.
19
+ #
20
+ # @param [Range] range
21
+ # Specifies the range of acceptable numbers.
22
+ #
23
+ def initialize(range: nil)
24
+ @range = range
25
+ end
26
+
10
27
  #
11
28
  # Validates a value.
12
29
  #
@@ -17,10 +34,12 @@ module CommandMapper
17
34
  # Returns true if the value is valid, or `false` and a validation error
18
35
  # message if the value is not compatible.
19
36
  #
37
+ # @api semipublic
38
+ #
20
39
  def validate(value)
21
40
  case value
22
41
  when Integer
23
- return true
42
+ # no-op
24
43
  when String
25
44
  unless value =~ /\A\d+\z/
26
45
  return [false, "contains non-numeric characters (#{value.inspect})"]
@@ -31,6 +50,12 @@ module CommandMapper
31
50
  end
32
51
  end
33
52
 
53
+ if @range
54
+ unless @range.include?(value.to_i)
55
+ return [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"]
56
+ end
57
+ end
58
+
34
59
  return true
35
60
  end
36
61
 
@@ -43,6 +68,8 @@ module CommandMapper
43
68
  # @return [String]
44
69
  # The formatted numeric value.
45
70
  #
71
+ # @api semipublic
72
+ #
46
73
  def format(value)
47
74
  case value
48
75
  when Integer, String then value.to_s
@@ -2,6 +2,9 @@ require 'command_mapper/types/type'
2
2
 
3
3
  module CommandMapper
4
4
  module Types
5
+ #
6
+ # Represents an arbitrary string value.
7
+ #
5
8
  class Str < Type
6
9
  #
7
10
  # Initializes the value.
@@ -22,6 +25,8 @@ module CommandMapper
22
25
  #
23
26
  # @return [Boolean]
24
27
  #
28
+ # @api semipublic
29
+ #
25
30
  def allow_empty?
26
31
  @allow_empty
27
32
  end
@@ -31,6 +36,8 @@ module CommandMapper
31
36
  #
32
37
  # @return [Boolean]
33
38
  #
39
+ # @api semipublic
40
+ #
34
41
  def allow_blank?
35
42
  @allow_blank
36
43
  end
@@ -42,7 +49,7 @@ module CommandMapper
42
49
  # The given value to validate.
43
50
  #
44
51
  # @return [true, (false, String)]
45
- # Returns true if the valid is considered valid, or false and a
52
+ # Returns true if the value is considered valid, or false and a
46
53
  # validation message if the value is not valid.
47
54
  # * If `nil` is given and a value is required, then `false` will be
48
55
  # returned.
@@ -51,6 +58,8 @@ module CommandMapper
51
58
  # * If an empty value is given and blank values are not allowed, then
52
59
  # `false` will be returned.
53
60
  #
61
+ # @api semipublic
62
+ #
54
63
  def validate(value)
55
64
  case value
56
65
  when nil
@@ -50,6 +50,8 @@ module CommandMapper
50
50
  #
51
51
  # argument :ports, required: true, type: PortRange.new
52
52
  #
53
+ # @api semipublic
54
+ #
53
55
  class Type
54
56
 
55
57
  #
@@ -96,6 +98,8 @@ module CommandMapper
96
98
  # @raise [ArgumentError]
97
99
  # The given type value was not a {Type}, `Hash`, or `nil`,
98
100
  #
101
+ # @api semipublic
102
+ #
99
103
  def self.Type(value)
100
104
  case value
101
105
  when Type then value
@@ -1,4 +1,4 @@
1
1
  module CommandMapper
2
2
  # Version of command_mapper
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.1'
4
4
  end