Class: Machinery::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) ⇒ Array

Returns a new instance of Array.



53
54
55
# File 'lib/array.rb', line 53

def initialize(elements = [])
  @elements = self.class.convert_raw_array(elements)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



112
113
114
# File 'lib/array.rb', line 112

def method_missing(name, *args, &block)
  @elements.send(name, *args, &block)
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



51
52
53
# File 'lib/array.rb', line 51

def elements
  @elements
end

Class Method Details

.convert_element(element) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/array.rb', line 25

def convert_element(element)
  if @element_class
    element.is_a?(@element_class) ? element : @element_class.from_json(element)
  else
    case element
    when ::Array
      Machinery::Array.from_json(element)
    when Hash
      Machinery::Object.from_json(element)
    else
      element
    end
  end
end

.convert_raw_array(array) ⇒ Object



40
41
42
43
44
# File 'lib/array.rb', line 40

def convert_raw_array(array)
  array.map do |element|
    convert_element(element)
  end
end

.from_json(json_object) ⇒ Object



46
47
48
# File 'lib/array.rb', line 46

def from_json(json_object)
  new(json_object)
end

.has_elements(options) ⇒ Object



21
22
23
# File 'lib/array.rb', line 21

def has_elements(options)
  @element_class = options[:class]
end

Instance Method Details

#&(other) ⇒ Object



74
75
76
# File 'lib/array.rb', line 74

def &(other)
  self.class.new(@elements & other.elements)
end

#+(array) ⇒ Object



86
87
88
# File 'lib/array.rb', line 86

def +(array)
  self.class.new(@elements + self.class.new(array).elements)
end

#-(other) ⇒ Object



70
71
72
# File 'lib/array.rb', line 70

def -(other)
  self.class.new(@elements - other.elements)
end

#<<(element) ⇒ Object



82
83
84
# File 'lib/array.rb', line 82

def <<(element)
  @elements << self.class.convert_element(element)
end

#==(other) ⇒ Object Also known as: eql?



57
58
59
# File 'lib/array.rb', line 57

def ==(other)
  self.class == other.class && @elements == other.elements
end

#as_jsonObject



94
95
96
97
98
99
100
101
102
# File 'lib/array.rb', line 94

def as_json
  @elements.map do |element|
    if element.is_a?(Machinery::Array) || element.is_a?(Machinery::Object)
      element.as_json
    else
      element
    end
  end
end

#compare_with(other) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/array.rb', line 104

def compare_with(other)
  [
    self - other,
    other - self,
    self & other
  ].map { |e| !e.empty? ? e : nil }
end

#hashObject



66
67
68
# File 'lib/array.rb', line 66

def hash
  @elements.hash
end

#insert(index, *elements) ⇒ Object



90
91
92
# File 'lib/array.rb', line 90

def insert(index, *elements)
  @elements.insert(index, *self.class.new(elements).elements)
end

#push(element) ⇒ Object



78
79
80
# File 'lib/array.rb', line 78

def push(element)
  @elements.push(self.class.convert_element(element))
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/array.rb', line 116

def respond_to?(name, include_all = false)
  super || @elements.respond_to?(name, include_all)
end