Class: RequestType::Validator::ArrayWithDefault

Inherits:
Object
  • Object
show all
Defined in:
app/models/request_type/validator.rb

Overview

Array class that lets you set a default value If first argument is an array, second argument is assumed to be default Raises exception is default is not in the array In all other cases passes argument to standard array initializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, default) ⇒ ArrayWithDefault

Returns a new instance of ArrayWithDefault

Raises:

  • (StandardError)

40
41
42
43
44
45
# File 'app/models/request_type/validator.rb', line 40

def initialize(array, default)
  raise StandardError, 'Default is not in array' unless array.include?(default)

  @default = default
  @array = array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


47
48
49
# File 'app/models/request_type/validator.rb', line 47

def method_missing(method, *args, &block)
  @array.send(method, *args, &block)
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default


38
39
40
# File 'app/models/request_type/validator.rb', line 38

def default
  @default
end

Instance Method Details

#include?(option) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
54
# File 'app/models/request_type/validator.rb', line 51

def include?(option)
  # We have to define include specifically
  @array.include?(option)
end

#to_aObject


56
57
58
# File 'app/models/request_type/validator.rb', line 56

def to_a
  @array
end