arqo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +148 -0
- data/lib/arqo.rb +9 -0
- data/lib/arqo/query.rb +27 -0
- data/lib/arqo/version.rb +5 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1887e4c98d7848ac3d743b338ea8aeaf26f3d643
|
4
|
+
data.tar.gz: 22d1f86473d4adc3437aedc2401fc64eb040fd70
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c0b3222b5b3023d9eb4d16a6bf9d244414523e19dc2e4295e0f1ff963fc08bde610fd55656a8e1881ca63a7e891f48578580f192945cd99b27b92cf5f36af10
|
7
|
+
data.tar.gz: 9214d81840071fd81d134b89006fa4ff99bc50702b0ae9a81137cf089afa3db208553d2f8a88f4ecb451819a60c4677218bc00316d25f87c55897142476c89a3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Santiago Bartesaghi
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
# ARQO 
|
2
|
+
|
3
|
+
ARQO (Active Record Query Objects) is a minimal gem that let you use Query Objects in an easy and Rails friendly way. It leverages `ActiveRecord` features and tries to make query objects as intuitive as possible for developers. In combination with the documentation we hope `ARQO` helps people keep their projects well structured and healthy.
|
4
|
+
|
5
|
+

|
6
|
+
[](https://codeclimate.com/github/rootstrap/arqo/maintainability)
|
7
|
+
[](https://codeclimate.com/github/rootstrap/arqo/test_coverage)
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
- [Motivation](#motivation)
|
12
|
+
- [Why ARQO?](#why-arqo)
|
13
|
+
- [Installation](#installation)
|
14
|
+
- [Usage](#usage)
|
15
|
+
- [Setting up a query object](#setting-up-a-query-object)
|
16
|
+
- [Deriving the model](#deriving-the-model)
|
17
|
+
- [Chaining scopes](#chaining-scopes)
|
18
|
+
- [Development](#development)
|
19
|
+
- [Contributing](#contributing)
|
20
|
+
- [License](#license)
|
21
|
+
- [Code of Conduct](#code-of-conduct)
|
22
|
+
- [Credits](#credits)
|
23
|
+
|
24
|
+
## Motivation
|
25
|
+
|
26
|
+
`ActiveRecord` provides us with an amazing abstraction of the database structure, allowing us to write queries in a simple way. Unfortunately, models can grow large for several reasons, one of them being adding a lot of scopes or placing querying logic in methods.
|
27
|
+
|
28
|
+
For this reason is that we created `ARQO`, so that the query logic is placed into specific objects responsible for building queries while not losing any of the benefits that Rails gives us.
|
29
|
+
|
30
|
+
### Why ARQO?
|
31
|
+
|
32
|
+
- It is really simple, but still enough to have the best of Rails & query objects.
|
33
|
+
|
34
|
+
- It will dynamically add scopes to your `ActiveRecord::Relation` instances, clearly making a separation of concerns by not making them accessible through the model.
|
35
|
+
|
36
|
+
- It supports chaining methods defined in the query object just like when using Rails `scope`s.
|
37
|
+
|
38
|
+
- It centralizes the querying logic of your models in a single source of truth.
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'arqo'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle install
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install arqo
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
In the following sections we explain some basic usage and the API provided by the gem.
|
59
|
+
|
60
|
+
### Setting up a query object
|
61
|
+
|
62
|
+
In order to use an `ARQO` query object, you need to inherit from `ARQO::Query` and define the `Scope` module inside it. Methods should be defined within the `Scope` module like this:
|
63
|
+
```ruby
|
64
|
+
# app/queries/user_query.rb
|
65
|
+
|
66
|
+
class UserQuery < ARQO::Query
|
67
|
+
module Scope
|
68
|
+
def active_last_week
|
69
|
+
where('last_active_at > ?', 1.week.ago)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
And then you can use it from anywhere in your code.
|
76
|
+
```ruby
|
77
|
+
UserQuery.new.active_last_week
|
78
|
+
```
|
79
|
+
|
80
|
+
## Deriving the model
|
81
|
+
In this previous example, the model was derived from the query object name. In case it's not derivable you should provide the `ActiveRecord::Relation` when initializing the query object, for example if you have:
|
82
|
+
```ruby
|
83
|
+
# app/queries/custom_named_query.rb
|
84
|
+
|
85
|
+
class CustomNamedQuery < ARQO::Query
|
86
|
+
module Scope
|
87
|
+
def active_last_week
|
88
|
+
where('last_active_at > ?', 1.week.ago)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
you can use it like this:
|
95
|
+
```ruby
|
96
|
+
CustomNamedQuery.new(User.all).active_last_week
|
97
|
+
```
|
98
|
+
|
99
|
+
## Chaining scopes
|
100
|
+
Of course you can chain everything together, methods defined in the query object and scopes defined in the model or by Rails.
|
101
|
+
```ruby
|
102
|
+
# app/queries/user_query.rb
|
103
|
+
|
104
|
+
class UserQuery < ARQO::Query
|
105
|
+
module Scope
|
106
|
+
def active_last_week
|
107
|
+
where('last_active_at > ?', 1.week.ago)
|
108
|
+
end
|
109
|
+
|
110
|
+
def not_deleted
|
111
|
+
where(deleted_at: nil)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
And then you chain everything together and it will just work :)
|
118
|
+
```ruby
|
119
|
+
UserQuery.new.where.not(name: nil).active_last_week.not_deleted.order(:id)
|
120
|
+
```
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
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.
|
125
|
+
|
126
|
+
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).
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/santib/arqo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/santib/arqo/blob/master/CODE_OF_CONDUCT.md).
|
131
|
+
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
136
|
+
|
137
|
+
## Code of Conduct
|
138
|
+
|
139
|
+
Everyone interacting in the ARQO project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/santib/arqo/blob/master/CODE_OF_CONDUCT.md).
|
140
|
+
|
141
|
+
## Logo attribution
|
142
|
+
Logo made by [iconixar](https://www.flaticon.com/free-icon/archery_3080892) from [www.flaticon.com](https://www.flaticon.com/)
|
143
|
+
|
144
|
+
## Credits
|
145
|
+
|
146
|
+
ARQO is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our [contributors](https://github.com/rootstrap/arqo/contributors).
|
147
|
+
|
148
|
+
[<img src="https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png" width="100"/>](http://www.rootstrap.com)
|
data/lib/arqo.rb
ADDED
data/lib/arqo/query.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ARQO
|
4
|
+
# Parent class for query objects
|
5
|
+
class Query
|
6
|
+
attr_reader :relation
|
7
|
+
delegate_missing_to :relation
|
8
|
+
|
9
|
+
def initialize(relation = derived_relation)
|
10
|
+
@relation = relation.extending(scope_module)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def derived_relation_name
|
16
|
+
self.class.name.sub(/Query$/, '')
|
17
|
+
end
|
18
|
+
|
19
|
+
def derived_relation
|
20
|
+
derived_relation_name.constantize.all if Object.const_defined?(derived_relation_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def scope_module
|
24
|
+
"#{self.class}::Scope".constantize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/arqo/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arqo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Santiago Bartesaghi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: database_cleaner-active_record
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.8.0
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.8.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 13.0.1
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 13.0.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: reek
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 5.6.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 5.6.0
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 3.9.0
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 3.9.0
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rubocop
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.80.0
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.80.0
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.17.1
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 0.17.1
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: sqlite3
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 1.4.2
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 1.4.2
|
131
|
+
description:
|
132
|
+
email:
|
133
|
+
- sbartesaghi@hotmail.com
|
134
|
+
executables: []
|
135
|
+
extensions: []
|
136
|
+
extra_rdoc_files: []
|
137
|
+
files:
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- lib/arqo.rb
|
141
|
+
- lib/arqo/query.rb
|
142
|
+
- lib/arqo/version.rb
|
143
|
+
homepage: https://github.com/santib/arqo
|
144
|
+
licenses:
|
145
|
+
- MIT
|
146
|
+
metadata:
|
147
|
+
homepage_uri: https://github.com/santib/arqo
|
148
|
+
source_code_uri: https://github.com/santib/arqo
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 2.3.0
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.5.2.3
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Easing the query object pattern in Rails applications.
|
169
|
+
test_files: []
|