Class: Margrid::Sorter

Inherits:
Struct
  • Object
show all
Defined in:
lib/margrid/components.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, direction) ⇒ Sorter

Returns a new instance of Sorter.



3
4
5
# File 'lib/margrid/components.rb', line 3

def initialize(column, direction)
  super column.to_s, direction.to_s
end

Instance Attribute Details

#columnObject

Returns the value of attribute column

Returns:

  • (Object)

    the current value of column



2
3
4
# File 'lib/margrid/components.rb', line 2

def column
  @column
end

#directionObject

Returns the value of attribute direction

Returns:

  • (Object)

    the current value of direction



2
3
4
# File 'lib/margrid/components.rb', line 2

def direction
  @direction
end

Class Method Details

.load(data) ⇒ Object



23
24
25
# File 'lib/margrid/components.rb', line 23

def self.load(data)
  new data["sort"], data["direction"] if data.key? "sort"
end

Instance Method Details

#apply(relation) ⇒ Object



7
8
9
# File 'lib/margrid/components.rb', line 7

def apply(relation)
  relation.reorder(sorting)
end

#asc?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/margrid/components.rb', line 19

def asc?
  !desc?
end

#desc?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/margrid/components.rb', line 15

def desc?
  direction == "desc"
end

#dumpObject



27
28
29
# File 'lib/margrid/components.rb', line 27

def dump
  {"sort" => column, "direction" => direction}
end

#invertedObject



11
12
13
# File 'lib/margrid/components.rb', line 11

def inverted
  self.class.new(column, asc? ? :desc : :asc)
end