Class: MockServer::Model::ArrayOf

Inherits:
Array
  • Object
show all
Defined in:
lib/mockserver/model/array_of.rb

Overview

The ArrayOf class stores instances of a given class only.

Direct Known Subclasses

Cookies, Expectations, Headers, Parameters, Requests, Strings

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ ArrayOf

Create an array from the elements passed in



23
24
25
26
27
# File 'lib/mockserver/model/array_of.rb', line 23

def initialize(items)
  items.each do |item|
    self << item
  end
end

Instance Method Details

#<<(item) ⇒ Object

Add the item to the array

Parameters:

  • item (Object)

    an item of the type/class supported by this array

Raises:

  • (Exception)

    if the item cannot be converted to the allowed class



37
38
39
# File 'lib/mockserver/model/array_of.rb', line 37

def <<(item)
  add_element(convert_to_child_class(item))
end

#[]=(index, item) ⇒ Object

Set the given item at the index

Parameters:

  • index (Integer)

    the index for the new element

  • item (Object)

    an item of the type/class supported by this array

Raises:

  • (Exception)

    if the item cannot be converted to the allowed class



45
46
47
# File 'lib/mockserver/model/array_of.rb', line 45

def []=(index, item)
  set_element(index, convert_to_child_class(item))
end

#add_elementObject



18
# File 'lib/mockserver/model/array_of.rb', line 18

alias_method :add_element, :<<

#child_classObject

The class/type that this array stores



30
31
32
# File 'lib/mockserver/model/array_of.rb', line 30

def child_class
  fail 'Subclass should override method :child_class'
end

#convert_to_child_class(item) ⇒ Object

Cast item to target class



74
75
76
77
78
79
80
81
82
83
# File 'lib/mockserver/model/array_of.rb', line 74

def convert_to_child_class(item)
  if item && item.class != child_class
    begin
      item = child_class.new(item)
    rescue Exception => e # rubocop:disable Lint/RescueException
      raise "Failed to convert element: #{item} to required type #{child_class}. Error: #{e.message}"
    end
  end
  item
end

#insert(index, item) ⇒ Object

Adds the given item at the index and shifts elements forward

Parameters:

  • index (Integer)

    the index for the new element

  • item (Object)

    an item of the type/class supported by this array

Raises:

  • (Exception)

    if the item cannot be converted to the allowed class



53
54
55
# File 'lib/mockserver/model/array_of.rb', line 53

def insert(index, item)
  insert_element(index, convert_to_child_class(item))
end

#insert_elementObject



20
# File 'lib/mockserver/model/array_of.rb', line 20

alias_method :insert_element, :insert

#set(index, item) ⇒ Object

Method to set the element at the specified index. Will insert at index if there is another object at the index; otherwise will update. If the special DEFAULT_MISSING_INDEX value is given, will insert at the end.

Parameters:

  • index (Integer)

    the index for the new element

  • item (Object)

    an item of the type/class supported by this array

Raises:

  • (Exception)

    if the item cannot be converted to the allowed class



63
64
65
66
67
68
69
70
71
# File 'lib/mockserver/model/array_of.rb', line 63

def set(index, item)
  if index == DEFAULT_MISSING_INDEX
    self << item
  elsif self[index]
    insert(index, item)
  else
    self[index] = item
  end
end

#set_elementObject



19
# File 'lib/mockserver/model/array_of.rb', line 19

alias_method :set_element, :[]=