Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to concatenate Arel.sql fragments
- Loading branch information
Showing
8 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Arel # :nodoc: all | ||
module Nodes | ||
class Fragments < Arel::Nodes::Node | ||
attr_reader :values | ||
|
||
def initialize(values = []) | ||
super() | ||
@values = values | ||
end | ||
|
||
def initialize_copy(other) | ||
super | ||
@values = @values.clone | ||
end | ||
|
||
def hash | ||
[@values].hash | ||
end | ||
|
||
def +(other) | ||
raise ArgumentError, "Expected Arel node" unless Arel.arel_node?(other) | ||
|
||
self.class.new([*@values, other]) | ||
end | ||
|
||
def eql?(other) | ||
self.class == other.class && | ||
self.values == other.values | ||
end | ||
alias :== :eql? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../helper" | ||
require "yaml" | ||
|
||
module Arel | ||
module Nodes | ||
class FragmentsTest < Arel::Spec | ||
describe "equality" do | ||
it "is equal with equal values" do | ||
array = [Fragments.new(["foo", "bar"]), Fragments.new(["foo", "bar"])] | ||
assert_equal 1, array.uniq.size | ||
end | ||
|
||
it "is not equal with different values" do | ||
array = [Fragments.new(["foo"]), Fragments.new(["bar"])] | ||
assert_equal 2, array.uniq.size | ||
end | ||
|
||
it "can be joined with other nodes" do | ||
fragments = Fragments.new(["foo", "bar"]) | ||
sql = Arel.sql("SELECT") | ||
joined_fragments = fragments + sql | ||
|
||
assert_equal ["foo", "bar"], fragments.values | ||
assert_equal ["foo", "bar", sql], joined_fragments.values | ||
end | ||
|
||
it "fails if joined with something that is not an Arel node" do | ||
fragments = Fragments.new | ||
assert_raises ArgumentError do | ||
fragments + "Not a node" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters