Class: Apill::Parameters::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/apill/parameters/sort.rb

Constant Summary collapse

DESCENDING_PREFIX =
'-'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_parameters) ⇒ Sort

Returns a new instance of Sort.



8
9
10
# File 'lib/apill/parameters/sort.rb', line 8

def initialize(raw_parameters)
  self.raw_parameters = raw_parameters ? raw_parameters.split(',') : []
end

Instance Attribute Details

#raw_parametersObject

Returns the value of attribute raw_parameters.



6
7
8
# File 'lib/apill/parameters/sort.rb', line 6

def raw_parameters
  @raw_parameters
end

Instance Method Details

#present?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/apill/parameters/sort.rb', line 12

def present?
  raw_parameters.any?
end

#to_aObject



20
21
22
23
24
25
26
27
28
# File 'lib/apill/parameters/sort.rb', line 20

def to_a
  @to_a ||= raw_parameters.map do |raw_parameter|
    if raw_parameter.start_with?(DESCENDING_PREFIX)
      [raw_parameter[1..-1], 'desc']
    else
      [raw_parameter, 'asc']
    end
  end
end

#to_hObject



16
17
18
# File 'lib/apill/parameters/sort.rb', line 16

def to_h
  @to_h ||= Hash[to_a]
end