Skip to content

Commit e79e909

Browse files
author
Ray Zane
committed
First commit
0 parents  commit e79e909

20 files changed

+451
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: ruby
2+
rvm:
3+
- 2.1.6
4+
before_install: gem install bundler -v 1.11.2

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in baby_squeel.gemspec
4+
gemspec

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Ray Zane
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# BabySqueel
2+
3+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/baby_squeel`. To experiment with that code, run `bin/console` for an interactive prompt.
4+
5+
TODO: Delete this and the text above, and describe your gem
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'baby_squeel'
13+
```
14+
15+
And then execute:
16+
17+
$ bundle
18+
19+
Or install it yourself as:
20+
21+
$ gem install baby_squeel
22+
23+
## Usage
24+
25+
TODO: Write usage instructions here
26+
27+
## Development
28+
29+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30+
31+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32+
33+
## Contributing
34+
35+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/baby_squeel.
36+
37+
38+
## License
39+
40+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41+

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

baby_squeel.gemspec

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'baby_squeel/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "baby_squeel"
8+
spec.version = BabySqueel::VERSION
9+
spec.authors = ["Ray Zane"]
10+
spec.email = ["[email protected]"]
11+
12+
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13+
spec.description = %q{TODO: Write a longer description or delete this line.}
14+
spec.homepage = "TODO: Put your gem's website or public repo URL here."
15+
spec.license = "MIT"
16+
17+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18+
# delete this section to allow pushing this gem to any host.
19+
if spec.respond_to?(:metadata)
20+
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21+
else
22+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23+
end
24+
25+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26+
spec.bindir = "exe"
27+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28+
spec.require_paths = ["lib"]
29+
30+
spec.add_dependency 'activerecord', '>= 4.0.0'
31+
32+
spec.add_development_dependency "bundler", "~> 1.11"
33+
spec.add_development_dependency "rake", "~> 10.0"
34+
spec.add_development_dependency "rspec", "~> 3.0"
35+
spec.add_development_dependency 'simplecov'
36+
spec.add_development_dependency "sqlite3"
37+
end

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "baby_squeel"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

lib/baby_squeel.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'active_record'
2+
require 'baby_squeel/version'
3+
require 'baby_squeel/dsl'
4+
5+
module BabySqueel
6+
module WhereChain
7+
def has(&block)
8+
@scope.where_values += DSL.evaluate(@scope, &block)
9+
@scope
10+
end
11+
end
12+
13+
module QueryMethods
14+
def selecting(&block)
15+
select DSL.evaluate(self, &block)
16+
end
17+
18+
def ordering(&block)
19+
order DSL.evaluate(self, &block)
20+
end
21+
end
22+
end
23+
24+
ActiveRecord::Base.extend BabySqueel::QueryMethods
25+
ActiveRecord::QueryMethods::WhereChain.prepend BabySqueel::WhereChain

lib/baby_squeel/dsl.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module BabySqueel
2+
class DSL
3+
def self.evaluate(scope, &block)
4+
new(scope).evaluate(&block)
5+
end
6+
7+
def initialize(scope)
8+
@scope = scope
9+
end
10+
11+
def [](key)
12+
@scope.arel_table[key]
13+
end
14+
15+
def func(name, *args)
16+
Arel::Nodes::NamedFunction.new(name.to_s, args)
17+
end
18+
19+
def evaluate(&block)
20+
if block.arity.zero?
21+
[instance_eval(&block)]
22+
else
23+
[block.call(self)]
24+
end
25+
end
26+
27+
private
28+
29+
def respond_to_missing?(name, *)
30+
@scope.column_names.include?(name.to_s) ||
31+
!@scope.reflect_on_association(name).nil?
32+
end
33+
34+
def method_missing(name, *args, &block)
35+
if args.empty? && !block_given?
36+
if @scope.column_names.include?(name.to_s)
37+
@scope.arel_table[name]
38+
elsif assoc = @scope.reflect_on_association(name)
39+
assoc.klass.arel_table
40+
else
41+
super
42+
end
43+
else
44+
super
45+
end
46+
end
47+
end
48+
end

lib/baby_squeel/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module BabySqueel
2+
VERSION = "0.1.0"
3+
end

