rom-yesql 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.rubocop.yml +78 -0
- data/.rubocop_todo.yml +15 -0
- data/.travis.yml +28 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +20 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +7 -0
- data/db/.gitkeep +0 -0
- data/lib/rom-yesql.rb +3 -0
- data/lib/rom/yesql.rb +4 -0
- data/lib/rom/yesql/dataset.rb +27 -0
- data/lib/rom/yesql/relation.rb +110 -0
- data/lib/rom/yesql/repository.rb +98 -0
- data/lib/rom/yesql/version.rb +5 -0
- data/log/.gitkeep +0 -0
- data/rakelib/rubocop.rake +18 -0
- data/rom-yesql.gemspec +25 -0
- data/spec/fixtures/tasks/by_id.sql +1 -0
- data/spec/fixtures/users/by_name.sql +1 -0
- data/spec/integration/adapter_spec.rb +59 -0
- data/spec/shared/database_setup.rb +27 -0
- data/spec/shared/repository_setup.rb +10 -0
- data/spec/shared/users_and_tasks.rb +12 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/unit/rom/yesql/repository_spec.rb +38 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9786490a43411e42735f76e6748f5cae21523f89
|
4
|
+
data.tar.gz: acc0b071db8c524d42d800596f5ed6544c9823b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a3fb6b2f8e318ad79d3b84708bd5dbbf24d97656bff5c6d6aca435e6478b1f5c0ebf59b90619b1bc805d7e2f1bb84a3b4404a69a28a409034f69292d6346625
|
7
|
+
data.tar.gz: 52ca49296a6d51d295946a4d84ce0e041dcf6360e2713650b1b4bb05d6e65430b1286d8ab4397e921d704a59968c189ef20e4a687d4ab81050f22602a9e953de
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Generated by `rubocop --auto-gen-config`
|
2
|
+
inherit_from: .rubocop_todo.yml
|
3
|
+
|
4
|
+
# Exclude temporary files
|
5
|
+
AllCops:
|
6
|
+
Exclude:
|
7
|
+
- tmp/**/*
|
8
|
+
|
9
|
+
# It’s quite readable when we know what we are doing
|
10
|
+
Lint/AssignmentInCondition:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
# No need to handle LoadError in Rakefile
|
14
|
+
Lint/HandleExceptions:
|
15
|
+
Exclude:
|
16
|
+
- Rakefile
|
17
|
+
|
18
|
+
# gemspec is a special snowflake
|
19
|
+
Metrics/LineLength:
|
20
|
+
Exclude:
|
21
|
+
- rom-yesql.gemspec
|
22
|
+
|
23
|
+
# The enforced style doesn’t match Vim’s defaults
|
24
|
+
Style/AlignParameters:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
# UTF-8 is perfectly fine in comments
|
28
|
+
Style/AsciiComments:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
# Allow using braces for value-returning blocks
|
32
|
+
Style/Blocks:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
# Documentation checked by Inch CI
|
36
|
+
Style/Documentation:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
# Early returns have their vices
|
40
|
+
Style/GuardClause:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
# Need to be skipped for >-> usage
|
44
|
+
Style/Lambda:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
# Multiline block chains are ok
|
48
|
+
Style/MultilineBlockChain:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
# Result::Success and Result::Failure use > for callbacks
|
52
|
+
Style/OpMethod:
|
53
|
+
Exclude:
|
54
|
+
- lib/rom/command_registry.rb
|
55
|
+
|
56
|
+
# Even a single escaped slash can be confusing
|
57
|
+
Style/RegexpLiteral:
|
58
|
+
MaxSlashes: 0
|
59
|
+
|
60
|
+
# Don’t introduce semantic fail/raise distinction
|
61
|
+
Style/SignalException:
|
62
|
+
EnforcedStyle: only_raise
|
63
|
+
|
64
|
+
# Need to be skipped for >-> usage
|
65
|
+
Style/SpaceAroundOperators:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
# Accept both single and double quotes
|
69
|
+
Style/StringLiterals:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
# Allow def self.foo; @foo; end
|
73
|
+
Style/TrivialAccessors:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
# Allow rom-sql
|
77
|
+
Style/FileName:
|
78
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-03-01 15:08:21 +0100 using RuboCop version 0.29.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 1
|
9
|
+
Lint/HandleExceptions:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
# Offense count: 1
|
13
|
+
# Configuration parameters: AllowURI, URISchemes.
|
14
|
+
Metrics/LineLength:
|
15
|
+
Max: 81
|
data/.travis.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
language: ruby
|
2
|
+
sudo: false
|
3
|
+
cache: bundler
|
4
|
+
bundler_args: --without tools
|
5
|
+
script: "bundle exec rake ci"
|
6
|
+
rvm:
|
7
|
+
- 2.0
|
8
|
+
- 2.1
|
9
|
+
- 2.2
|
10
|
+
- rbx-2
|
11
|
+
- jruby
|
12
|
+
- jruby-head
|
13
|
+
- ruby-head
|
14
|
+
env:
|
15
|
+
global:
|
16
|
+
- CODECLIMATE_REPO_TOKEN=15dad2a6b4f5f73f367783df8192230b73070b33fe1f193393e9867d50653885
|
17
|
+
- JRUBY_OPTS='--dev -J-Xmx1024M'
|
18
|
+
matrix:
|
19
|
+
allow_failures:
|
20
|
+
- rvm: ruby-head
|
21
|
+
- rvm: jruby-head
|
22
|
+
notifications:
|
23
|
+
webhooks:
|
24
|
+
urls:
|
25
|
+
- https://webhooks.gitter.im/e/39e1225f489f38b0bd09
|
26
|
+
on_success: change
|
27
|
+
on_failure: always
|
28
|
+
on_start: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'rom', github: 'rom-rb/rom', branch: 'master'
|
7
|
+
gem 'inflecto'
|
8
|
+
gem 'rspec', '~> 3.1'
|
9
|
+
gem 'codeclimate-test-reporter', require: false
|
10
|
+
gem 'sqlite3', platforms: [:mri, :rbx]
|
11
|
+
gem 'jdbc-sqlite3', platforms: :jruby
|
12
|
+
end
|
13
|
+
|
14
|
+
group :tools do
|
15
|
+
gem 'guard'
|
16
|
+
gem 'byebug', platforms: [:mri]
|
17
|
+
gem 'guard-rspec'
|
18
|
+
gem 'guard-rubocop'
|
19
|
+
gem 'rubocop', '~> 0.28'
|
20
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
group :red_green_refactor, halt_on_fail: true do
|
2
|
+
guard :rspec, cmd: "rspec", all_on_start: true do
|
3
|
+
# run all specs if Gemfile.lock is modified
|
4
|
+
watch('Gemfile.lock') { 'spec' }
|
5
|
+
|
6
|
+
# run all specs if any library code is modified
|
7
|
+
watch(%r{\Alib/.+\.rb\z}) { 'spec' }
|
8
|
+
|
9
|
+
# run all specs if supporting files are modified
|
10
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
11
|
+
watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
|
12
|
+
|
13
|
+
# run a spec if it is modified
|
14
|
+
watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
|
15
|
+
|
16
|
+
notification :tmux, display_message: true if ENV.key?('TMUX')
|
17
|
+
end
|
18
|
+
|
19
|
+
guard :rubocop do
|
20
|
+
# run rubocop on modified file
|
21
|
+
watch(%r{\Alib/.+\.rb\z})
|
22
|
+
watch(%r{\Aspec/.+\.rb\z})
|
23
|
+
end
|
24
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Piotr Solnica
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
[gem]: https://rubygems.org/gems/rom-yesql
|
2
|
+
[travis]: https://travis-ci.org/rom-rb/rom-yesql
|
3
|
+
[gemnasium]: https://gemnasium.com/rom-rb/rom-yesql
|
4
|
+
[codeclimate]: https://codeclimate.com/github/rom-rb/rom-yesql
|
5
|
+
[inchpages]: http://inch-ci.org/github/rom-rb/rom-yesql
|
6
|
+
|
7
|
+
# ROM::Yesql
|
8
|
+
|
9
|
+
[![Gem Version](https://badge.fury.io/rb/rom-yesql.svg)][gem]
|
10
|
+
[![Build Status](https://travis-ci.org/rom-rb/rom-yesql.svg?branch=master)][travis]
|
11
|
+
[![Dependency Status](https://gemnasium.com/rom-rb/rom-yesql.png)][gemnasium]
|
12
|
+
[![Code Climate](https://codeclimate.com/github/rom-rb/rom-yesql/badges/gpa.svg)][codeclimate]
|
13
|
+
[![Test Coverage](https://codeclimate.com/github/rom-rb/rom-yesql/badges/coverage.svg)][codeclimate]
|
14
|
+
[![Inline docs](http://inch-ci.org/github/rom-rb/rom-yesql.svg?branch=master)][inchpages]
|
15
|
+
|
16
|
+
|
17
|
+
[Yesql](https://github.com/krisajenkins/yesql)-like adapter for [Ruby Object Mapper](https://github.com/rom-rb/rom).
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'rom-yesql'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install rom-yesql
|
34
|
+
|
35
|
+
## Synopsis
|
36
|
+
|
37
|
+
``` ruby
|
38
|
+
# given sql/my_queries/users.sql includes "SELECT * FROM users WHERE name = '%{name}'"
|
39
|
+
|
40
|
+
ROM.setup(:yesql, ['sqlite://path/to/your/db', path: './sql'])
|
41
|
+
|
42
|
+
class MyQueries < ROM::Relation[:yesql]
|
43
|
+
end
|
44
|
+
|
45
|
+
rom = ROM.finalize.env
|
46
|
+
|
47
|
+
my_queries = rom.relations[:my_queries]
|
48
|
+
my_queries.users(name: 'Jane').to_a # => gets the users
|
49
|
+
```
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
See `LICENSE` file.
|
data/Rakefile
ADDED
data/db/.gitkeep
ADDED
File without changes
|
data/lib/rom-yesql.rb
ADDED
data/lib/rom/yesql.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module ROM
|
2
|
+
module Yesql
|
3
|
+
# Yesql dataset simply uses a sequel connection to fetch results of a query
|
4
|
+
#
|
5
|
+
# @api private
|
6
|
+
class Dataset
|
7
|
+
# @return [Sequel::Database]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
attr_reader :connection
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def initialize(connection)
|
14
|
+
@connection = connection
|
15
|
+
end
|
16
|
+
|
17
|
+
# Fetch results of a query using sequel connection
|
18
|
+
#
|
19
|
+
# @return [Array<Hash>]
|
20
|
+
#
|
21
|
+
# @api private
|
22
|
+
def read(query)
|
23
|
+
connection.fetch(query)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'rom/relation'
|
2
|
+
|
3
|
+
module ROM
|
4
|
+
module Yesql
|
5
|
+
# Yesql relation subclass
|
6
|
+
#
|
7
|
+
# Class that inherits from this relation will be extended with methods
|
8
|
+
# based on its repository queries hash
|
9
|
+
#
|
10
|
+
# It also supports overriding query_proc
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# ROM.setup(:yesql, [uri, path: '/my/sql/queries/are/here'])
|
14
|
+
#
|
15
|
+
# class Reports < ROM::Relation[:yesql]
|
16
|
+
# query_proc(proc { |name, query, *args|
|
17
|
+
# # magic if needed
|
18
|
+
# })
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# rom = ROM.finalize.env
|
22
|
+
#
|
23
|
+
# rom.relation(:reports) # use like a normal rom relation
|
24
|
+
#
|
25
|
+
# @api public
|
26
|
+
class Relation < ROM::Relation
|
27
|
+
defines :query_proc
|
28
|
+
|
29
|
+
# Extends a relation with query methods
|
30
|
+
#
|
31
|
+
# This will only kick in if the derived dataset name matches the key under
|
32
|
+
# which relation queries were registered. If not it is expected that the
|
33
|
+
# dataset will be set manually
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
def self.inherited(klass)
|
37
|
+
super
|
38
|
+
define_query_methods(klass, queries[klass.dataset] || {})
|
39
|
+
end
|
40
|
+
|
41
|
+
# Set dataset name for the relation class
|
42
|
+
#
|
43
|
+
# The class will be extended with queries registered under that name.
|
44
|
+
# By default dataset name is derived from the class name, which doesn't
|
45
|
+
# have to match they key under which its queries were registered
|
46
|
+
#
|
47
|
+
# @return [Symbol]
|
48
|
+
#
|
49
|
+
# @api public
|
50
|
+
def self.dataset(name = Undefined)
|
51
|
+
return @dataset if name == Undefined
|
52
|
+
@dataset = name
|
53
|
+
define_query_methods(self, Relation.queries[name] || {})
|
54
|
+
@dataset
|
55
|
+
end
|
56
|
+
|
57
|
+
# Extend provided klass with query methods
|
58
|
+
#
|
59
|
+
# @param [Class] klass A relation class
|
60
|
+
# @param [Hash] queries A hash with name, query pairs for the relation
|
61
|
+
#
|
62
|
+
# @api private
|
63
|
+
def self.define_query_methods(klass, queries)
|
64
|
+
queries.each do |name, query|
|
65
|
+
klass.class_eval do
|
66
|
+
define_method(name) do |*args|
|
67
|
+
ROM::Relation.new(dataset.read(query_proc.call(name, query, *args)))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# All loaded queries provided by repository
|
74
|
+
#
|
75
|
+
# @return [Hash]
|
76
|
+
#
|
77
|
+
# @api privatek
|
78
|
+
def self.queries
|
79
|
+
@queries || {}
|
80
|
+
end
|
81
|
+
|
82
|
+
# Hook called by a repository to load all configured queries
|
83
|
+
#
|
84
|
+
# @param [Hash] queries A hash with queries
|
85
|
+
#
|
86
|
+
# @api private
|
87
|
+
def self.load_queries(queries)
|
88
|
+
@queries = {}
|
89
|
+
queries.each do |ds, ds_queries|
|
90
|
+
@queries[ds] = ds_queries.each_with_object({}) do |(name, query), h|
|
91
|
+
h[name] = query
|
92
|
+
end
|
93
|
+
end
|
94
|
+
@queries
|
95
|
+
end
|
96
|
+
|
97
|
+
# Return query proc set on a relation class
|
98
|
+
#
|
99
|
+
# By default this returns whatever was set in the repository or the default
|
100
|
+
# one which simply uses hash % query to evaluate a query string
|
101
|
+
#
|
102
|
+
# @return [Proc]
|
103
|
+
#
|
104
|
+
# @api public
|
105
|
+
def query_proc
|
106
|
+
self.class.query_proc
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
require 'rom/yesql/dataset'
|
4
|
+
require 'rom/yesql/relation'
|
5
|
+
|
6
|
+
module ROM
|
7
|
+
module Yesql
|
8
|
+
# Yesql repository exposes access to configured SQL queries
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# # Load all queries from a specific path
|
12
|
+
# ROM::Yesql::Repository.new(uri, path: '/path/to/my_queries')
|
13
|
+
#
|
14
|
+
# # Provide queries explicitly using a hash
|
15
|
+
# ROM::Yesql::Repository.new(uri, queries: {
|
16
|
+
# reports: {
|
17
|
+
# all_users: 'SELECT * FROM users'
|
18
|
+
# }
|
19
|
+
# })
|
20
|
+
#
|
21
|
+
# # Override default query proc handler
|
22
|
+
# ROM::Yesql::Repository.new(uri, query_proc: proc { |name, query, *args|
|
23
|
+
# # do something to return an sql string
|
24
|
+
# })
|
25
|
+
#
|
26
|
+
# @api public
|
27
|
+
class Repository < ROM::Repository
|
28
|
+
include Options
|
29
|
+
|
30
|
+
option :path, reader: true
|
31
|
+
option :queries, type: Hash, default: EMPTY_HASH
|
32
|
+
option :query_proc, reader: true, default: proc { |repository|
|
33
|
+
proc do |_name, query, opts|
|
34
|
+
query % opts
|
35
|
+
end
|
36
|
+
}
|
37
|
+
|
38
|
+
# Return Sequel database connection
|
39
|
+
#
|
40
|
+
# @return [Sequel::Database]
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
attr_reader :connection
|
44
|
+
|
45
|
+
# Hash with loaded queries indexed using dataset names
|
46
|
+
#
|
47
|
+
# @return [Hash]
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
attr_reader :queries
|
51
|
+
|
52
|
+
# @api private
|
53
|
+
def initialize(uri, options = {})
|
54
|
+
super
|
55
|
+
@connection = Sequel.connect(uri, options)
|
56
|
+
initialize_queries
|
57
|
+
queries.freeze
|
58
|
+
Relation.query_proc(query_proc)
|
59
|
+
Relation.load_queries(queries)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Initialize a dataset
|
63
|
+
#
|
64
|
+
# Since all relations use the same dataset we simply create one instance
|
65
|
+
#
|
66
|
+
# @api private
|
67
|
+
def dataset(_name)
|
68
|
+
@dataset ||= Dataset.new(connection)
|
69
|
+
end
|
70
|
+
|
71
|
+
# @api private
|
72
|
+
def dataset?(_name)
|
73
|
+
! @dataset.nil?
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Load queries from filesystem if :path was provided
|
79
|
+
#
|
80
|
+
# @api private
|
81
|
+
def initialize_queries
|
82
|
+
@queries = options[:queries].dup
|
83
|
+
|
84
|
+
return unless path
|
85
|
+
|
86
|
+
Dir["#{path}/*"].each do |dir|
|
87
|
+
dataset = File.basename(dir).to_sym
|
88
|
+
@queries[dataset] = {}
|
89
|
+
Dir["#{dir}/**/*.sql"].each do |file|
|
90
|
+
name = File.basename(file, '.*').to_sym
|
91
|
+
sql = File.read(file)
|
92
|
+
@queries[dataset][name] = sql.strip
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/log/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,18 @@
|
|
1
|
+
begin
|
2
|
+
require "rubocop/rake_task"
|
3
|
+
|
4
|
+
Rake::Task[:default].enhance [:rubocop]
|
5
|
+
|
6
|
+
RuboCop::RakeTask.new do |task|
|
7
|
+
task.options << "--display-cop-names"
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :rubocop do
|
11
|
+
desc 'Generate a configuration file acting as a TODO list.'
|
12
|
+
task :auto_gen_config do
|
13
|
+
exec "bundle exec rubocop --auto-gen-config"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
rescue LoadError
|
18
|
+
end
|
data/rom-yesql.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rom/yesql/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rom-yesql"
|
8
|
+
spec.version = ROM::Yesql::VERSION.dup
|
9
|
+
spec.authors = ["Piotr Solnica"]
|
10
|
+
spec.email = ["piotr.solnica@gmail.com"]
|
11
|
+
spec.summary = 'Yesql databases support for ROM'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "http://rom-rb.org"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'rom', '~> 0.6.0.beta2'
|
22
|
+
spec.add_runtime_dependency 'sequel', '~> 4.19.0'
|
23
|
+
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
SELECT * FROM tasks WHERE id = :id:
|
@@ -0,0 +1 @@
|
|
1
|
+
SELECT * FROM users WHERE name = '%{name}'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'ROM / Yesql' do
|
4
|
+
include_context 'users and tasks'
|
5
|
+
|
6
|
+
let(:rom) { setup.finalize }
|
7
|
+
|
8
|
+
let!(:setup) do
|
9
|
+
ROM.setup(:yesql, [uri, path: path, queries: { reports: report_queries }])
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:report_queries) { { all_users: 'SELECT * FROM users ORDER BY %{order}' } }
|
13
|
+
|
14
|
+
let(:users) { rom.relation(:users) }
|
15
|
+
let(:tasks) { rom.relation(:tasks) }
|
16
|
+
let(:reports) { rom.relation(:reports) }
|
17
|
+
|
18
|
+
before do
|
19
|
+
class Users < ROM::Relation[:yesql]
|
20
|
+
end
|
21
|
+
|
22
|
+
class Tasks < ROM::Relation[:yesql]
|
23
|
+
query_proc(proc { |_name, query, opts| query.gsub(':id:', opts[:id].to_s) })
|
24
|
+
end
|
25
|
+
|
26
|
+
module Test
|
27
|
+
class Reports < ROM::Relation[:yesql]
|
28
|
+
dataset :reports
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'query method' do
|
34
|
+
it 'uses hash-based interpolation by default' do
|
35
|
+
expect(users.by_name(name: 'Jane')).to match_array([
|
36
|
+
{ id: 1, name: 'Jane' }
|
37
|
+
])
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'uses provided proc to preprocess a query' do
|
41
|
+
expect(tasks.by_id(id: 1)).to match_array([
|
42
|
+
{ id: 1, title: 'Task One' }
|
43
|
+
])
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'uses queries provided explicitly during setup' do
|
47
|
+
expect(reports.all_users(order: 'name').to_a).to eql([
|
48
|
+
{ id: 3, name: 'Jade' },
|
49
|
+
{ id: 1, name: 'Jane' },
|
50
|
+
{ id: 2, name: 'Joe' }
|
51
|
+
])
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns rom relation' do
|
55
|
+
relation = users.by_name(name: 'Jane') >> proc { |r| r.map { |t| t[:name] } }
|
56
|
+
expect(relation).to match_array(['Jane'])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
shared_context 'database setup' do
|
2
|
+
include_context 'repository setup'
|
3
|
+
|
4
|
+
let!(:conn) { Sequel.connect(uri) }
|
5
|
+
|
6
|
+
def drop_tables
|
7
|
+
[:users, :tasks].each { |name| conn.drop_table?(name) }
|
8
|
+
end
|
9
|
+
|
10
|
+
before do
|
11
|
+
conn.loggers << LOGGER
|
12
|
+
|
13
|
+
drop_tables
|
14
|
+
|
15
|
+
conn.create_table :users do
|
16
|
+
primary_key :id
|
17
|
+
String :name, null: false
|
18
|
+
index :name, unique: true
|
19
|
+
end
|
20
|
+
|
21
|
+
conn.create_table :tasks do
|
22
|
+
primary_key :id
|
23
|
+
String :title, null: false
|
24
|
+
index :title, unique: true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
shared_context 'repository setup' do
|
2
|
+
let(:root) { Pathname(__FILE__).dirname.join('../..') }
|
3
|
+
let(:path) { root.join('spec/fixtures') }
|
4
|
+
|
5
|
+
if RUBY_ENGINE == 'jruby'
|
6
|
+
let(:uri) { "jdbc:sqlite://#{root.join('db/test.sqlite')}" }
|
7
|
+
else
|
8
|
+
let(:uri) { "sqlite://#{root.join('db/test.sqlite')}" }
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
shared_context 'users and tasks' do
|
2
|
+
include_context 'database setup'
|
3
|
+
|
4
|
+
before do
|
5
|
+
conn[:users].insert id: 1, name: 'Jane'
|
6
|
+
conn[:users].insert id: 2, name: 'Joe'
|
7
|
+
conn[:users].insert id: 3, name: 'Jade'
|
8
|
+
|
9
|
+
conn[:tasks].insert id: 1, title: 'Task One'
|
10
|
+
conn[:tasks].insert id: 2, title: 'Task Two'
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.setup
|
5
|
+
|
6
|
+
if RUBY_ENGINE == 'rbx'
|
7
|
+
require "codeclimate-test-reporter"
|
8
|
+
CodeClimate::TestReporter.start
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rom-yesql'
|
12
|
+
# FIXME: why do we need to require it manually??
|
13
|
+
require 'sequel/adapters/sqlite' unless RUBY_ENGINE == 'jruby'
|
14
|
+
require 'inflecto'
|
15
|
+
require 'logger'
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'byebug'
|
19
|
+
rescue LoadError
|
20
|
+
end
|
21
|
+
|
22
|
+
LOGGER = Logger.new(File.open('./log/test.log', 'a'))
|
23
|
+
|
24
|
+
root = Pathname(__FILE__).dirname
|
25
|
+
|
26
|
+
Dir[root.join('shared/*.rb').to_s].each { |f| require f }
|
27
|
+
|
28
|
+
RSpec.configure do |config|
|
29
|
+
config.before do
|
30
|
+
@constants = Object.constants
|
31
|
+
end
|
32
|
+
|
33
|
+
config.after do
|
34
|
+
added_constants = Object.constants - @constants
|
35
|
+
added_constants.each { |name| Object.send(:remove_const, name) }
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM::Yesql::Repository do
|
4
|
+
include_context 'repository setup'
|
5
|
+
|
6
|
+
it 'loads queries from file system when :path is provided' do
|
7
|
+
repository = ROM::Yesql::Repository.new(uri, path: path)
|
8
|
+
|
9
|
+
expect(repository.queries.keys).to match_array([:users, :tasks])
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'combines queries from :queries option and loaded from provided :path' do
|
13
|
+
queries = { reports: { true: 'SELECT 1' } }
|
14
|
+
repository = ROM::Yesql::Repository.new(uri, path: path, queries: queries)
|
15
|
+
|
16
|
+
expect(repository.queries.keys).to match_array([:users, :tasks, :reports])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'loads queries from :queries option' do
|
20
|
+
queries = { reports: { true: 'SELECT 1' } }
|
21
|
+
repository = ROM::Yesql::Repository.new(uri, queries: queries)
|
22
|
+
|
23
|
+
expect(repository.queries).to eql(queries)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'loads empty queries hash when no options were provided' do
|
27
|
+
repository = ROM::Yesql::Repository.new(uri)
|
28
|
+
|
29
|
+
expect(repository.queries).to eql({})
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'freezes queries' do
|
33
|
+
queries = { reports: { true: 'SELECT 1' } }
|
34
|
+
repository = ROM::Yesql::Repository.new(uri, queries: queries)
|
35
|
+
|
36
|
+
expect(repository.queries).to be_frozen
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rom-yesql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Solnica
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rom
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.6.0.beta2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.6.0.beta2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sequel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.19.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.19.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Yesql databases support for ROM
|
70
|
+
email:
|
71
|
+
- piotr.solnica@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".rubocop_todo.yml"
|
80
|
+
- ".travis.yml"
|
81
|
+
- CHANGELOG.md
|
82
|
+
- Gemfile
|
83
|
+
- Guardfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- db/.gitkeep
|
88
|
+
- lib/rom-yesql.rb
|
89
|
+
- lib/rom/yesql.rb
|
90
|
+
- lib/rom/yesql/dataset.rb
|
91
|
+
- lib/rom/yesql/relation.rb
|
92
|
+
- lib/rom/yesql/repository.rb
|
93
|
+
- lib/rom/yesql/version.rb
|
94
|
+
- log/.gitkeep
|
95
|
+
- rakelib/rubocop.rake
|
96
|
+
- rom-yesql.gemspec
|
97
|
+
- spec/fixtures/tasks/by_id.sql
|
98
|
+
- spec/fixtures/users/by_name.sql
|
99
|
+
- spec/integration/adapter_spec.rb
|
100
|
+
- spec/shared/database_setup.rb
|
101
|
+
- spec/shared/repository_setup.rb
|
102
|
+
- spec/shared/users_and_tasks.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/unit/rom/yesql/repository_spec.rb
|
105
|
+
homepage: http://rom-rb.org
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Yesql databases support for ROM
|
129
|
+
test_files:
|
130
|
+
- spec/fixtures/tasks/by_id.sql
|
131
|
+
- spec/fixtures/users/by_name.sql
|
132
|
+
- spec/integration/adapter_spec.rb
|
133
|
+
- spec/shared/database_setup.rb
|
134
|
+
- spec/shared/repository_setup.rb
|
135
|
+
- spec/shared/users_and_tasks.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/unit/rom/yesql/repository_spec.rb
|
138
|
+
has_rdoc:
|