Class: RestfulQuery::Parser
- Inherits:
-
Object
- Object
- RestfulQuery::Parser
- Defined in:
- lib/restful_query/parser.rb
Instance Attribute Summary collapse
-
#default_sort_options ⇒ Object
readonly
Returns the value of attribute default_sort_options.
-
#exclude_columns ⇒ Object
readonly
Returns the value of attribute exclude_columns.
-
#integer_columns ⇒ Object
readonly
Returns the value of attribute integer_columns.
-
#map_columns ⇒ Object
readonly
Returns the value of attribute map_columns.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #clear_default_sort! ⇒ Object
- #conditions ⇒ Object
- #conditions_for(column) ⇒ Object
- #has_conditions? ⇒ Boolean
- #has_sort? ⇒ Boolean
-
#initialize(query, options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #set_sort(column_name, direction) ⇒ Object
- #sort(column_name) ⇒ Object
- #sort_sql ⇒ Object
- #sorted_by?(column_name) ⇒ Boolean
- #sorted_columns ⇒ Object
- #sorts ⇒ Object
- #to_conditions_array(join = nil) ⇒ Object
- #to_query_hash ⇒ Object
Constructor Details
#initialize(query, options = {}) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/restful_query/parser.rb', line 5 def initialize(query, = {}) = || {} @exclude_columns = (:exclude, ) @integer_columns = (:integer, ) @map_columns = [:map_columns] || {} @single_sort = [:single_sort] || true = [:sort_options] || {} @query = (!query || query.empty? || query.to_s =~ /^\s*$/ ? {} : query).dup @default_sort = [:default_sort] ? [make_sort([:default_sort])] : [] @default_join = @query.delete(:join) || :and extract_sorts_from_conditions map_conditions end |
Instance Attribute Details
#default_sort_options ⇒ Object (readonly)
Returns the value of attribute default_sort_options.
3 4 5 |
# File 'lib/restful_query/parser.rb', line 3 def end |
#exclude_columns ⇒ Object (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_columns ⇒ Object (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 |
#map_columns ⇒ Object (readonly)
Returns the value of attribute map_columns.
3 4 5 |
# File 'lib/restful_query/parser.rb', line 3 def map_columns @map_columns end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/restful_query/parser.rb', line 3 def end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/restful_query/parser.rb', line 3 def query @query 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 |
#conditions ⇒ Object
19 20 21 |
# File 'lib/restful_query/parser.rb', line 19 def conditions conditions_hash.values.flatten end |
#conditions_for(column) ⇒ Object
27 28 29 |
# File 'lib/restful_query/parser.rb', line 27 def conditions_for(column) conditions_hash[column.to_s] end |
#has_conditions? ⇒ Boolean
23 24 25 |
# File 'lib/restful_query/parser.rb', line 23 def has_conditions? !conditions.empty? end |
#has_sort? ⇒ Boolean
54 55 56 |
# File 'lib/restful_query/parser.rb', line 54 def has_sort? !sorts.empty? end |
#set_sort(column_name, direction) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/restful_query/parser.rb', line 76 def set_sort(column_name, direction) column = map_column(column_name) if new_sort = self.sort(column_name) if direction.nil? self.sorts.reject! {|s| s.column == column.to_s } else new_sort.direction = direction end else new_sort = make_sort(column, direction) self.sorts << new_sort end new_sort end |
#sort(column_name) ⇒ Object
71 72 73 74 |
# File 'lib/restful_query/parser.rb', line 71 def sort(column_name) column = map_column(column_name) sorts.detect {|s| s && s.column == column } end |
#sort_sql ⇒ Object
50 51 52 |
# File 'lib/restful_query/parser.rb', line 50 def sort_sql @sorts.collect {|s| s.to_sql }.join(', ') end |
#sorted_by?(column_name) ⇒ Boolean
66 67 68 69 |
# File 'lib/restful_query/parser.rb', line 66 def sorted_by?(column_name) column = map_column(column_name) sorted_columns.include?(column.to_s) end |
#sorted_columns ⇒ Object
62 63 64 |
# File 'lib/restful_query/parser.rb', line 62 def sorted_columns sorts.collect {|s| s.column } end |
#sorts ⇒ Object
58 59 60 |
# File 'lib/restful_query/parser.rb', line 58 def sorts @sorts ||= [] end |
#to_conditions_array(join = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/restful_query/parser.rb', line 31 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_hash ⇒ Object
44 45 46 47 48 |
# File 'lib/restful_query/parser.rb', line 44 def to_query_hash hash = @query hash['_sort'] = sorts.collect {|s| s.to_s } unless sorts.empty? hash end |