class RethinkDB::RQL

Attributes

bitop[RW]
body[RW]

Public Class Methods

fast_expr(x, max_depth) click to toggle source
# File lib/shim.rb, line 118
def self.fast_expr(x, max_depth)
  if max_depth == 0
    raise ReqlDriverCompileError, "Maximum expression depth exceeded " +
      "(you can override this with `r.expr(X, MAX_DEPTH)`)."
  end
  case x
  when RQL then x
  when Array then RQL.new([Term::TermType::MAKE_ARRAY,
                           x.map{|y| fast_expr(y, max_depth-1)}])
  when Hash then RQL.new(Hash[x.map{|k,v| [safe_to_s(k),
                                           fast_expr(v, max_depth-1)]}])
  when Proc then RQL.new.new_func(&x)
  when Binary then RQL.new.binary(x)
  when String then RQL.new(x)
  when Symbol then RQL.new(x)
  when Numeric then RQL.new(x)
  when FalseClass then RQL.new(x)
  when TrueClass then RQL.new(x)
  when NilClass then RQL.new(x)
  when Time then
    epoch_time = (x.to_r * 1000).to_i / 1000.0
    offset = x.utc_offset
    raw_offset = offset.abs
    raw_hours = raw_offset / 3600
    raw_minutes = (raw_offset / 60) - (raw_hours * 60)
    tz = (offset < 0 ? "-" : "+") + sprintf("%02d:%02d", raw_hours, raw_minutes);
    RQL.new({ '$reql_type$' => 'TIME',
              'epoch_time'  => epoch_time,
              'timezone'    => tz })
  else raise ReqlDriverCompileError, "r.expr can't handle #{x.inspect} of class #{x.class}."
  end
end
new(body = RQL, bitop = nil) click to toggle source
# File lib/rethinkdb.rb, line 34
def initialize(body = RQL, bitop = nil)
  @body = body
  @bitop = bitop
end
safe_to_s(x) click to toggle source
# File lib/shim.rb, line 109
def self.safe_to_s(x)
  case x
  when String then x
  when Symbol then x.to_s
  else raise ReqlDriverCompileError, 'Object keys must be strings or symbols.  '+
      "(Got object `#{x.inspect}` of class `#{x.class}`.)"
  end
end
set_default_conn(c;) click to toggle source
# File lib/net.rb, line 285
def self.set_default_conn c; @@default_conn = c; end

Public Instance Methods

-@() click to toggle source
# File lib/func.rb, line 139
def -@; RQL.new.sub(0, self); end
==(rhs) click to toggle source
# File lib/func.rb, line 150
def ==(rhs)
  raise ArgumentError,"
  Cannot use inline ==/!= with RQL queries, use .eq() instead if
  you want a query that does equality comparison.

  If you need to see whether two queries are the same, compare
  the raw qeuries like: `query1.to_pb == query2.to_pb`."
end
[](ind) click to toggle source
# File lib/func.rb, line 141
def [](ind)
  if ind.is_a?(Range)
    return slice(ind.begin, ind.end, :right_bound =>
                 (ind.exclude_end? ? 'open' : 'closed'))
  else
    return bracket(ind)
  end
end
as_json(*a, &b) click to toggle source
# File lib/shim.rb, line 93
def as_json(*a, &b)
  @body.as_json(*a, &b)
end
binary(*a) click to toggle source
# File lib/shim.rb, line 99
def binary(*a)
    args = ((@body != RQL) ? [self] : []) + a
    RQL.new([Term::TermType::BINARY, args.map {|x|
                case x
                when RQL then x.to_pb
                else { '$reql_type$' => 'BINARY', 'data' => Base64.strict_encode64(x) }
                end
             }])
end
coerce(other) click to toggle source
# File lib/shim.rb, line 158
def coerce(other)
  [RQL.new.expr(other), self]
end
connect(*args, &b) click to toggle source
# File lib/func.rb, line 133
def connect(*args, &b)
  unbound_if @body != RQL
  c = Connection.new(*args)
  b ? begin b.call(c) ensure c.close end : c
end
do(*args, &b) click to toggle source
# File lib/func.rb, line 159
def do(*args, &b)
  a = ((@body != RQL) ? [self] : []) + args.dup
  if a == [] && !b
    raise ReqlDriverCompileError, "Expected 1 or more arguments but found 0."
  end
  funcall_args = (b ? [new_func(&b)] : [a.pop]) + a
  # PP.pp funcall_args
  RQL.new.funcall(*funcall_args)
