Class: LibyearRb::SortColumn

Inherits:
Data
  • Object
show all
Defined in:
lib/libyear_rb/sorter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction

Returns:

  • (Object)

    the current value of direction



4
5
6
# File 'lib/libyear_rb/sorter.rb', line 4

def direction
  @direction
end

#fieldObject (readonly)

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



4
5
6
# File 'lib/libyear_rb/sorter.rb', line 4

def field
  @field
end

Instance Method Details

#compare(a, b) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/libyear_rb/sorter.rb', line 5

def compare(a, b)
  av = a.public_send(field)
  bv = b.public_send(field)
  return 0 if av.nil? && bv.nil?
  return 1 if av.nil?
  return -1 if bv.nil?
  (direction == :desc) ? bv <=> av : av <=> bv
end