From c22abda79b7f0fbee5f0ef0e9648d52d7d483902 Mon Sep 17 00:00:00 2001 From: Shahbaz Javeed Date: Thu, 10 Dec 2015 16:50:28 -0500 Subject: * Support for bitwise operations as infix operators. Tests included. *** Individual commit messages included below *** * Preliminary support for bitwise operations as infix operators. Tests to follow. * Added bitwise xor, shift left and shift right operators * Fixed the BitwiseOr class so it uses the :| operator instead of :& * All the methods for the bitwise operators in the Arel::Math module now wrap them up in Arel::Nodes::Grouping so the operation becomes isolated like addition and subtraction * Preliminary set of tests for the new operators * Updated README with examples of bitwise operations * Added a new UnaryOperation class which is a riff on the InfixOperation class * Added tests for UnaryOperation (inspired by InfixOperation tests) * Added the bitwise not (~) operator as a UnaryOperation * Added tests for the bitwise not operator * Added documentation for the bitwise not operator * Updated gemspec using `rake arel.gemspec` --- lib/arel/nodes/infix_operation.rb | 30 ++++++++++++++++++++++++++++++ lib/arel/nodes/unary_operation.rb | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 lib/arel/nodes/unary_operation.rb (limited to 'lib/arel/nodes') diff --git a/lib/arel/nodes/infix_operation.rb b/lib/arel/nodes/infix_operation.rb index a3f04da6fa..55ac715f73 100644 --- a/lib/arel/nodes/infix_operation.rb +++ b/lib/arel/nodes/infix_operation.rb @@ -45,5 +45,35 @@ module Arel super('||', left, right) end end + + class BitwiseAnd < InfixOperation + def initialize left, right + super(:&, left, right) + end + end + + class BitwiseOr < InfixOperation + def initialize left, right + super(:|, left, right) + end + end + + class BitwiseXor < InfixOperation + def initialize left, right + super(:^, left, right) + end + end + + class BitwiseShiftLeft < InfixOperation + def initialize left, right + super(:<<, left, right) + end + end + + class BitwiseShiftRight < InfixOperation + def initialize left, right + super(:>>, left, right) + end + end end end diff --git a/lib/arel/nodes/unary_operation.rb b/lib/arel/nodes/unary_operation.rb new file mode 100644 index 0000000000..1636c01279 --- /dev/null +++ b/lib/arel/nodes/unary_operation.rb @@ -0,0 +1,25 @@ +module Arel + module Nodes + + class UnaryOperation < Unary + include Arel::Expressions + include Arel::Predications + include Arel::OrderPredications + include Arel::AliasPredication + include Arel::Math + + attr_reader :operator + + def initialize operator, operand + super(operand) + @operator = operator + end + end + + class BitwiseNot < UnaryOperation + def initialize operand + super(:~, operand) + end + end + end +end \ No newline at end of file -- cgit v1.2.3