Class: Parliament::Response::NTripleResponse
- Inherits:
-
BaseResponse
- Object
- BaseResponse
- Parliament::Response::NTripleResponse
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/parliament/response/ntriple_response.rb
Overview
N-Triple response object that wraps an Array of Grom::Node objects with common helper methods.
Delegates a number of common methods to the array of Grom::Nodes including, but not limited to, :size, :each, :map, :count etc.
Instance Attribute Summary collapse
-
#nodes ⇒ Array<Grom::Node>
readonly
Graph nodes.
Instance Method Summary collapse
-
#filter(*types) ⇒ Array<Grom::Node> || Array<*Array<Grom::Node>>
Given our array of Grom::Nodes, filter them into arrays of ‘types’ of nodes.
-
#initialize(nodes) ⇒ NTripleResponse
constructor
A new instance of NTripleResponse.
-
#multi_direction_sort(parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in ascending or descending order by a set of attributes on each node.
-
#reverse_sort_by(*parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in descending order by a set of attributes on each node.
-
#sort_by(*parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in ascending order by a set of attributes on each node.
Constructor Details
#initialize(nodes) ⇒ NTripleResponse
Returns a new instance of NTripleResponse.
21 22 23 |
# File 'lib/parliament/response/ntriple_response.rb', line 21 def initialize(nodes) @nodes = nodes end |
Instance Attribute Details
#nodes ⇒ Array<Grom::Node> (readonly)
Graph nodes.
14 15 16 |
# File 'lib/parliament/response/ntriple_response.rb', line 14 def nodes @nodes end |
Instance Method Details
#filter(*types) ⇒ Array<Grom::Node> || Array<*Array<Grom::Node>>
Given our array of Grom::Nodes, filter them into arrays of ‘types’ of nodes.
Note: this method assumes all of your nodes include a #type attribute or are blank nodes.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/parliament/response/ntriple_response.rb', line 77 def filter(*types) filtered_objects = Array.new(types.size) { [] } unless types.empty? @nodes.each do |node| node_types = node.blank? ? Array(::Grom::Node::BLANK) : Array(node.type) indexes = node_types.reduce([]) do |memo, type| index = types.index(type) memo << index if index memo end indexes.each { |index| filtered_objects[index] << node } end end result = build_responses(filtered_objects) types.size == 1 ? result.first : result end |
#multi_direction_sort(parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in ascending or descending order by a set of attributes on each node.
125 126 127 128 129 130 131 132 |
# File 'lib/parliament/response/ntriple_response.rb', line 125 def multi_direction_sort(parameters) Parliament::NTriple::Utils.multi_direction_sort( { list: @nodes, parameters: parameters } ) end |
#reverse_sort_by(*parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in descending order by a set of attributes on each node.
142 143 144 145 146 147 148 149 |
# File 'lib/parliament/response/ntriple_response.rb', line 142 def reverse_sort_by(*parameters) Parliament::NTriple::Utils.reverse_sort_by( { list: @nodes, parameters: parameters } ) end |
#sort_by(*parameters) ⇒ Array<Grom::Node>
Sort the Parliament::Response nodes in ascending order by a set of attributes on each node.
108 109 110 111 112 113 114 115 |
# File 'lib/parliament/response/ntriple_response.rb', line 108 def sort_by(*parameters) Parliament::NTriple::Utils.sort_by( { list: @nodes, parameters: parameters } ) end |