Class: Radar::Config::UseArray
- Inherits:
-
Object
- Object
- Radar::Config::UseArray
- Extended by:
- Forwardable
- Defined in:
- lib/radar/config.rb
Overview
A subclass of Array which allows for slightly different usage, based
on ActionDispatch::MiddlewareStack
in Rails 3. The main methods are
enumerated below:
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete the object with the given key or index.
-
#index(key) ⇒ Object
Returns the value for the given key.
-
#initialize(*args, &block) ⇒ UseArray
constructor
Initializes the UseArray with the given block used to generate the value created for the #use method.
-
#insert(key, *args, &block) ⇒ Object
(also: #insert_before)
Insert the given key at the given index or directly before the given object (by key).
-
#insert_after(key, *args, &block) ⇒ Object
Insert after the given key.
-
#swap(key, *args, &block) ⇒ Object
Swaps out the given object at the given index or key with a new object.
-
#use(*args, &block) ⇒ Object
Use the given key.
-
#values ⇒ Object
Returns the values of this array.
Constructor Details
#initialize(*args, &block) ⇒ UseArray
69 70 71 72 |
# File 'lib/radar/config.rb', line 69 def initialize(*args, &block) @_array = [] @_use_block = block || Proc.new { |key, *args| [key, key] } end |
Instance Method Details
#delete(key) ⇒ Object
Delete the object with the given key or index.
104 105 106 |
# File 'lib/radar/config.rb', line 104 def delete(key) @_array.delete_at(index(key)) end |
#index(key) ⇒ Object
Returns the value for the given key. If the key is an integer, it is returned as-is. Otherwise, do a lookup on the array for the the given key and return the index of it.
111 112 113 114 115 116 117 118 |
# File 'lib/radar/config.rb', line 111 def index(key) return key if key.is_a?(Integer) @_array.each_with_index do |data, i| return i if data[0] == key end nil end |
#insert(key, *args, &block) ⇒ Object Also known as: insert_before
Insert the given key at the given index or directly before the given object (by key).
82 83 84 |
# File 'lib/radar/config.rb', line 82 def insert(key, *args, &block) @_array.insert(index(key), @_use_block.call(*args, &block)) end |
#insert_after(key, *args, &block) ⇒ Object
Insert after the given key.
88 89 90 91 92 |
# File 'lib/radar/config.rb', line 88 def insert_after(key, *args, &block) i = index(key) raise ArgumentError.new("No such key found: #{key}") if !i insert(i + 1, *args, &block) end |
#swap(key, *args, &block) ⇒ Object
Swaps out the given object at the given index or key with a new object.
96 97 98 99 100 101 |
# File 'lib/radar/config.rb', line 96 def swap(key, *args, &block) i = index(key) raise ArgumentError.new("No such key found: #{key}") if !i delete(i) insert(i, *args, &block) end |
#use(*args, &block) ⇒ Object
Use the given key. It is up to the configured use block (given by the initializer) to generate the actual key/value stored in the array.
76 77 78 |
# File 'lib/radar/config.rb', line 76 def use(*args, &block) insert(length, *args, &block) end |
#values ⇒ Object
Returns the values of this array.
121 122 123 124 125 126 |
# File 'lib/radar/config.rb', line 121 def values @_array.inject([]) do |acc, data| acc << data[1] acc end end |