Module: PostJson::QueryMethods

Extended by:
ActiveSupport::Concern
Includes:
ArgumentMethods
Included in:
FinderMethods
Defined in:
lib/post_json/concerns/query_methods.rb

Instance Method Summary collapse

Methods included from ArgumentMethods

#assert_valid_indifferent_keys, #flatten_arguments, #join_arguments

Instance Method Details

#add_query(method, *arguments) ⇒ Object


25
26
27
28
# File 'lib/post_json/concerns/query_methods.rb', line 25

def add_query(method, *arguments)
  query_tree[method] = (query_tree[method] || []) + arguments
  self
end

#except(*attributes) ⇒ Object


36
37
38
# File 'lib/post_json/concerns/query_methods.rb', line 36

def except(*attributes)
  query_clone.except!(*attributes)
end

#except!(*attributes) ⇒ Object


30
31
32
33
34
# File 'lib/post_json/concerns/query_methods.rb', line 30

def except!(*attributes)
  remove_keys = flatten_arguments(attributes).map(&:to_sym)
  query_tree.except!(*remove_keys)
  self
end

#limit(value) ⇒ Object


44
45
46
# File 'lib/post_json/concerns/query_methods.rb', line 44

def limit(value)
  query_clone.limit!(value)
end

#limit!(value) ⇒ Object


40
41
42
# File 'lib/post_json/concerns/query_methods.rb', line 40

def limit!(value)
  except!(:limit).add_query(:limit, value.to_i)
end

#offset(value) ⇒ Object


52
53
54
# File 'lib/post_json/concerns/query_methods.rb', line 52

def offset(value)
  query_clone.offset!(value)
end

#offset!(value) ⇒ Object


48
49
50
# File 'lib/post_json/concerns/query_methods.rb', line 48

def offset!(value)
  except!(:offset).add_query(:offset, value.to_i)
end

#only(*attributes) ⇒ Object


72
73
74
# File 'lib/post_json/concerns/query_methods.rb', line 72

def only(*attributes)
  query_clone.only!(*attributes)
end

#only!(*attributes) ⇒ Object


66
67
68
69
70
# File 'lib/post_json/concerns/query_methods.rb', line 66

def only!(*attributes)
  keep_keys = flatten_arguments(attributes).map(&:to_sym)
  query_tree.keep_if{|key| key.in?(keep_keys)}
  self
end

#order(*args) ⇒ Object


95
96
97
# File 'lib/post_json/concerns/query_methods.rb', line 95

def order(*args)
  query_clone.order!(*args)
end

#order!(*args) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/post_json/concerns/query_methods.rb', line 76

def order!(*args)
  if 0 < args.length
    flatten_arguments(args).each do |arg|
      name, direction = arg.split(' ')

      direction = direction.to_s.upcase
      direction = "ASC" unless direction.present?


      if direction.in?(["ASC", "DESC"]) == false
        raise ArgumentError, "Direction should be 'asc' or 'desc'"
      end

      add_query(:order, "#{name} #{direction}")
    end
  end
  self
end

#page(page, per_page) ⇒ Object


62
63
64
# File 'lib/post_json/concerns/query_methods.rb', line 62

def page(page, per_page)
  query_clone.page!(page, per_page)
end

#page!(page, per_page) ⇒ Object


56
57
58
59
60
# File 'lib/post_json/concerns/query_methods.rb', line 56

def page!(page, per_page)
  page_int = page.to_i
  per_page_int = per_page.to_i
  offset!((page_int-1)*per_page_int).limit!(per_page_int)
end

#query_cloneObject


19
20
21
22
23
# File 'lib/post_json/concerns/query_methods.rb', line 19

def query_clone
  cloned_self = clone
  cloned_self.query_tree_renew!
  cloned_self
end

#query_treeObject


11
12
13
# File 'lib/post_json/concerns/query_methods.rb', line 11

def query_tree
  @query_tree ||= {}
end

#query_tree_renew!Object


15
16
17
# File 'lib/post_json/concerns/query_methods.rb', line 15

def query_tree_renew!
  @query_tree = @query_tree.deep_dup
end

#reorder(*args) ⇒ Object


103
104
105
# File 'lib/post_json/concerns/query_methods.rb', line 103

def reorder(*args)
  query_clone.reorder!(*args)
end

#reorder!(*args) ⇒ Object


99
100
101
# File 'lib/post_json/concerns/query_methods.rb', line 99

def reorder!(*args)
  except!(:order).order!(*args)
end

#reverse_orderObject Also known as: reverse


123
124
125
# File 'lib/post_json/concerns/query_methods.rb', line 123

def reverse_order
  query_clone.reverse_order!
end

#reverse_order!Object Also known as: reverse!


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/post_json/concerns/query_methods.rb', line 107

def reverse_order!
  current_order = query_tree.delete(:order)
  if current_order.present?
    current_order.each do |arg|
      name, direction = arg.split(' ')
      reverse_direction = direction == "DESC" ? "ASC" : "DESC"
      order!("#{name} #{reverse_direction}")          
    end
    self
  else
    order!("id DESC")
  end
end

#where(filter = :none, *options) ⇒ Object


153
154
155
# File 'lib/post_json/concerns/query_methods.rb', line 153

def where(filter = :none, *options)
  query_clone.where!(filter, *options)
end

#where!(opts = :chain, *rest) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/post_json/concerns/query_methods.rb', line 129

def where!(opts = :chain, *rest)
  if opts == :chain || opts.blank?
    self
  else
    case opts
    when String
      if opts.start_with?("function")
        add_query(:where_function, {function: opts, arguments: rest})
      else
        add_query(:where_forward, [opts] + rest)
      end
    when Array
      add_query(:where_forward, [opts] + rest)
    when Hash
      opts.stringify_keys.flatten_hash.each do |attribute, value|
        add_query(:where_equal, {attribute: attribute, argument: value})
      end
    else
      add_query(:where_forward, [opts] + rest)
    end
    self
  end
end