Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Partial ExQL.Condition refactoring, better support for infix operators

  • Loading branch information...
commit f4df1dc8301f364f894150c1ab82b17269745f00 1 parent c4d3b3c
@yrashk authored
Showing with 17 additions and 10 deletions.
  1. +17 −10 lib/condition.ex
View
27 lib/condition.ex
@@ -1,34 +1,41 @@
defmodule ExQL.Op do
- def __binary__(record, op, l,r) do
+ def __binary__(record, op, name, l,r) do
quote do
- unquote(record).new op: unquote(op), exprs: [unquote(l),unquote(r)]
+ unquote(record).new op: unquote(op), name: to_binary(unquote(name)), exprs: [unquote(l),unquote(r)]
end
end
- defmacro __using__(ops) do
- arity_ops = lc op inlist ops, do: {op, 2}
+
+ defmacro __using__(_) do
+ quote do
+ import ExQL.Op, only: [infix: 1]
+ end
+ end
+
+ defmacro infix(ops) do
+ arity_ops = lc {op, _} inlist ops, do: {op, 2}
head =
quote do
- import ExQL.Op, only: [__binary__: 4]
def __ops__, do: unquote(arity_ops)
end
- tail = lc op inlist ops do
+ tail = lc {op, name} inlist ops do
quote do
defmacro unquote(op).(l,r) do
- __binary__(unquote(__CALLER__.module), unquote(op), l, r)
+ ExQL.Op.__binary__(unquote(__CALLER__.module), unquote(op), unquote(name), l, r)
end
end
end
{:__block__, 0, [head|tail]}
end
end
-defrecord ExQL.Condition, op: nil, exprs: [] do
- use ExQL.Op, [:<, :<=, :>=, :>, :==, :and, :or]
+defrecord ExQL.Condition, op: nil, name: nil, exprs: [] do
+ use ExQL.Op
+ infix <: "<", <=: "<=", >=: ">=", >: ">", ==: "=", and: "AND", or: "OR"
end
defimpl ExQL.Expression, for: ExQL.Condition do
def join(condition, type, _delim) do
- delim = to_binary(ExQL.Condition.op(condition))
+ delim = ExQL.Condition.name(condition)
[[_, first]|rest] = lc value inlist ExQL.Condition.exprs(condition), do: [delim, ExQL.Expression.join(value, type, delim)]
["(",[first|rest],")"]
end
Please sign in to comment.
Something went wrong with that request. Please try again.