rql 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/lib/rql/dsl/comparisons.rb +2 -2
- data/lib/rql/dsl/context.rb +4 -2
- data/lib/rql/queryable.rb +5 -5
- data/lib/rql/scope/block_methods.rb +25 -1
- data/lib/rql/scope/param_methods.rb +25 -1
- data/lib/rql/scope/rql_scope.rb +19 -2
- data/lib/rql/version.rb +1 -1
- data/rql.gemspec +2 -0
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73bfb99910e53072a8efb800ec7e1d16ce2a6c9c
|
4
|
+
data.tar.gz: 70e3691955083453373248274ed088aa18298290
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f22889e8798227e85c0fcba82f12151b88ee40879053d2063ec9aa986783acbf0dec5f61c06f9c80fdae58a2215696238ee812feabcf6116eee8aeb1470ecb9
|
7
|
+
data.tar.gz: 286d67606f9049498a480d88b4f3e1826abede59c14f12354d57990cc5d3b6ff4b6475e94c8e4b63741f8cfb48b67761412f2f607c9a46ff685ec34ddc603e98
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--plugin activesupport-concern
|
data/lib/rql/dsl/comparisons.rb
CHANGED
@@ -27,11 +27,11 @@ module Rql
|
|
27
27
|
Context.new(@model, arel.gteq(value))
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def start_with?(value)
|
31
31
|
Context.new(@model, arel.matches("#{value}%"))
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
34
|
+
def end_with?(value)
|
35
35
|
Context.new(@model, arel.matches("%#{value}"))
|
36
36
|
end
|
37
37
|
|
data/lib/rql/dsl/context.rb
CHANGED
@@ -7,6 +7,8 @@ module Rql
|
|
7
7
|
include Orders
|
8
8
|
include Logic
|
9
9
|
|
10
|
+
attr_accessor :arel, :scope, :model
|
11
|
+
|
10
12
|
def initialize(scope, arel, name = nil)
|
11
13
|
@scope = scope
|
12
14
|
@model = scope.unscoped
|
@@ -14,8 +16,8 @@ module Rql
|
|
14
16
|
@name = name
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
18
|
-
@
|
19
|
+
def context_name
|
20
|
+
@name
|
19
21
|
end
|
20
22
|
|
21
23
|
def build_context(name)
|
data/lib/rql/queryable.rb
CHANGED
@@ -5,7 +5,7 @@ module Rql
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
class_methods do
|
8
|
-
# Gets
|
8
|
+
# Gets an RQL scope object for access to rql query methods
|
9
9
|
#
|
10
10
|
# @return [Rql::Scope::RqlScope] object wrapping the current scope
|
11
11
|
def rql
|
@@ -15,7 +15,7 @@ module Rql
|
|
15
15
|
# Evaluates a block of code against the RQL DSL
|
16
16
|
#
|
17
17
|
# @param alias_derived_attr [Boolean] value specifying if derived attributes will be assigned an alias
|
18
|
-
# @
|
18
|
+
# @yield the code block to be converted to sql
|
19
19
|
# @return [Rql::Dsl::Context] object wrapping arel for the evaluated block of code
|
20
20
|
def eval_rql(alias_derived_attr = false, &block)
|
21
21
|
Dsl::Base.new(self, alias_derived_attr).instance_eval(&block)
|
@@ -24,7 +24,7 @@ module Rql
|
|
24
24
|
# Defines a derived attribute on a model
|
25
25
|
#
|
26
26
|
# @param name [Symbol] the name of the derived attribute
|
27
|
-
# @
|
27
|
+
# @yield the code to derive the attribute
|
28
28
|
# @!macro [attach] derive_attr
|
29
29
|
# @return [Object] the value of the derived attribute
|
30
30
|
def derive_attr(name, &block)
|
@@ -37,7 +37,7 @@ module Rql
|
|
37
37
|
|
38
38
|
# Preloads the specified derived attributes from the database
|
39
39
|
#
|
40
|
-
# @param
|
40
|
+
# @param attributes [Array<Symbol>] the attributes to be preloaded
|
41
41
|
# @return scope including derived attributes
|
42
42
|
def derive(*attributes)
|
43
43
|
rql.select(self.arel_table[Arel.star], *attributes).all
|
@@ -45,7 +45,7 @@ module Rql
|
|
45
45
|
|
46
46
|
# Gets the derived attributes defined on the model
|
47
47
|
#
|
48
|
-
# @
|
48
|
+
# @return [Hash<Symbol, Proc>] hash of derived attributes indexed by name
|
49
49
|
def derived_attributes
|
50
50
|
@derive_attribute ||= {}
|
51
51
|
end
|
@@ -1,22 +1,42 @@
|
|
1
1
|
module Rql
|
2
2
|
module Scope
|
3
|
-
|
3
|
+
module BlockMethods
|
4
|
+
# Filters scope based on specified conditions
|
5
|
+
#
|
6
|
+
# @yield RQL block defining the conditions to filter on
|
7
|
+
# @return [Rql::Scope::RqlScope] a new scope filtered by the specified conditions
|
4
8
|
def where(&block)
|
5
9
|
scope.where(scope.eval_rql(&block).arel)
|
6
10
|
end
|
7
11
|
|
12
|
+
# Specifies attributes to be selected from the database
|
13
|
+
#
|
14
|
+
# @yield RQL block defining the attributes to be selected
|
15
|
+
# @return [Rql::Scope::RqlScope] a new scope selecting the specified attributes
|
8
16
|
def select(&block)
|
9
17
|
scope.select(*build_attributes(true, &block))
|
10
18
|
end
|
11
19
|
|
20
|
+
# Orders by specified attributes
|
21
|
+
#
|
22
|
+
# @yield RQL block defining the attributes order by
|
23
|
+
# @return [Rql::Scope::RqlScope] a new scope ordered by the specified attributes
|
12
24
|
def order(&block)
|
13
25
|
scope.order(*build_attributes(&block))
|
14
26
|
end
|
15
27
|
|
28
|
+
# Joins to the specified associated models
|
29
|
+
#
|
30
|
+
# @yield RQL block defining the associations to join to
|
31
|
+
# @return [Rql::Scope::RqlScope] a new scope joining the specified associations
|
16
32
|
def joins(&block)
|
17
33
|
scope.joins(*build_join_path(&block))
|
18
34
|
end
|
19
35
|
|
36
|
+
# Includes the specified associated models
|
37
|
+
#
|
38
|
+
# @yield RQL block defining the associations to include
|
39
|
+
# @return [Rql::Scope::RqlScope] a new scope including the specified associations
|
20
40
|
def includes(&block)
|
21
41
|
scope.includes(*build_join_path(&block))
|
22
42
|
end
|
@@ -34,5 +54,9 @@ module Rql
|
|
34
54
|
attributes.map(&:arel)
|
35
55
|
end
|
36
56
|
end
|
57
|
+
|
58
|
+
class BlockMethodGroup < MethodGroup
|
59
|
+
include BlockMethods
|
60
|
+
end
|
37
61
|
end
|
38
62
|
end
|
@@ -1,22 +1,42 @@
|
|
1
1
|
module Rql
|
2
2
|
module Scope
|
3
|
-
|
3
|
+
module ParamMethods
|
4
|
+
# Filters scope based on specified conditions
|
5
|
+
#
|
6
|
+
# @param conditions [Hash] A hash of attribute => value conditions to filter on. Use nested hashes to filter on associations. Supports ranges and arrays as values.
|
7
|
+
# @return [Rql::Scope::RqlScope] a new scope filtered by the specified conditions
|
4
8
|
def where(**conditions)
|
5
9
|
scope.where(build_conditions(conditions))
|
6
10
|
end
|
7
11
|
|
12
|
+
# Specifies attributes to be selected from the database
|
13
|
+
#
|
14
|
+
# @param attributes [Array] the attributes to select
|
15
|
+
# @return [Rql::Scope::RqlScope] a new scope selecting the specified attributes
|
8
16
|
def select(*attributes)
|
9
17
|
scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
|
10
18
|
end
|
11
19
|
|
20
|
+
# Orders by specified attributes
|
21
|
+
#
|
22
|
+
# @param attributes [Array] the attributes to order by. Attributes may be a symbol, sql string or a hash specifying attribute and order.
|
23
|
+
# @return [Rql::Scope::RqlScope] a new scope ordered by the specified attributes
|
12
24
|
def order(*attributes)
|
13
25
|
scope.order(*build_order(attributes))
|
14
26
|
end
|
15
27
|
|
28
|
+
# Joins to the specified associated models
|
29
|
+
#
|
30
|
+
# @param attributes [Array] the associations to join to. Use nested hashes to chain joins through associations.
|
31
|
+
# @return [Rql::Scope::RqlScope] a new scope joining the specified associations
|
16
32
|
def joins(*attributes)
|
17
33
|
scope.joins(*attributes)
|
18
34
|
end
|
19
35
|
|
36
|
+
# Includes the specified associated models
|
37
|
+
#
|
38
|
+
# @param attributes [Array] the associations to include. Use nested hashes to include associations of associations.
|
39
|
+
# @return [Rql::Scope::RqlScope] a new scope including the specified associations
|
20
40
|
def includes(*attributes)
|
21
41
|
scope.includes(*attributes)
|
22
42
|
end
|
@@ -54,5 +74,9 @@ module Rql
|
|
54
74
|
order_by.flatten
|
55
75
|
end
|
56
76
|
end
|
77
|
+
|
78
|
+
class ParamMethodGroup < MethodGroup
|
79
|
+
include ParamMethods
|
80
|
+
end
|
57
81
|
end
|
58
82
|
end
|
data/lib/rql/scope/rql_scope.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
module Rql
|
2
2
|
module Scope
|
3
3
|
class RqlScope
|
4
|
+
# @!parse include Rql::Scope::ParamMethods
|
5
|
+
# @!parse include Rql::Scope::BlockMethods
|
6
|
+
|
7
|
+
# @param scope [ActiveRecord::Relation] the underlying scope to wrap
|
4
8
|
def initialize(scope)
|
5
|
-
@block_methods =
|
6
|
-
@param_methods =
|
9
|
+
@block_methods = BlockMethodGroup.new(scope)
|
10
|
+
@param_methods = ParamMethodGroup.new(scope)
|
7
11
|
@scope = scope
|
8
12
|
end
|
9
13
|
|
14
|
+
# Gets the underlying ActiveRecord Relation object for the scope
|
15
|
+
#
|
16
|
+
# @return [ActiveRecord::Relation] the underlying arel object
|
10
17
|
def scope
|
11
18
|
@scope
|
12
19
|
end
|
@@ -28,15 +35,25 @@ module Rql
|
|
28
35
|
super
|
29
36
|
end
|
30
37
|
|
38
|
+
# Creates a new scope merging the current scope with the other scope
|
39
|
+
#
|
40
|
+
# @param [Rql::Scope::RqlScope, ActiveRecord::Relation] other the scope to merge with the current one.
|
41
|
+
# @return [Rql::Scope::RqlScope] the new merged scope
|
31
42
|
def merge(other)
|
32
43
|
other = other.scope if other.is_a?(RqlScope)
|
33
44
|
RqlScope.new(scope.merge(other))
|
34
45
|
end
|
35
46
|
|
47
|
+
# Gets the underlying arel object for the scope
|
48
|
+
#
|
49
|
+
# @return [Arel::SelectManager] the underlying arel object
|
36
50
|
def arel
|
37
51
|
scope.arel
|
38
52
|
end
|
39
53
|
|
54
|
+
# Executes the scope against the database returning the results as an array
|
55
|
+
#
|
56
|
+
# @return [Array] the results as an array
|
40
57
|
def to_a
|
41
58
|
scope.to_a
|
42
59
|
end
|
data/lib/rql/version.rb
CHANGED
data/rql.gemspec
CHANGED
@@ -25,6 +25,8 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.0"
|
27
27
|
spec.add_development_dependency "pg"
|
28
|
+
spec.add_development_dependency "simplecov"
|
29
|
+
spec.add_development_dependency "yard-activesupport-concern"
|
28
30
|
|
29
31
|
spec.add_runtime_dependency "activerecord", ">= 5.0"
|
30
32
|
end
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,34 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: yard-activesupport-concern
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
98
|
name: activerecord
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,6 +121,7 @@ files:
|
|
93
121
|
- ".gitignore"
|
94
122
|
- ".rspec"
|
95
123
|
- ".travis.yml"
|
124
|
+
- ".yardopts"
|
96
125
|
- CODE_OF_CONDUCT.md
|
97
126
|
- CONTRIBUTING.md
|
98
127
|
- Gemfile
|