Class: Drillbit::Parameters::Sort

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

Constant Summary collapse

DESCENDING_PREFIX =
'-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_parameters) ⇒ Sort

Returns a new instance of Sort.



10
11
12
# File 'lib/drillbit/parameters/sort.rb', line 10

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.



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

def raw_parameters
  @raw_parameters
end

Instance Method Details

#present?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/drillbit/parameters/sort.rb', line 14

def present?
  raw_parameters.any?
end

#to_aObject



22
23
24
25
26
27
28
29
30
# File 'lib/drillbit/parameters/sort.rb', line 22

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



18
19
20
# File 'lib/drillbit/parameters/sort.rb', line 18

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