parliament-ntriple 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3dd09c5be5ce20cb330d49d4965a3a35b48731c
4
- data.tar.gz: 611b84fb8051553bd8b0a6e536ff030e6f9759a7
3
+ metadata.gz: c28f49cd480518c28176b043002dacb7074eb57d
4
+ data.tar.gz: a1039e64e1bc609d59ea45e1df745dd9512a1e1d
5
5
  SHA512:
6
- metadata.gz: 132bfbaec83772496778823c557aa4577764acf3d16a9e3e868107cb4cae4cc72fdfc028c7143647fa1c82d0a2853d9c36dcf32f57030ce64abed1452e8b85d3
7
- data.tar.gz: 241fb9a82e78f62cb3d53614ae3f40890f8671c09d27fa62db0f067b1d744a67d0460a498e3ce503fceb71a2f8412a03883e346f31e752889fd854bbdc0cb101
6
+ metadata.gz: e5fd10da414857335e4387c5e79bb41458321c41ce699d6f97e7f07a7fb8d42f593803c834138f14fcbd7de65a2bbaf6078e19da48766b7b31d88190d2c1bbb6
7
+ data.tar.gz: 87eb07680ee2a8ef2bc2c561f803efcfb479ebd8121cfd86e7cc40a6564448478bba386964061042362042ec4f5cb3db124a2ec5049bb305f627662cca331814
@@ -94,6 +94,46 @@ module Parliament
94
94
  { prepend_rejected: true }
95
95
  end
96
96
 
97
+ # Sort an array of objects in ascending or descending order.
98
+ #
99
+ # @example Sorting a list objects by count (descending) and name (ascending)
100
+ # response = parliament.houses('123').parties.current.get.filter('http://id.ukpds.org/schema/Party')
101
+ #
102
+ # args = {
103
+ # list: response.nodes,
104
+ # parameters: { count: :desc, name: :asc }
105
+ # }
106
+ #
107
+ # sorted_list = Parliament::NTriple::Util.multi_direction_sort(args)
108
+ #
109
+ # sorted_list.each{ |party| p "#{party.name} - #{party.count}" }
110
+ # # http://id.ukpds.org/1837 - Conservative - 220
111
+ # # http://id.ukpds.org/3824 - Labour - 220
112
+ # # http://id.ukpds.org/7283 - Green Party - 1
113
+ # # http://id.ukpds.org/2837 - Independent Liberal Democrat - 1
114
+ # # http://id.ukpds.org/3726 - Plaid Cymru - 1
115
+ #
116
+ # @param [Hash] args a hash of arguments.
117
+ # @option args [Array<Object>] :list the 'list' which we are sorting.
118
+ # @option args [Hash<Symbol>,<Symbol>] :parameters a hash of parameters to sort by as keys and the sort direction as values.
119
+ # @option args [Boolean] :prepend_rejected (true) should objects that do not respond to our parameters be prepended?
120
+ #
121
+ # @return [Array<Object>] a sorted array of objects using the args passed in.
122
+ def self.multi_direction_sort(args)
123
+ rejected = []
124
+ args = sort_defaults.merge(args)
125
+
126
+ list = args[:list].dup
127
+ sort_directions = args[:parameters]
128
+
129
+ list, rejected = prune_list(list, rejected, sort_directions.keys)
130
+
131
+ list = multi_sort_list(list, sort_directions)
132
+
133
+ # Any rejected (nil) values will be added to the start of the result unless specified otherwise
134
+ args[:prepend_rejected] ? rejected.concat(list) : list.concat(rejected)
135
+ end
136
+
97
137
  # @!method self.prune_list(list, rejected, parameters)
98
138
  # Prune all objects that do not respond to a given array of parameters.
99
139
  #
@@ -122,7 +162,7 @@ module Parliament
122
162
  # @!visibility private
123
163
  #
124
164
  # @param [Array<Object>] list the 'list' of objects we are pruning from.
125
- # @param [Array<Symbol>] parameters an array of parameters we are checking.
165
+ # @param [Array<Symbol>] parameters a hash of parameters to sort by as keys and the sort direction as values.
126
166
  #
127
167
  # @return [Array<Object>] our sorted list.
128
168
  private_class_method def self.sort_list(list, parameters)
