Class: NextPage::Sort::SegmentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/next_page/sort/segment_parser.rb

Overview

Segment Parser

Parses each sort segment to provide direction, associations, and name.

Constant Summary collapse

SEGMENT_REGEX =
/(?<sign>[+|-]?)(?<names>.+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment) ⇒ SegmentParser

Returns a new instance of SegmentParser.



13
14
15
16
17
18
# File 'lib/next_page/sort/segment_parser.rb', line 13

def initialize(segment)
  @segment = segment
  parsed = segment.match SEGMENT_REGEX
  @direction = parsed['sign'] == '-' ? 'desc' : 'asc'
  *@associations, @name = *parsed['names'].split('.')
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



9
10
11
# File 'lib/next_page/sort/segment_parser.rb', line 9

def associations
  @associations
end

#directionObject (readonly)

Returns the value of attribute direction.



9
10
11
# File 'lib/next_page/sort/segment_parser.rb', line 9

def direction
  @direction
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/next_page/sort/segment_parser.rb', line 9

def name
  @name
end

Instance Method Details

#attribute_with_directionObject



24
25
26
# File 'lib/next_page/sort/segment_parser.rb', line 24

def attribute_with_direction
  { @name => @direction }
end

#nested?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/next_page/sort/segment_parser.rb', line 28

def nested?
  @associations.present?
end

#to_sObject



20
21
22
# File 'lib/next_page/sort/segment_parser.rb', line 20

def to_s
  @segment
end