Class: RestfulQuery::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/restful_query/parser.rb', line 5

def initialize(query, options = {})
  @options         = options || {}
  @exclude_columns = options[:exclude_columns] ? [options.delete(:exclude_columns)].flatten.collect {|c| c.to_s } : []
  @integer_columns = options[:integer_columns] ? [options.delete(:integer_columns)].flatten.collect {|c| c.to_s } : []
  @default_sort    = options[:default_sort] ? [Sort.parse(options[:default_sort])] : []
  @single_sort     = options[:single_sort] || false
  @query           = (query || {}).dup
  @default_join    = @query.delete(:join) || :and
  extract_sorts_from_conditions
  map_conditions
end

Instance Attribute Details

#exclude_columnsObject (readonly)

Returns the value of attribute exclude_columns.



3
4
5
# File 'lib/restful_query/parser.rb', line 3

def exclude_columns
  @exclude_columns
end

#integer_columnsObject (readonly)

Returns the value of attribute integer_columns.



3
4
5
# File 'lib/restful_query/parser.rb', line 3

def integer_columns
  @integer_columns
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/restful_query/parser.rb', line 3

def options
  @options
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/restful_query/parser.rb', line 3

def query
  @query
end

Class Method Details

.sorts_from_hash(sorts) ⇒ Object



48
49
50
51
# File 'lib/restful_query/parser.rb', line 48

def self.sorts_from_hash(sorts)
  sort_conditions = [sorts].flatten.compact
  sort_conditions.collect {|c| Sort.parse(c) }
end

Instance Method Details

#clear_default_sort!Object



91
92
93
# File 'lib/restful_query/parser.rb', line 91

def clear_default_sort!
  @sorts.reject! {|s| s == @default_sort.first }
end

#conditionsObject



17
18
19
# File 'lib/restful_query/parser.rb', line 17

def conditions
  conditions_hash.values.flatten
end

#conditions_for(column) ⇒ Object



25
26
27
# File 'lib/restful_query/parser.rb', line 25

def conditions_for(column)
  conditions_hash[column.to_s]
end

#has_conditions?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/restful_query/parser.rb', line 21

def has_conditions?
  !conditions.empty?
end

#has_sort?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/restful_query/parser.rb', line 57

def has_sort?
  !sorts.empty?
end

#set_sort(column, direction) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/restful_query/parser.rb', line 77

def set_sort(column, direction)
  if new_sort = self.sort(column)
    if direction.nil?
      self.sorts.reject! {|s| s.column == column.to_s }
    else
      new_sort.direction = direction
    end
  else
    new_sort = Sort.new(column, direction)
    self.sorts << new_sort
  end
  new_sort
end

#sort(column) ⇒ Object



73
74
75
# File 'lib/restful_query/parser.rb', line 73

def sort(column)
  sorts.detect {|s| s && s.column == column.to_s }
end

#sort_sqlObject



53
54
55
# File 'lib/restful_query/parser.rb', line 53

def sort_sql
  @sorts.collect {|s| s.to_sql }.join(', ')
end

#sorted_by?(column) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/restful_query/parser.rb', line 69

def sorted_by?(column)
  sorted_columns.include?(column.to_s)
end

#sorted_columnsObject



65
66
67
# File 'lib/restful_query/parser.rb', line 65

def sorted_columns
  sorts.collect {|s| s.column }
end

#sortsObject



61
62
63
# File 'lib/restful_query/parser.rb', line 61

def sorts
  @sorts ||= []
end

#to_conditions_array(join = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/restful_query/parser.rb', line 29

def to_conditions_array(join = nil)
  join ||= @default_join
  join_string = (join == :or) ? ' OR ' : ' AND '
  conditions_string = []
  conditions_values = []
  conditions.each do |c| 
    ca = c.to_condition_array
    conditions_string << ca[0]
    conditions_values << ca[1]
  end
  conditions_values.unshift(conditions_string.join(join_string))
end

#to_query_hashObject



42
43
44
45
46
# File 'lib/restful_query/parser.rb', line 42

def to_query_hash
  hash = @query
  hash['_sort'] = sorts.collect {|s| s.to_s } unless sorts.empty?
  hash
end