@@ -132,6 +172,29 @@ module Parliament
132
172
  end
133
173
  end
134
174
  end
175
+
176
+ # @!method self.multi_sort_list(list, sort_directions)
177
+ # Sort a given list of objects by a list of parameters and their sort directions.
178
+ #
179
+ # @private
180
+ # @!scope class
181
+ # @!visibility private
182
+ #
183
+ # @param [Array<Object>] list the 'list' of objects we are pruning from.
184
+ # @param [Hash<Symbol>,<Symbol>] parameters an array of parameters we are checking.
185
+ #
186
+ # @return [Array<Object>] our sorted list.
187
+ private_class_method def self.multi_sort_list(list, sort_directions)
188
+ directions_hash = { asc: 1, desc: -1 }
189
+
190
+ list.sort do |obj1, obj2|
191
+ sort_values = sort_directions.map do |method_name, direction|
192
+ directions_hash[direction] * (obj1.send(method_name) <=> obj2.send(method_name))
193
+ end
194
+
195
+ sort_values.find { |value| value != 0 } || 0
196
+ end
197
+ end
135
198
  end
136
199
  end
137
200
  end
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module NTriple
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.1.2'.freeze
4
4
  end
5
5
  end
@@ -26,7 +26,7 @@ module Parliament
26
26
  #
27
27
  # Note: this method assumes all of your nodes include a #type attribute or are blank nodes.
28
28
  #
29
- # @since 0.2.0
29
+ # @since 0.1.0
30
30
  #
31
31
  # @example Filtering for a single type
32
32
  # node_1 = Grom::Node.new
@@ -94,7 +94,7 @@ module Parliament
94
94
  #
95
95
  # @see Parliament::Utils.sort_by
96
96
  #
97
- # @since 0.5.0
97
+ # @since 0.1.0
98
98
  #
99
99
  # @param [Array<Symbol>] parameters Attributes to sort on - left to right.
100
100
  # @return [Array<Grom::Node>] A sorted array of nodes.
@@ -107,11 +107,28 @@ module Parliament
107
107
  )
108
108
  end
109
109
 
110
+ # Sort the Parliament::Response nodes in ascending or descending order by a set of attributes on each node.
111
+ #
112
+ # @see Parliament::Utils.multi_direction_sort
113
+ #
114
+ # @since 0.1.2
115
+ #
116
+ # @param [Hash<Symbol,Symbol>] parameters Attributes to sort on (left to right) - and their direction of sort(asc or desc).
117
+ # @return [Array<Grom::Node>] A sorted array of nodes.
118
+ def multi_direction_sort(parameters)
119
+ Parliament::NTriple::Utils.multi_direction_sort(
120
+ {
121
+ list: @nodes,
122
+ parameters: parameters
123
+ }
124
+ )
125
+ end
126
+
110
127
  # Sort the Parliament::Response nodes in descending order by a set of attributes on each node.
111
128
  #
112
129
  # @see Parliament::Utils.reverse_sort_by
113
130
  #
114
- # @since 0.5.0
131
+ # @since 0.1.0
115
132
  #
116
133
  # @param [Array<Symbol>] parameters Attributes to sort on - left to right.
117
134
  # @return [Array<Grom::Node>] A sorted array of nodes.
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'simplecov', '~> 0.12'
30
30
  spec.add_development_dependency 'vcr', '~> 3.0'
31
31
  spec.add_development_dependency 'webmock', '~> 2.3'
32
- spec.add_development_dependency 'parliament-grom-decorators', '~> 0.1.0'
33
- spec.add_development_dependency 'parliament-ruby', '~> 0.7.6'
32
+ spec.add_development_dependency 'parliament-grom-decorators', '~> 0.1'
33
+ spec.add_development_dependency 'parliament-ruby', '~> 0.7'
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-ntriple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-02 00:00:00.000000000 Z
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grom
@@ -128,28 +128,28 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 0.1.0
131
+ version: '0.1'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 0.1.0
138
+ version: '0.1'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: parliament-ruby
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.7.6
145
+ version: '0.7'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.7.6
152
+ version: '0.7'
153
153
  description: Parliamentary NTriple response builder
154
154
  email:
155
155
  - rklappleyard@gmail.com