spec/baby_squeel/dsl_spec.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'spec_helper'
2+
require 'baby_squeel/dsl'
3+
4+
describe BabySqueel::DSL do
5+
subject(:dsl) { BabySqueel::DSL.new(Post) }
6+
7+
describe '#respond_to?' do
8+
it 'resolves attributes' do
9+
expect(dsl.respond_to?(:id)).to eq(true)
10+
end
11+
12+
it 'resolves associations' do
13+
expect(dsl.respond_to?(:author)).to eq(true)
14+
end
15+
end
16+
17+
describe '#method_missing' do
18+
it 'resolves attributes' do
19+
expect(dsl.id).to be_an(Arel::Attribute)
20+
end
21+
22+
it 'resolves associations' do
23+
expect(dsl.author).to eq(Author.arel_table)
24+
end
25+
26+
it 'does not resolve when arguments are given' do
27+
expect { dsl.id(:arg) }.to raise_error(NameError)
28+
end
29+
30+
it 'does not resolve when a block is given' do
31+
expect { dsl.id { 'block' } }.to raise_error(NameError)
32+
end
33+
34+
it 'does not resolve for non-existent columns' do
35+
expect { dsl.non_existent_column }.to raise_error(NameError)
36+
end
37+
end
38+
39+
describe '#evaluate' do
40+
context 'when an arity is given' do
41+
it 'yields itself' do
42+
dsl.evaluate do |table|
43+
expect(table).to be_a(BabySqueel::DSL)
44+
end
45+
end
46+
47+
it 'does not change self' do
48+
this = self
49+
that = nil
50+
dsl.evaluate { |t| that = self }
51+
expect(that).to equal(this)
52+
end
53+
end
54+
55+
context 'when no arity is given' do
56+
it 'changes self' do
57+
this = self
58+
that = nil
59+
dsl.evaluate { that = self }
60+
expect(that).not_to equal(this)
61+
end
62+
63+
it 'resolves attributes without a receiver' do
64+
resolution = nil
65+
dsl.evaluate { resolution = title }
66+
expect(resolution).to eq(Post.arel_table[:title])
67+
end
68+
end
69+
end
70+
71+
describe '#[]' do
72+
it 'returns an arel attribute' do
73+
expect(dsl[:title]).to be_an(Arel::Attribute)
74+
end
75+
end
76+
77+
describe '#func' do
78+
it 'constructs a named function' do
79+
expect(dsl.func(:coalesce, 0, 1).to_sql).to eq('coalesce(0, 1)')
80+
end
81+
end
82+
end
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe BabySqueel::QueryMethods do
4+
describe '#selecting' do
5+
it 'selects using arel' do
6+
relation = Post.selecting { [id, title] }
7+
8+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
9+
SELECT "posts"."id", "posts"."title"
10+
FROM "posts"
11+
EOSQL
12+
end
13+
14+
it 'selects aggregates' do
15+
relation = Post.selecting { id.count }
16+
17+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
18+
SELECT COUNT("posts"."id")
19+
FROM "posts"
20+
EOSQL
21+
end
22+
23+
it 'selects associations' do
24+
relation = Post.joins(:author).selecting {
25+
[id, author[:id]]
26+
}
27+
28+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
29+
SELECT "posts"."id", "authors"."id"
30+
FROM "posts"
31+
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
32+
EOSQL
33+
end
34+
end
35+
36+
describe '#ordering' do
37+
it 'orders using arel' do
38+
relation = Post.ordering { title.desc }
39+
40+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
41+
SELECT "posts".* FROM "posts"
42+
ORDER BY "posts"."title" DESC
43+
EOSQL
44+
end
45+
46+
it 'orders using multiple arel columns' do
47+
relation = Post.ordering {
48+
[title.desc, published_at.asc]
49+
}
50+
51+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
52+
SELECT "posts".* FROM "posts"
53+
ORDER BY "posts"."title" DESC, "posts"."published_at" ASC
54+
EOSQL
55+
end
56+
57+
it 'orders on an aggregate column' do
58+
relation = Post.ordering { id.count }.group(:id)
59+
60+
expect(relation.to_sql.squish).to eq(<<-EOSQL.squish)
61+
SELECT "posts".* FROM "posts"
62+
GROUP BY "posts"."id"
63+
ORDER BY COUNT("posts"."id")
64+
EOSQL
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)