Class: DSLCompose::DSL::Arguments::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/dsl/arguments/argument.rb,
lib/dsl_compose/dsl/arguments/argument/interpreter.rb,
lib/dsl_compose/dsl/arguments/argument/in_validation.rb,
lib/dsl_compose/dsl/arguments/argument/is_a_validation.rb,
lib/dsl_compose/dsl/arguments/argument/format_validation.rb,
lib/dsl_compose/dsl/arguments/argument/length_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_in_validation.rb,
lib/dsl_compose/dsl/arguments/argument/end_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/equal_to_validation.rb,
lib/dsl_compose/dsl/arguments/argument/less_than_validation.rb,
lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/greater_than_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_end_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_start_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/less_than_or_equal_to_validation.rb,
lib/dsl_compose/dsl/arguments/argument/greater_than_or_equal_to_validation.rb

Defined Under Namespace

Classes: ArgumentNameReservedError, DescriptionAlreadyExistsError, EndWithValidation, EqualToValidation, FormatValidation, GreaterThanOrEqualToValidation, GreaterThanValidation, InValidation, Interpreter, InvalidDescriptionError, InvalidNameError, InvalidTypeError, IsAValidation, LengthValidation, LessThanOrEqualToValidation, LessThanValidation, NotEndWithValidation, NotInValidation, NotStartWithValidation, StartWithValidation, ValidationAlreadyExistsError, ValidationIncompatibleError, ValidationInvalidArgumentError

Constant Summary collapse

RESERVED_ARGUMENT_NAMES =
[
  :child_class,
  :dsl_name,
  :method_name
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, required, type, array: false, &block) ⇒ Argument

Create a new Attribute object.

‘name` must be a symbol. `required` is a boolean which determines if this Attribute must be provided when calling its associated DSLMethod. `type` can be either :integer, :boolean, :float, :string or :symbol `block` contains the instructions to further configure this Attribute



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
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 76

def initialize name, required, type, array: false, &block
  if name.is_a? Symbol

    if RESERVED_ARGUMENT_NAMES.include? name
      raise ArgumentNameReservedError, "This argument name `#{name}` is a reserved word. The names #{RESERVED_ARGUMENT_NAMES.join ", "} can not be used here because the Parser uses them to express a structural part of the DSL"
    end

    @name = name
  else
    raise InvalidNameError, "The option name `#{name}` is invalid, it must be of type symbol."
  end

  if type == :integer || type == :boolean || type == :float || type == :string || type == :symbol || type == :class || type == :object
    @type = type
  else
    raise InvalidTypeError, "Argument type `#{type}` must be one of :integer, :boolean, :float, :string, :symbol, :class or :object"
  end

  @required = required ? true : false

  @array = array ? true : false

  # If a block was provided, then we evaluate it using a seperate
  # interpreter class. We do this because the interpreter class contains
  # no other methods or variables, if it was evaluated in the context of
  # this class then the block would have access to all of the methods defined
  # in here.
  if block
    Interpreter.new(self).instance_eval(&block)
  end
rescue => e
  raise e, "Error defining argument #{name}: #{e.message}", e.backtrace
end

Instance Attribute Details

#arrayObject (readonly)

If true, then this argument accepts an array of values. It will also accept a single value, but that single value will be automatically converted to an array



47
48
49
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 47

def array
  @array
end

#descriptionObject (readonly)

An otional description of this Attribute, if provided then it must be a string. The description accepts markdown and is used when generating documentation.



50
51
52
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 50

def description
  @description
end

#end_with_validationObject (readonly)

Returns the value of attribute end_with_validation.



62
63
64
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 62

def end_with_validation
  @end_with_validation
end

#equal_to_validationObject (readonly)

Returns the value of attribute equal_to_validation.



59
60
61
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 59

def equal_to_validation
  @equal_to_validation
end

#format_validationObject (readonly)

Returns the value of attribute format_validation.



58
59
60
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 58

def format_validation
  @format_validation
end

#greater_than_or_equal_to_validationObject (readonly)

Returns the value of attribute greater_than_or_equal_to_validation.



55
56
57
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 55

