Class: Sortiri::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sortiri/parser.rb

Constant Summary collapse

DELIMITER =
','

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sortable_columns:, sort_string:) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
# File 'lib/sortiri/parser.rb', line 9

def initialize(sortable_columns:, sort_string:)
  @sortable_columns = sortable_columns # Array of Sortiri::ActiveRecord::Column
  @sort_string = sort_string
end

Instance Attribute Details

#sort_stringObject (readonly)

Returns the value of attribute sort_string.



7
8
9
# File 'lib/sortiri/parser.rb', line 7

def sort_string
  @sort_string
end

#sortable_columnsObject (readonly)

Returns the value of attribute sortable_columns.



7
8
9
# File 'lib/sortiri/parser.rb', line 7

def sortable_columns
  @sortable_columns
end

Class Method Details

.parse(sort_string:) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sortiri/parser.rb', line 22

def self.parse(sort_string:)
  return [] if sort_string.blank?

  sort_string.split(DELIMITER).map do |s|
    Sortiri::Column.new(column: s)
  end
end

Instance Method Details

#whitelisted_columnsObject



14
15
16
17
18
19
20
# File 'lib/sortiri/parser.rb', line 14

def whitelisted_columns
  columns = self.class.parse(sort_string: sort_string)

  columns.select do |column|
    sortable_columns.any? { |c| c.matches_with?(column) }
  end
end