end
em_run(*args, &b) click to toggle source
# File lib/net.rb, line 345
def em_run(*args, &b)
  if !EM.reactor_running?
    raise RuntimeError, "RethinkDB::RQL::em_run can only be called inside `EM.run`"
  end
  unbound_if(@body == RQL)
  args = parse(*args, &b)
  if args[:block].is_a?(Proc)
    args[:block] = CallbackHandler.new(args[:block])
  end
  if !args[:block].is_a?(Handler)
    raise ArgumentError, "No handler specified."
  end

  # If the user has defined the `on_state` method, we assume they want states.
  if args[:block].respond_to?(:on_state)
    args[:opts] = args[:opts].merge(include_states: true)
  end

  EM_Guard.register(args[:conn])
  args[:conn].run(@body, args[:opts], args[:block])
end
expr(x, max_depth=20) click to toggle source
# File lib/shim.rb, line 151
def expr(x, max_depth=20)
  if not max_depth.is_a? Numeric
    raise ReqlDriverCompileError, "Second argument to `r.expr` must be a number."
  end
  unbound_if(@body != RQL)
  RQL.fast_expr(x, max_depth)
end
inspect() click to toggle source
Calls superclass method
# File lib/rethinkdb.rb, line 43
def inspect
  (@body != RQL) ? pp : super
end
new_func(&b) click to toggle source
# File lib/func.rb, line 5
def new_func(&b)
  args = @@gensym_mutex.synchronize{(0...b.arity).map{@@gensym_cnt += 1}}
  body = b.call(*(args.map{|i| RQL.new.var i}))
  RQL.new.func(args, body)
end
parse(*args, &b) click to toggle source
# File lib/net.rb, line 286
def parse(*args, &b)
  conn = nil
  opts = nil
  block = nil
  args = args.map{|x| x.is_a?(Class) ? x.new : x}
  args.each {|arg|
    case arg
    when RethinkDB::Connection
      raise ArgumentError, "Unexpected second Connection #{arg.inspect}." if conn
      conn = arg
    when Hash
      raise ArgumentError, "Unexpected second Hash #{arg.inspect}." if opts
      opts = arg
    when Proc
      raise ArgumentError, "Unexpected second callback #{arg.inspect}." if block
      block = arg
    when Handler
      raise ArgumentError, "Unexpected second callback #{arg.inspect}." if block
      block = arg
    else
      raise ArgumentError, "Unexpected argument #{arg.inspect} " +
        "(got #{args.inspect})."
    end
  }
  conn = @@default_conn if !conn
  opts = {} if !opts
  block = b if !block
  if (tf = opts[:time_format])
    opts[:time_format] = (tf = tf.to_s)
    if tf != 'raw' && tf != 'native'
      raise ArgumentError, "`time_format` must be 'raw' or 'native' (got `#{tf}`)."
    end
  end
  if (gf = opts[:group_format])
    opts[:group_format] = (gf = gf.to_s)
    if gf != 'raw' && gf != 'native'
      raise ArgumentError, "`group_format` must be 'raw' or 'native' (got `#{gf}`)."
    end
  end
  if (bf = opts[:binary_format])
    opts[:binary_format] = (bf = bf.to_s)
    if bf != 'raw' && bf != 'native'
      raise ArgumentError, "`binary_format` must be 'raw' or 'native' (got `#{bf}`)."
    end
  end
  if !conn
    raise ArgumentError, "No connection specified!\n" \
    "Use `query.run(conn)` or `conn.repl(); query.run`."
  end
  {conn: conn, opts: opts, block: block}
end
pp() click to toggle source
# File lib/rethinkdb.rb, line 39
def pp
  unbound_if(@body == RQL)
  RethinkDB::RPP.pp(@body)
end
row() click to toggle source
# File lib/func.rb, line 169
def row
  unbound_if(@body != RQL)
  raise NoMethodError, ("Sorry, r.row is not available in the ruby driver.  " +
                        "Use blocks instead.")
end
run(*args, &b) click to toggle source
# File lib/net.rb, line 337
def run(*args, &b)
  unbound_if(@body == RQL)
  args = parse(*args, &b)
  if args[:block].is_a?(Handler)
    raise RuntimeError, "Cannot call `run` with a handler, did you mean `em_run`?"
  end
  args[:conn].run(@body, args[:opts], args[:block])
end
to_json(*a, &b) click to toggle source
# File lib/shim.rb, line 89
def to_json(*a, &b)
  @body.to_json(*a, &b)
end
to_pb() click to toggle source
# File lib/shim.rb, line 97
def to_pb; @body; end