Class: ActiveRecord::Refined::AST::Over

Inherits:
Node
  • Object
show all
Includes:
Arithmetics, Predications
Defined in:
lib/active_record/refined/ast.rb

Overview

A function with a window. The window is built by chaining, the way Arel's own is, and each method returns a new node rather than adding to this one, so a window can be finished more than one way.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_json, #distinct_from?, #end_with?, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(function, partitions = [], orders = [], frame = nil) ⇒ Over

Returns a new instance of Over.



839
840
841
842
843
844
# File 'lib/active_record/refined/ast.rb', line 839

def initialize(function, partitions = [], orders = [], frame = nil)
  @function = function
  @partitions = partitions
  @orders = orders
  @frame = frame
end

Instance Attribute Details

#frameObject (readonly)

Returns the value of attribute frame.



837
838
839
# File 'lib/active_record/refined/ast.rb', line 837

def frame
  @frame
end

#functionObject (readonly)

Returns the value of attribute function.



837
838
839
# File 'lib/active_record/refined/ast.rb', line 837

def function
  @function
end

#ordersObject (readonly)

Returns the value of attribute orders.



837
838
839
# File 'lib/active_record/refined/ast.rb', line 837

def orders
  @orders
end

#partitionsObject (readonly)

Returns the value of attribute partitions.



837
838
839
# File 'lib/active_record/refined/ast.rb', line 837

def partitions
  @partitions
end

Instance Method Details

#order(*exprs) ⇒ Object

Raises:

  • (ArgumentError)


851
852
853
854
# File 'lib/active_record/refined/ast.rb', line 851

def order(*exprs)
  raise ArgumentError, "order needs an expression" if exprs.empty?
  Over.new(function, partitions, orders + exprs, frame)
end

#partition(*exprs) ⇒ Object

Raises:

  • (ArgumentError)


846
847
848
849
# File 'lib/active_record/refined/ast.rb', line 846

def partition(*exprs)
  raise ArgumentError, "partition needs an expression" if exprs.empty?
  Over.new(function, partitions + exprs, orders, frame)
end

#range(bounds) ⇒ Object



860
861
862
# File 'lib/active_record/refined/ast.rb', line 860

def range(bounds)
  Over.new(function, partitions, orders, framing(:range, bounds))
end

#rows(bounds) ⇒ Object



856
857
858
# File 'lib/active_record/refined/ast.rb', line 856

def rows(bounds)
  Over.new(function, partitions, orders, framing(:rows, bounds))
end

#to_arel(table, model) ⇒ Object



864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/active_record/refined/ast.rb', line 864

def to_arel(table, model)
  window = Arel::Nodes::Window.new
  partitions.each {|expr| window.partition(to_arel_operand(expr, table, model)) }
  orders.each {|expr| window.order(to_arel_operand(expr, table, model)) }
  frame_arel(window) if frame

  # A window-only function refuses to build on its own; here is where
  # it is asked for the call itself.
  arel_function =
    function.is_a?(WindowFunction) ? function.call_arel(table, model)
                                   : function.to_arel(table, model)
  Arel::Nodes::Over.new(arel_function, window)
end