Class: Parliament::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/parliament/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Response

Returns a new instance of Response


10
11
12
# File 'lib/parliament/response.rb', line 10

def initialize(nodes)
  @nodes = nodes
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes


7
8
9
# File 'lib/parliament/response.rb', line 7

def nodes
  @nodes
end

Instance Method Details

#build_responses(filtered_objects) ⇒ Object


29
30
31
32
33
34
35
36
# File 'lib/parliament/response.rb', line 29

def build_responses(filtered_objects)
  result = []

  filtered_objects.each do |objects|
    result << Parliament::Response.new(objects)
  end
  result
end

#filter(*types) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parliament/response.rb', line 14

def filter(*types)
  filtered_objects = Array.new(types.size) { [] }

  unless types.empty?
    @nodes.each do |node|
      type_index = types.index(node.type)
      filtered_objects[type_index] << node unless type_index.nil?
    end
  end

  result = build_responses(filtered_objects)

  types.size == 1 ? result.first : result
end

#reverse_sort_by(*parameters) ⇒ Object


45
46
47
48
49
50
# File 'lib/parliament/response.rb', line 45

def reverse_sort_by(*parameters)
  Parliament::Utils.reverse_sort_by({
    list:       @nodes,
    parameters: parameters
  })
end

#sort_by(*parameters) ⇒ Object


38
39
40
41
42
43
# File 'lib/parliament/response.rb', line 38

def sort_by(*parameters)
  Parliament::Utils.sort_by({
    list:       @nodes,
    parameters: parameters
  })
end