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?, #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.



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

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.



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

def frame
  @frame
end

#functionObject (readonly)

Returns the value of attribute function.



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

def function
  @function
end

#ordersObject (readonly)

Returns the value of attribute orders.



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

def orders
  @orders
end

#partitionsObject (readonly)

Returns the value of attribute partitions.



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

def partitions
  @partitions
end

Instance Method Details

#order(*exprs) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


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

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

#range(bounds) ⇒ Object



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

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

#rows(bounds) ⇒ Object



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

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

#to_arel(table, model) ⇒ Object



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

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