def greater_than_or_equal_to_validation
  @greater_than_or_equal_to_validation
end

#greater_than_validationObject (readonly)

Optional validations that have been applied to this Argument. When the DSL is used each of these validations will be checked against the value provided to this Argument.



54
55
56
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 54

def greater_than_validation
  @greater_than_validation
end

#in_validationObject (readonly)

Returns the value of attribute in_validation.



60
61
62
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 60

def in_validation
  @in_validation
end

#is_a_validationObject (readonly)

Returns the value of attribute is_a_validation.



67
68
69
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 67

def is_a_validation
  @is_a_validation
end

#length_validationObject (readonly)

Returns the value of attribute length_validation.



66
67
68
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 66

def length_validation
  @length_validation
end

#less_than_or_equal_to_validationObject (readonly)

Returns the value of attribute less_than_or_equal_to_validation.



57
58
59
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 57

def less_than_or_equal_to_validation
  @less_than_or_equal_to_validation
end

#less_than_validationObject (readonly)

Returns the value of attribute less_than_validation.



56
57
58
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 56

def less_than_validation
  @less_than_validation
end

#nameObject (readonly)

The name of this Argument.



38
39
40
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 38

def name
  @name
end

#not_end_with_validationObject (readonly)

Returns the value of attribute not_end_with_validation.



63
64
65
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 63

def not_end_with_validation
  @not_end_with_validation
end

#not_in_validationObject (readonly)

Returns the value of attribute not_in_validation.



61
62
63
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 61

def not_in_validation
  @not_in_validation
end

#not_start_with_validationObject (readonly)

Returns the value of attribute not_start_with_validation.



65
66
67
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 65

def not_start_with_validation
  @not_start_with_validation
end

#requiredObject (readonly)

if required, then this Argument must be provided when calling its associated DSLMethod.



44
45
46
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 44

def required
  @required
end

#start_with_validationObject (readonly)

Returns the value of attribute start_with_validation.



64
65
66
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 64

def start_with_validation
  @start_with_validation
end

#typeObject (readonly)

An arguments type. This determines what kind of value can be passed when calling the associated DSLMethod. ‘type` should be set to either :integer, :boolean, :float, :string or :symbol



42
43
44
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 42

def type
  @type
end

Instance Method Details

#has_description?Boolean

Returns ‘true` if this DSL has a description, else false.

Returns:

  • (Boolean)


127
128
129
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 127

def has_description?
  @description.nil? == false
end

#optional?Boolean

returns true if this DSLMethod is flagged as optional, otherwise returns false.

Returns:

  • (Boolean)


137
138
139
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 137

def optional?
  @required == false
end

#required?Boolean

returns true if this DSLMethod is flagged as required, otherwise returns false.

Returns:

  • (Boolean)


132
133
134
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 132

def required?
  @required == true
end

#set_description(description) ⇒ Object

Set the description for this Argument to the provided value.

‘description` must be a string with a length greater than 0. The `description` can only be set once per Argument



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 114

def set_description description
  unless description.is_a?(String) && description.strip.length > 0
    raise InvalidDescriptionError, "The option description `#{description}` is invalid, it must be of type string and have length greater than 0."
  end

  if has_description?
    raise DescriptionAlreadyExistsError, "The description has already been set"
  end

  @description = description.strip
end

#validate_boolean!(value) ⇒ Object

returns true if every provided boolean validation also returns true



404
405
406
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 404

def validate_boolean! value
  (equal_to_validation.nil? || equal_to_validation.validate!(value))
end

#validate_class!(value) ⇒ Object

returns true if every provided class validation also returns true



409
410
411
412
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 409

def validate_class! value
  # there are no class validations
  true
end

#validate_end_with(value) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 266

def validate_end_with value
  unless value.is_a?(Symbol) || (value.is_a?(Array) && value.all? { |value| value.is_a? Symbol })
    raise ValidationInvalidArgumentError, "The value `#{value}` provided to this validator must be a Symbol or an Array of Symbols"
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @end_with_validation
    @end_with_validation.add_values value
  else
    @end_with_validation = EndWithValidation.new value
  end
end

