Class: Familia::Validation::ArgumentMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/familia/validation/expectations.rb

Overview

Argument matcher for flexible command argument validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, *options) ⇒ ArgumentMatcher

Returns a new instance of ArgumentMatcher.



322
323
324
325
# File 'lib/familia/validation/expectations.rb', line 322

def initialize(type, *options)
  @type = type
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



320
321
322
# File 'lib/familia/validation/expectations.rb', line 320

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



320
321
322
# File 'lib/familia/validation/expectations.rb', line 320

def type
  @type
end

Instance Method Details

#matches?(value) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/familia/validation/expectations.rb', line 327

def matches?(value)
  case @type
  when :any_string
    value.is_a?(String)
  when :any_number
    value.to_s.match?(/^\d+$/)
  when :any_value
    true
  when :regex
    @options.first.match?(value.to_s)
  else
    false
  end
end

#to_sObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/familia/validation/expectations.rb', line 342

def to_s
  case @type
  when :any_string
    '<any_string>'
  when :any_number
    '<any_number>'
  when :any_value
    '<any_value>'
  when :regex
    "<match:#{@options.first}>"
  else
    "<#{@type}>"
  end
end