Class: NextPage::Sort::SegmentParser
- Inherits:
-
Object
- Object
- NextPage::Sort::SegmentParser
- 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
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #attribute_with_direction ⇒ Object
-
#initialize(segment) ⇒ SegmentParser
constructor
A new instance of SegmentParser.
- #nested? ⇒ Boolean
- #to_s ⇒ Object
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
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
9 10 11 |
# File 'lib/next_page/sort/segment_parser.rb', line 9 def associations @associations end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
9 10 11 |
# File 'lib/next_page/sort/segment_parser.rb', line 9 def direction @direction end |
#name ⇒ Object (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_direction ⇒ Object
24 25 26 |
# File 'lib/next_page/sort/segment_parser.rb', line 24 def attribute_with_direction { @name => @direction } end |
#nested? ⇒ Boolean
28 29 30 |
# File 'lib/next_page/sort/segment_parser.rb', line 28 def nested? @associations.present? end |
#to_s ⇒ Object
20 21 22 |
# File 'lib/next_page/sort/segment_parser.rb', line 20 def to_s @segment end |