#validate_equal_to(value) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 220

def validate_equal_to value
  if @equal_to_validation
    raise ValidationAlreadyExistsError, "The validation `equal_to` has already been applied to this method option."
  end

  unless @type == :integer || @type == :float || @type == :string || @type == :symbol || @type == :boolean
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @equal_to_validation = EqualToValidation.new value
end

#validate_float!(value) ⇒ Object

returns true if every provided float validation also returns true



372
373
374
375
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 372

def validate_float! value
  # floats are validated with the same set of validators as integers
  validate_integer! value
end

#validate_format(regexp) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 205

def validate_format regexp
  if @format_validation
    raise ValidationAlreadyExistsError, "The validation `format` has already been applied to this method option."
  end

  unless regexp.is_a? Regexp
    raise ValidationInvalidArgumentError, regexp
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end
  @format_validation = FormatValidation.new regexp
end

#validate_greater_than(value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 141

def validate_greater_than value
  if @greater_than_validation
    raise ValidationAlreadyExistsError, "The validation `greater_than` has already been applied to this method option."
  end

  unless value.is_a?(Numeric)
    raise ValidationInvalidArgumentError, value
  end

  unless @type == :integer || @type == :float
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @greater_than_validation = GreaterThanValidation.new value
end

#validate_greater_than_or_equal_to(value) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 157

def validate_greater_than_or_equal_to value
  if @greater_than_or_equal_to_validation
    raise ValidationAlreadyExistsError, "The validation `greater_than_or_equal_to` has already been applied to this method option."
  end

  unless value.is_a?(Numeric)
    raise ValidationInvalidArgumentError, value
  end

  unless @type == :integer || @type == :float
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @greater_than_or_equal_to_validation = GreaterThanOrEqualToValidation.new value
end

#validate_in(values) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 232

def validate_in values
  unless values.is_a? Array
    raise ValidationInvalidArgumentError, values
  end

  unless @type == :integer || @type == :float || @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @in_validation
    @in_validation.add_values values
  else
    @in_validation = InValidation.new values
  end
end

#validate_integer!(value) ⇒ Object

returns true if every provided integer validation also returns true



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 359

def validate_integer! value
  (greater_than_validation.nil? || greater_than_validation.validate!(value)) &&
    (greater_than_or_equal_to_validation.nil? || greater_than_or_equal_to_validation.validate!(value)) &&
    (less_than_validation.nil? || less_than_validation.validate!(value)) &&
    (less_than_or_equal_to_validation.nil? || less_than_or_equal_to_validation.validate!(value)) &&
    (format_validation.nil? || format_validation.validate!(value)) &&
    (equal_to_validation.nil? || equal_to_validation.validate!(value)) &&
    (in_validation.nil? || in_validation.validate!(value)) &&
    (not_in_validation.nil? || not_in_validation.validate!(value)) &&
    (length_validation.nil? || length_validation.validate!(value))
end

#validate_is_a(klass) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 346

def validate_is_a klass
  if @is_a_validation
    raise ValidationAlreadyExistsError, "The validation `is_a` has already been applied to this method option."
  end

  unless @type == :object
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @is_a_validation = IsAValidation.new(klass)
end

#validate_length(maximum: nil, minimum: nil, is: nil) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 334

def validate_length maximum: nil, minimum: nil, is: nil
  if @length_validation
    raise ValidationAlreadyExistsError, "The validation `length` has already been applied to this method option."
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @length_validation = LengthValidation.new(maximum: maximum, minimum: minimum, is: is)
end

#validate_less_than(value) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 173

def validate_less_than value
  if @less_than_validation
    raise ValidationAlreadyExistsError, "The validation `less_than` has already been applied to this method option."
  end

  unless value.is_a?(Numeric)
    raise ValidationInvalidArgumentError, value
  end

  unless @type == :integer || @type == :float
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @less_than_validation = LessThanValidation.new value
end

#validate_less_than_or_equal_to(value) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 189

