Module: Sorting::Helpers

Defined in:
lib/sorting/helpers.rb

Overview

Note:

require 'sorting' loads this module

This module provides convenience methods to create Sorting::Ascending and Sorting::Descending instances.

Important: see Helpers.append_to for how to properly use this module with other modules

Constant Summary collapse

MethodDefinitions =

The proc with the method definitions of asc and desc. See append_to for an explanation.

proc {

  # Convenience method to create a Sorting::Ascending instance.
  # Works exactly the same as {Sorting::Ascending#initialize}
  def asc(*args, &lazy)
    Sorting::Ascending.new(*args, &lazy)
  end
  module_function :asc

  # Convenience method to create a Sorting::Descending instance.
  # Works exactly the same as {Sorting::Descending#initialize Ascending#initialize}
  def desc(*args, &lazy)
    Sorting::Descending.new(*args, &lazy)
  end
  module_function :desc
}

Class Method Summary collapse

Class Method Details

.append_to(mod) ⇒ Object

Note:

The methods are stored in the MethodDefinitions proc. That proc is class_eval'ed in in the module given by the mod parameter. It is done that way to avoid an issue with methods of modules which are included in another module not being properly shared

Adds the Sorting::Helpers methods 'asc' and 'desc' to the given module. This method class_eval because modules included into modules don't properly share methods


56
57
58
# File 'lib/sorting/helpers.rb', line 56

def self.append_to(mod)
  mod.class_eval(&MethodDefinitions)
end

.asc(*args, &lazy) ⇒ Object

Note:

This method is a module function, and as such also available as a private instance method.

Convenience method to create a Sorting::Ascending instance. Works exactly the same as Ascending#initialize.


# File 'lib/sorting/helpers.rb', line 62

.desc(*args, &lazy) ⇒ Object

Note:

This method is a module function, and as such also available as a private instance method.

Convenience method to create a Sorting::Descending instance. Works exactly the same as Descending#initialize.


# File 'lib/sorting/helpers.rb', line 71