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_text, #distinct_from?, #end_with?, #except, #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.



974
975
976
977
978
979
# File 'lib/active_record/refined/ast.rb', line 974

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.



972
973
974
# File 'lib/active_record/refined/ast.rb', line 972

def frame
  @frame
end

#functionObject (readonly)

Returns the value of attribute function.



972
973
974
# File 'lib/active_record/refined/ast.rb', line 972

def function
  @function
end

#ordersObject (readonly)

Returns the value of attribute orders.



972
973
974
# File 'lib/active_record/refined/ast.rb', line 972

def orders
  @orders
end

#partitionsObject (readonly)

Returns the value of attribute partitions.



972
973
974
# File 'lib/active_record/refined/ast.rb', line 972

def partitions
  @partitions
end

Instance Method Details

#order(*exprs) ⇒ Object

Raises:

  • (ArgumentError)


986
987
988
989
# File 'lib/active_record/refined/ast.rb', line 986

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)


981
982
983
984
# File 'lib/active_record/refined/ast.rb', line 981

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

#range(bounds) ⇒ Object



995
996
997
# File 'lib/active_record/refined/ast.rb', line 995

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

#rows(bounds) ⇒ Object



991
992
993
# File 'lib/active_record/refined/ast.rb', line 991

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

#to_arel(table, model) ⇒ Object



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/active_record/refined/ast.rb', line 999

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