Class: Hike::NormalizedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/hike/normalized_array.rb

Overview

`NormalizedArray` is an internal abstract wrapper class that calls a callback `normalize_element` anytime an element is added to the Array.

`Extensions` and `Paths` are subclasses of `NormalizedArray`.

Direct Known Subclasses

Extensions, Paths

Instance Method Summary collapse

Constructor Details

#initializeNormalizedArray

Returns a new instance of NormalizedArray.


8
9
10
# File 'lib/hike/normalized_array.rb', line 8

def initialize
  super()
end

Instance Method Details

#<<(element) ⇒ Object


24
25
26
# File 'lib/hike/normalized_array.rb', line 24

def <<(element)
  super normalize_element(element)
end

#[]=(*args) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hike/normalized_array.rb', line 12

def []=(*args)
  value = args.pop

  if value.respond_to?(:to_ary)
    value = normalize_elements(value)
  else
    value = normalize_element(value)
  end

  super(*args.concat([value]))
end

#collect!Object Also known as: map!


28
29
30
31
32
33
# File 'lib/hike/normalized_array.rb', line 28

def collect!
  super do |element|
    result = yield element
    normalize_element(result)
  end
end

#insert(index, *elements) ⇒ Object


37
38
39
# File 'lib/hike/normalized_array.rb', line 37

def insert(index, *elements)
  super index, *normalize_elements(elements)
end

#normalize_elements(elements) ⇒ Object


53
54
55
56
57
# File 'lib/hike/normalized_array.rb', line 53

def normalize_elements(elements)
  elements.map do |element|
    normalize_element(element)
  end
end

#push(*elements) ⇒ Object


41
42
43
# File 'lib/hike/normalized_array.rb', line 41

def push(*elements)
  super(*normalize_elements(elements))
end

#replace(elements) ⇒ Object


45
46
47
# File 'lib/hike/normalized_array.rb', line 45

def replace(elements)
  super normalize_elements(elements)
end

#unshift(*elements) ⇒ Object


49
50
51
# File 'lib/hike/normalized_array.rb', line 49

def unshift(*elements)
  super(*normalize_elements(elements))
end