def validate_less_than_or_equal_to value
  if @less_than_or_equal_to_validation
    raise ValidationAlreadyExistsError, "The validation `less_than_or_equal_to` has already been applied to this method option."
  end

  unless value.is_a?(Numeric)
    raise ValidationInvalidArgumentError, value
  end

  unless @type == :integer || @type == :float
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  @less_than_or_equal_to_validation = LessThanOrEqualToValidation.new value
end

#validate_not_end_with(value) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 283

def validate_not_end_with value
  unless value.is_a?(Symbol) || (value.is_a?(Array) && value.all? { |value| value.is_a? Symbol })
    raise ValidationInvalidArgumentError, "The value `#{value}` provided to this validator must be a Symbol or an Array of Symbols"
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @not_end_with_validation
    @not_end_with_validation.add_values value
  else
    @not_end_with_validation = NotEndWithValidation.new value
  end
end

#validate_not_in(values) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 249

def validate_not_in values
  unless values.is_a? Array
    raise ValidationInvalidArgumentError, values
  end

  unless @type == :integer || @type == :float || @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @not_in_validation
    @not_in_validation.add_values values
  else
    @not_in_validation = NotInValidation.new values
  end
end

#validate_not_start_with(value) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 317

def validate_not_start_with value
  unless value.is_a?(Symbol) || (value.is_a?(Array) && value.all? { |value| value.is_a? Symbol })
    raise ValidationInvalidArgumentError, "The value `#{value}` provided to this validator must be a Symbol or an Array of Symbols"
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @not_start_with_validation
    @not_start_with_validation.add_values value
  else
    @not_start_with_validation = NotStartWithValidation.new value
  end
end

#validate_object!(value) ⇒ Object

returns true if every provided object validation also returns true



415
416
417
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 415

def validate_object! value
  (is_a_validation.nil? || is_a_validation.validate!(value))
end

#validate_start_with(value) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 300

def validate_start_with value
  unless value.is_a?(Symbol) || (value.is_a?(Array) && value.all? { |value| value.is_a? Symbol })
    raise ValidationInvalidArgumentError, "The value `#{value}` provided to this validator must be a Symbol or an Array of Symbols"
  end

  unless @type == :string || @type == :symbol
    raise ValidationIncompatibleError, "The validation type #{@type} is not compatible with this argument type"
  end

  # if this validation has already been applied, then add the values to the existing validation
  if @start_with_validation
    @start_with_validation.add_values value
  else
    @start_with_validation = StartWithValidation.new value
  end
end

#validate_string!(value) ⇒ Object

returns true if every provided string validation also returns true



391
392
393
394
395
396
397
398
399
400
401
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 391

def validate_string! value
  (format_validation.nil? || format_validation.validate!(value)) &&
    (equal_to_validation.nil? || equal_to_validation.validate!(value)) &&
    (in_validation.nil? || in_validation.validate!(value)) &&
    (not_in_validation.nil? || not_in_validation.validate!(value)) &&
    (end_with_validation.nil? || end_with_validation.validate!(value)) &&
    (not_end_with_validation.nil? || not_end_with_validation.validate!(value)) &&
    (start_with_validation.nil? || start_with_validation.validate!(value)) &&
    (not_start_with_validation.nil? || not_start_with_validation.validate!(value)) &&
    (length_validation.nil? || length_validation.validate!(value))
end

#validate_symbol!(value) ⇒ Object

returns true if every provided symbol validation also returns true



378
379
380
381
382
383
384
385
386
387
388
# File 'lib/dsl_compose/dsl/arguments/argument.rb', line 378

def validate_symbol! value
  (format_validation.nil? || format_validation.validate!(value)) &&
    (equal_to_validation.nil? || equal_to_validation.validate!(value)) &&
    (in_validation.nil? || in_validation.validate!(value)) &&
    (not_in_validation.nil? || not_in_validation.validate!(value)) &&
    (end_with_validation.nil? || end_with_validation.validate!(value)) &&
    (not_end_with_validation.nil? || not_end_with_validation.validate!(value)) &&
    (start_with_validation.nil? || start_with_validation.validate!(value)) &&
    (not_start_with_validation.nil? || not_start_with_validation.validate!(value)) &&
    (length_validation.nil? || length_validation.validate!(value))
end