Class: MockServer::Model::ArrayOf
- Inherits:
-
Array
- Object
- Array
- MockServer::Model::ArrayOf
- Defined in:
- lib/mockserver/model/array_of.rb
Overview
The ArrayOf class stores instances of a given class only.
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Add the item to the array.
-
#[]=(index, item) ⇒ Object
Set the given item at the index.
- #add_element ⇒ Object
-
#child_class ⇒ Object
The class/type that this array stores.
-
#convert_to_child_class(item) ⇒ Object
Cast item to target class.
-
#initialize(items) ⇒ ArrayOf
constructor
Create an array from the elements passed in.
-
#insert(index, item) ⇒ Object
Adds the given item at the index and shifts elements forward.
- #insert_element ⇒ Object
-
#set(index, item) ⇒ Object
Method to set the element at the specified index.
- #set_element ⇒ Object
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
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
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_element ⇒ Object
18 |
# File 'lib/mockserver/model/array_of.rb', line 18 alias_method :add_element, :<< |
#child_class ⇒ Object
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.}" end end item end |
#insert(index, item) ⇒ Object
Adds the given item at the index and shifts elements forward
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_element ⇒ Object
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.
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_element ⇒ Object
19 |
# File 'lib/mockserver/model/array_of.rb', line 19 alias_method :set_element, :[]= |