class RailsServersideDatatables::ExprTreeNode

Public Class Methods

create_by_type(type) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 53
def self.create_by_type(type)
  if type.is_a? Numeric
    return ExprTreeNode.new(type, Type::NUMERIC)
  elsif type.is_a?(Expression)
    return type.expression
  elsif type.nil?
    return raw('NULL')
  elsif type.is_a? Symbol
    return ExprTreeNode.new(type, Type::COLUMN)
  elsif type.is_a? String
    return ExprTreeNode.new(type, Type::STRING)
  elsif type.is_a? ExprTreeNode
    return type
  elsif type.is_a? Array
    f = ExprTreeNode.new('||', Type::OPERATOR)
    type.each { |e| f.add_argument e }
    return f
  else
    raise 'Invalid type'
  end
end
new(token, type, force_brackets = true) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 17
def initialize(token, type, force_brackets = true)
  @token = token
  @type = type
  @arguments = []
  @force_brackets = force_brackets
end
raw(expr) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 75
def self.raw(expr)
  ExprTreeNode.new(expr.to_s, Type::RAW)
end

Private Class Methods

format_column(name) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 81
def self.format_column(name)
  name.to_s.split('.').map { |f| '"' + f + '"' }.join '.'
end
format_function(name, arguments) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 89
def self.format_function(name, arguments)
  name.to_s.upcase + '(' + arguments.map { |arg| arg.to_s }.join(',') + ')'
end
format_operator(operator, arguments, brackets) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 93
def self.format_operator(operator, arguments, brackets)
  term = arguments.map { |arg| arg.to_s }.join(" #{operator} ")

  brackets ? '(' + term + ')' : term
end
format_string(name) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 85
def self.format_string(name)
  "#{ActiveRecord::Base.sanitize name}"
end

Public Instance Methods

add_argument(argument) click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 24
def add_argument(argument)
  @arguments.push ExprTreeNode.create_by_type(argument) unless argument.is_a? Nothing
end
arguments?() click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 28
def arguments?
  @arguments.any?
end
to_s() click to toggle source
# File lib/rails_serverside_datatables/expression_tree_node.rb, line 32
def to_s
  case @type
    when Type::RAW
      return @token.to_s
    when Type::COLUMN
      return ExprTreeNode.format_column @token
    when Type::STRING
      return ExprTreeNode.format_string @token
    when Type::NUMERIC
      return @token.is_a?(Numeric) ? @token.to_s : @token.to_f.to_s
    when Type::FUNCTION
      return ExprTreeNode.format_function @token, @arguments
    when Type::OPERATOR
      return ExprTreeNode.format_operator @token, @arguments, @force_brackets
    when Type::NULL
      return 'NULL'
    else
      raise 'Invalid fragment type passed.'
  end
end