Module: CLASP::Util::ValueParser::Internal_

Includes:
Exceptions
Defined in:
lib/clasp/util/value_parser.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.is_integer?(type) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'lib/clasp/util/value_parser.rb', line 63

def self.is_integer? type

    h = {}

    return true if Integer == type
    return true if :integer == type

    false
end

.obtain_integer(value, constraint, argument_spec) ⇒ Object



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/clasp/util/value_parser.rb', line 73

def self.obtain_integer value, constraint, argument_spec

    # If no value is given, then use the default (and don't do any
    # range testing)

    if (value || '').empty?

        def_value = argument_spec.default_value

        if (def_value || '').to_s.empty?

            msg = "no value specified for the option '#{argument_spec.name}', which has no default value either"

            warn msg if $DEBUG

            raise MissingValueException, msg
        end

        begin

            return Integer(def_value)
        rescue ArgumentError => x

            msg = "default value '#{def_value}' specified for option '#{argument_spec.name}' that requires the value to be an integer"

            warn msg if $DEBUG

            raise InvalidIntegerException, msg
        end
    end

    # obtain the integer from the value

    v = nil

    begin

        v = Integer(value)
    rescue ArgumentError => x

        msg = "value '#{value}' specified for option '#{argument_spec.name}' that requires the value to be an integer"

        warn msg if $DEBUG

        raise InvalidIntegerException, msg
    end

    # Is there a value constraint?:
    #
    # - values (obtained from argument_spec#values)
    # - range
    # - minimum & maximum

    values_range = argument_spec.values_range

    unless values_range.empty?

        v_s     =   v.to_s

        v_s     =   '+' + v_s unless '-' == v_s[0]

        vr_s    =   values_range.map { |x| x.to_s }.map { |x| '-' == x[0] ? x : '+' + x }

        unless vr_s.include? v_s

            msg = "given value '#{value}' specified for option '#{argument_spec.name}' does not fall within the required range"

            raise IntegerOutOfRangeException, msg
        end
    else

        case range = constraint[:range]
        when :negative

            if v >= 0

                msg = "given value '#{value}' specified for option '#{argument_spec.name}' must be a negative integer"

                raise IntegerOutOfRangeException, msg
            end
        when :positive

            if v < 1

                msg = "given value '#{value}' specified for option '#{argument_spec.name}' must be a positive integer"

                raise IntegerOutOfRangeException, msg
            end
        when :non_positive

            if v > 0

                msg = "given value '#{value}' specified for option '#{argument_spec.name}' must be a non-positive integer"

                raise IntegerOutOfRangeException, msg
            end
        when :non_negative

            if v < 0

                msg = "given value '#{value}' specified for option '#{argument_spec.name}' must be a non-negative integer"

                raise IntegerOutOfRangeException, msg
            end
        when Range

            unless range.include?

                msg = "given value '#{value}' specified for option '#{argument_spec.name}' does not fall within the required range"

                raise IntegerOutOfRangeException, msg
            end
        else

            ;
        end
    end

    v
end