Class: Machinery::Array

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

Direct Known Subclasses

FileScope

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = [], attributes = {}) ⇒ Array

Returns a new instance of Array.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/array.rb', line 45

def initialize(elements = [], attributes = {})
  attributes ||= {}
  @attribute_keys = self.class.attribute_keys || []

  unknown_attributes = attributes.keys.map(&:to_s) - @attribute_keys
  if unknown_attributes.length > 0
    raise RuntimeError, "Unknown properties: #{unknown_attributes.join(",")}"
  end

  @attributes = {}
  attributes.each do |k, v|
    @attributes[k.to_s] = v
  end
  @elements = convert_raw_array(elements)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/array.rb', line 179

def method_missing(name, *args, &block)
  name = name.to_s

  if @attribute_keys.include?(name)
    @attributes[name]
  elsif name.end_with?("=") and @attribute_keys.include?(name[0..-2])
    @attributes[name[0..-2]] = args.first
  else
    @elements.send(name, *args, &block)
  end
end

Class Attribute Details

.attribute_keysObject (readonly)

Returns the value of attribute attribute_keys.



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

def attribute_keys
  @attribute_keys
end

.element_classesObject (readonly)

Returns the value of attribute element_classes.



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

def element_classes
  @element_classes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#elementsObject (readonly)

Returns the value of attribute elements.



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

def elements
  @elements
end

#scopeObject

Returns the value of attribute scope.



43
44
45
# File 'lib/array.rb', line 43

def scope
  @scope
end

Class Method Details

.from_json(json_object) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/array.rb', line 32

def from_json(json_object)
  if json_object.is_a?(::Array)
    new(json_object)
  else
    new(json_object["_elements"], json_object["_attributes"])
  end
end

.has_attributes(*keys) ⇒ Object



23
24
25
# File 'lib/array.rb', line 23

def has_attributes(*keys)
  @attribute_keys = keys.map(&:to_s)
end

.has_elements(options) ⇒ Object



27
28
29
30
# File 'lib/array.rb', line 27

def has_elements(options)
  @element_classes ||= []
  @element_classes << options
end

Instance Method Details

#&(other) ⇒ Object



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

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

#+(array) ⇒ Object



132
133
134
# File 'lib/array.rb', line 132

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

#-(other) ⇒ Object



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

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

#<<(element) ⇒ Object



128
129
130
# File 'lib/array.rb', line 128

def <<(element)
  @elements << convert_element(element)
end

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



103
104
105
# File 'lib/array.rb', line 103

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

#as_jsonObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/array.rb', line 144

def as_json
  object = {}

  object["_attributes"] = @attributes unless @attributes.empty?
  object["_elements"] = @elements.map do |element|
    if element.is_a?(Machinery::Array) || element.is_a?(Machinery::Object)
      element.as_json
    else
      element
    end
  end

  object
end

#compare_with(other) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/array.rb', line 159

def compare_with(other)
  only_self = self.class.new(self.elements - other.elements)
  only_other = self.class.new(other.elements - self.elements)
  common = self.class.new(self.elements & other.elements)

  if self.attributes == other.attributes
    common.attributes = self.attributes
  else
    only_self.attributes = self.attributes
    only_other.attributes = other.attributes
  end

  [
    only_self,
    only_other,
    Machinery::Array.new,
    common
  ].map { |e| (e.empty? && (e.is_a?(Machinery::Array) && e.attributes.empty?)) ? nil : e }
end

#convert_element(element) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/array.rb', line 61

def convert_element(element)
  (self.class.element_classes || []).each do |definition|
    condition = definition[:if]
    klass = definition[:class]
    if condition
      next if condition.any? do |key, value|
        @attributes[key.to_s] != value
      end
    end

    return element.is_a?(klass) ? element : klass.from_json(element)
  end

  case element
  when Hash
    if element.keys.include?("_elements")
      Machinery::Array.from_json(element)
    else
      Machinery::Object.from_json(element)
    end
  else
    element
  end
end

#convert_raw_array(array) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/array.rb', line 140

def empty?
  @elements.empty?
end

#hashObject



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

def hash
  @elements.hash
end

#insert(index, *elements) ⇒ Object



136
137
138
# File 'lib/array.rb', line 136

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

#push(element) ⇒ Object



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

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

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

Returns:

  • (Boolean)


191
192
193
# File 'lib/array.rb', line 191

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

#to_sObject



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

def to_s
  @elements.empty? ? "(empty)" : @elements.join(",")
end