Class: QueryFilters::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/query_filters/parser.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  nesting_separator: ':',
  assignment_separator: '=',
  filters_separator: ',',
  symbolize_keys: true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
# File 'lib/query_filters/parser.rb', line 19

def initialize(query, options = {})
  @query = query
  @options = DEFAULT_OPTIONS.merge(options)
  parse
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



15
16
17
# File 'lib/query_filters/parser.rb', line 15

def filters
  @filters
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/query_filters/parser.rb', line 15

def options
  @options
end

#queryObject (readonly)

Returns the value of attribute query.



15
16
17
# File 'lib/query_filters/parser.rb', line 15

def query
  @query
end

Instance Method Details

#parseself

Returns:

  • (self)


26
27
28
29
30
31
# File 'lib/query_filters/parser.rb', line 26

def parse
  @filters = query_string.split(options[:filters_separator]).map do |filter|
    parse_filter(filter)
  end
  self
end

#to_hashHash

Returns:

  • (Hash)


34
35
36
37
38
# File 'lib/query_filters/parser.rb', line 34

def to_hash
  filters_hash = {}
  filters.each { |filter| filters_hash.deep_merge!(filter) }
  filters_hash
end