rql 0.1.1 → 0.1.2
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 +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +26 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
- data/CODE_OF_CONDUCT.md +73 -0
- data/CONTRIBUTING.md +3 -0
- data/PULL_REQUEST_TEMPLATE.md +7 -0
- data/README.md +52 -8
- data/lib/rql/queryable.rb +26 -5
- data/lib/rql/scope/param_methods.rb +1 -1
- data/lib/rql/scope/rql_scope.rb +4 -0
- data/lib/rql/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 084bde9b357cebc0e8e7d46ccddc7158bd1b0365
|
4
|
+
data.tar.gz: 87759ddc7eaf7f44d495d148cb1d6ef2039b1ee2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c05432fb7fe1f44af0eea3f5ff4767debf22a3d18f07658af5358587e4418719ff1333cb875d5eb3e8b8753b65b15aa8cf2d6f8166c52e6ed03745552510636
|
7
|
+
data.tar.gz: 65573f3f235301296dcf4240374c2aa90b8e9476ae4762860cd140f4b088de9ca06bb30989bdbdb61c4500e0d052664bcc43f0a4c23b9102530702e2d3fc7463
|
@@ -0,0 +1,26 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
**Describe the bug**
|
8
|
+
A clear and concise description of what the bug is.
|
9
|
+
|
10
|
+
**To Reproduce**
|
11
|
+
Steps to reproduce the behavior
|
12
|
+
|
13
|
+
**Expected behavior**
|
14
|
+
A clear and concise description of what you expected to happen.
|
15
|
+
|
16
|
+
**(please complete the following information if relevant):**
|
17
|
+
- OS: [e.g. MacOS 10.13.5]
|
18
|
+
- Gem Version [e.g. 0.1.1]
|
19
|
+
- Ruby Version
|
20
|
+
- Rails Version
|
21
|
+
- DBMS/Adapter
|
22
|
+
- Stack trace
|
23
|
+
- Related code and data
|
24
|
+
|
25
|
+
**Additional context**
|
26
|
+
Add any other context about the problem here.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
**Is your feature request related to a problem? Please describe.**
|
8
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
9
|
+
|
10
|
+
**Describe the solution you'd like**
|
11
|
+
A clear and concise description of what you want to happen.
|
12
|
+
|
13
|
+
**Describe alternatives you've considered**
|
14
|
+
A clear and concise description of any alternative solutions or features you've considered.
|
15
|
+
|
16
|
+
**Additional context**
|
17
|
+
Add any other context or screenshots about the feature request here.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
education, socio-economic status, nationality, personal appearance, race,
|
10
|
+
religion, or sexual identity and orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at [INSERT EMAIL ADDRESS]. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
72
|
+
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,3 @@
|
|
1
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/liefni/rql. This project is intended to be a
|
2
|
+
safe, welcoming space for collaboration, and contributors are expected to adhere to the
|
3
|
+
[Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
# RQL
|
1
|
+
# RQL [](http://travis-ci.org/liefni/rql)
|
2
2
|
|
3
|
-
RQL is a DSL for
|
3
|
+
RQL is a DSL for ActiveRecord designed to allow derived/calculated attributes on models to be used in database queries.
|
4
|
+
|
5
|
+
[Api Documentation](https://www.rubydoc.info/gems/rql/)
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -20,7 +22,7 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
23
|
-
|
25
|
+
Define a derived attribute in your model:
|
24
26
|
```ruby
|
25
27
|
class Book < ActiveRecord::Base
|
26
28
|
include Rql::Queryable
|
@@ -31,26 +33,68 @@ class Book < ActiveRecord::Base
|
|
31
33
|
end
|
32
34
|
```
|
33
35
|
|
34
|
-
|
36
|
+
then use it in a query:
|
35
37
|
```ruby
|
36
38
|
Book.rql.where(leaves: 100)
|
37
39
|
```
|
38
40
|
|
39
|
-
or
|
41
|
+
or using the dsl
|
40
42
|
|
41
43
|
```ruby
|
42
44
|
Book.rql.where{leaves > 100}
|
43
45
|
```
|
44
46
|
|
47
|
+
### Aggregate Functions
|
48
|
+
|
49
|
+
Aggregate functions can be used in derived attributes:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Author < ActiveRecord::Base
|
53
|
+
include Rql::Queryable
|
54
|
+
|
55
|
+
has_many :books
|
56
|
+
|
57
|
+
derive_attr :total_books do
|
58
|
+
books.count
|
59
|
+
end
|
60
|
+
|
61
|
+
derive_attr :total_pages do
|
62
|
+
books.sum(:page)
|
63
|
+
end
|
64
|
+
|
65
|
+
derive_attr :long_books do
|
66
|
+
books.rql.where{pages > 300}.count
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Preloading
|
72
|
+
|
73
|
+
Derived attributes can be preloaded from the database database to prevent multiple calls to the database when accessing
|
74
|
+
derived attributes that use associated models. To preload derived attributes:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
Authors.derive(:total_books, :total_pages)
|
78
|
+
```
|
79
|
+
|
80
|
+
If a derived attribute has not been preloaded then the code will be evaluated in the context of the model when it is
|
81
|
+
accessed. For instance calling `Book.first.leaves` will return `pages / 2` just as a normal method would, without having
|
82
|
+
to query the database.
|
83
|
+
|
45
84
|
## Development
|
46
85
|
|
47
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
86
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
87
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
88
|
|
49
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
90
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
91
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
92
|
|
51
93
|
## Contributing
|
52
94
|
|
53
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/liefni/rql.
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/liefni/rql. This project is intended to be a
|
96
|
+
safe, welcoming space for collaboration, and contributors are expected to adhere to the
|
97
|
+
[Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
54
98
|
|
55
99
|
## License
|
56
100
|
|
data/lib/rql/queryable.rb
CHANGED
@@ -5,26 +5,47 @@ module Rql
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
class_methods do
|
8
|
+
# Gets and RQL scope object for access to rql query methods
|
9
|
+
#
|
10
|
+
# @return [Rql::Scope::RqlScope] object wrapping the current scope
|
8
11
|
def rql
|
9
12
|
return Scope::RqlScope.new(all)
|
10
13
|
end
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
# Evaluates a block of code against the RQL DSL
|
16
|
+
#
|
17
|
+
# @param alias_derived_attr [Boolean] value specifying if derived attributes will be assigned an alias
|
18
|
+
# @param &block [Block] the code block to be converted to sql
|
19
|
+
# @return [Rql::Dsl::Context] object wrapping arel for the evaluated block of code
|
20
|
+
def eval_rql(alias_derived_attr = false, &block)
|
21
|
+
Dsl::Base.new(self, alias_derived_attr).instance_eval(&block)
|
14
22
|
end
|
15
23
|
|
16
|
-
|
24
|
+
# Defines a derived attribute on a model
|
25
|
+
#
|
26
|
+
# @param name [Symbol] the name of the derived attribute
|
27
|
+
# @param &block [Block] the code to derive the attribute
|
28
|
+
# @!macro [attach] derive_attr
|
29
|
+
# @return [Object] the value of the derived attribute
|
30
|
+
def derive_attr(name, &block)
|
17
31
|
define_method(name) do
|
18
|
-
attributes.has_key?(name.to_s) ? attributes[name.to_s] : instance_eval(&
|
32
|
+
attributes.has_key?(name.to_s) ? attributes[name.to_s] : instance_eval(&block)
|
19
33
|
end
|
20
34
|
|
21
|
-
derived_attributes[name] =
|
35
|
+
derived_attributes[name] = block
|
22
36
|
end
|
23
37
|
|
38
|
+
# Preloads the specified derived attributes from the database
|
39
|
+
#
|
40
|
+
# @param *attributes [Array<Symbol>] the attributes to be preloaded
|
41
|
+
# @return scope including derived attributes
|
24
42
|
def derive(*attributes)
|
25
43
|
rql.select(self.arel_table[Arel.star], *attributes).all
|
26
44
|
end
|
27
45
|
|
46
|
+
# Gets the derived attributes defined on the model
|
47
|
+
#
|
48
|
+
# @returns [Hash<Symbol, Proc>] hash of derived attributes indexed by name
|
28
49
|
def derived_attributes
|
29
50
|
@derive_attribute ||= {}
|
30
51
|
end
|
@@ -6,7 +6,7 @@ module Rql
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def select(*attributes)
|
9
|
-
scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(
|
9
|
+
scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
|
10
10
|
end
|
11
11
|
|
12
12
|
def order(*attributes)
|
data/lib/rql/scope/rql_scope.rb
CHANGED
data/lib/rql/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lief Nikkel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,11 +88,16 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
+
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
92
|
+
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
91
93
|
- ".gitignore"
|
92
94
|
- ".rspec"
|
93
95
|
- ".travis.yml"
|
96
|
+
- CODE_OF_CONDUCT.md
|
97
|
+
- CONTRIBUTING.md
|
94
98
|
- Gemfile
|
95
99
|
- LICENSE.txt
|
100
|
+
- PULL_REQUEST_TEMPLATE.md
|
96
101
|
- README.md
|
97
102
|
- Rakefile
|
98
103
|
- bin/console
|