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.



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

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



120
121
122
# File 'lib/array.rb', line 120

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

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
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



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

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

#+(array) ⇒ Object



94
95
96
# File 'lib/array.rb', line 94

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

#-(other) ⇒ Object



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

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

#<<(element) ⇒ Object



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

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

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



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

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

#as_jsonObject



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

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



112
113
114
115
116
117
118
# File 'lib/array.rb', line 112

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

#hashObject



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

def hash
  @elements.hash
end

#insert(index, *elements) ⇒ Object



98
99
100
# File 'lib/array.rb', line 98

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

#push(element) ⇒ Object



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

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

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/array.rb', line 124

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