Module: Parliament::NTriple::Utils

Defined in:
lib/parliament/ntriple/utils.rb

Overview

Namespace for helper methods used with parliament-ruby.

Since:

  • 0.6.0

Class Method Summary collapse

Class Method Details

.multi_direction_sort(args) ⇒ Array<Object>

Sort an array of objects in ascending or descending order.

Examples:

Sorting a list objects by count (descending) and name (ascending)

response = parliament.houses('123').parties.current.get.filter('http://id.ukpds.org/schema/Party')

args = {
  list: response.nodes,
  parameters: { count: :desc, name: :asc }
}

sorted_list = Parliament::NTriple::Util.multi_direction_sort(args)

sorted_list.each{ |party| p "#{party.name} - #{party.count}" }
# http://id.ukpds.org/1837 - Conservative - 220
# http://id.ukpds.org/3824 - Labour - 220
# http://id.ukpds.org/7283 - Green Party - 1
# http://id.ukpds.org/2837 - Independent Liberal Democrat - 1
# http://id.ukpds.org/3726 - Plaid Cymru - 1

Parameters:

  • args (Hash)

    a hash of arguments.

Options Hash (args):

  • :list (Array<Object>)

    the ‘list’ which we are sorting.

  • :parameters (Hash<Symbol>, <Symbol>)

    a hash of parameters to sort by as keys and the sort direction as values.

  • :prepend_rejected (Boolean) — default: true

    should objects that do not respond to our parameters be prepended?

Returns:

  • (Array<Object>)

    a sorted array of objects using the args passed in.

Since:

  • 0.6.0



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/parliament/ntriple/utils.rb', line 125

def self.multi_direction_sort(args)
  rejected = []
  args = sort_defaults.merge(args)

  list = args[:list].dup
  sort_directions = args[:parameters]

  list, rejected = prune_list(list, rejected, sort_directions.keys, nil)

  list = multi_sort_list(list, sort_directions)

  # Any rejected (nil) values will be added to the start of the result unless specified otherwise
  args[:prepend_rejected] ? rejected.concat(list) : list.concat(rejected)
end

.reverse_sort_by(args) ⇒ Array<Object>

Sort an Array of Objects in descending order. Largely, this implementation runs Parliament::NTriple::Utils.sort_by and calls reverse! on the result.

Examples:

Sorting a list of objects by date

response = parliament.people('123').get.filter('http://id.ukpds.org/schema/Person')

objects = response.first.incumbencies

args = {
  list: objects,
  parameters: [:endDate],
  prepend_rejected: false
}

sorted_list = Parliament::NTriple::Util.reverse_sort_by(args)

sorted_list.each { |incumbency| puts incumbency.respond_to?(:endDate) ? incumbency.endDate : 'Current' }
# http://id.ukpds.org/3141 - Current
# http://id.ukpds.org/9101 - 2011-09-04
# http://id.ukpds.org/1234 - 1997-01-01
# http://id.ukpds.org/5678 - 1991-03-15
# http://id.ukpds.org/1121 - 1981-07-31

Parameters:

  • args (Hash)

    a hash of arguments.

Options Hash (args):

  • :list (Array<Object>)

    the ‘list’ which we are sorting.

  • :parameters (Array<Symbol>)

    an array of parameters we are sorting by.

  • :block (Proc)

    a block used to sort.

  • :prepend_rejected (Boolean) — default: true

    should objects that do not respond to our parameters be prepended?

Returns:

  • (Array<Object>)

    a sorted array of objects using the args passed in.

See Also:

Since:

  • 0.6.0



86
87
88
# File 'lib/parliament/ntriple/utils.rb', line 86

def self.reverse_sort_by(args)
  Parliament::NTriple::Utils.sort_by(args).reverse!
end

.sort_by(args) ⇒ Array<Object>

Sort an Array of Objects in ascending order. The major difference between this implementation of sort_by and the standard one is that our implementation includes objects that return nil for our parameter values.

Examples:

Sorting a list of objects by date

response = parliament.people('123').get.filter('http://id.ukpds.org/schema/Person')

objects = response.first.incumbencies

args = {
  list: objects,
  parameters: [:endDate],
  prepend_rejected: false
}

sorted_list = Parliament::NTriple::Util.sort_by(args)

sorted_list.each { |incumbency| puts incumbency.respond_to?(:endDate) ? incumbency.endDate : 'Current' }
# http://id.ukpds.org/1121 - 1981-07-31
# http://id.ukpds.org/5678 - 1991-03-15
# http://id.ukpds.org/1234 - 1997-01-01
# http://id.ukpds.org/9101 - 2011-09-04
# http://id.ukpds.org/3141 - Current

Parameters:

  • args (Hash)

    a hash of arguments.

Options Hash (args):

  • :list (Array<Object>)

    the ‘list’ which we are sorting.

  • :parameters (Array<Symbol>)

    an array of parameters we are sorting by.

  • :block (Proc)

    a block used to sort.

  • :prepend_rejected (Boolean) — default: true

    should objects that do not respond to our parameters be prepended?

Returns:

  • (Array<Object>)

    a sorted array of objects using the args passed in.

See Also:

Since:

  • 0.6.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/parliament/ntriple/utils.rb', line 39

def self.sort_by(args)
  rejected = []
  args = sort_defaults.merge(args)
  list = args[:list].dup
  parameters = args[:parameters]
  block = args[:block]

  list, rejected = prune_list(list, rejected, parameters, block)

  list = sort_list(list, parameters, block)

  # Any rejected (nil) values will be added to the start of the result unless specified otherwise
  args[:prepend_rejected] ? rejected.concat(list) : list.concat(rejected)
end

.sort_defaultsHash

Default arguments hash for #sort_by and #reverse_sort_by.

Returns:

  • (Hash)

    default arguments used in sorting methods.

See Also:

Since:

  • 0.6.0



96
97
98
# File 'lib/parliament/ntriple/utils.rb', line 96

def self.sort_defaults
  { prepend_rejected: true }
end