rql 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b12840bc8c62d856292ad37398531c76cb8ff422
4
- data.tar.gz: ab75b04337811bfff13a03a25f568bf30f7f49a8
3
+ metadata.gz: 7b4ab947240986e99b39fe5a70b079e1fbd6e047
4
+ data.tar.gz: 3e3962ff1f6062d0c39494f17adeebc443927697
5
5
  SHA512:
6
- metadata.gz: f4a85a36bd4190cc0938d1ccf59ee68d3d7a3386d36059a037fd7d7308f6876cfc07b03481e8e32fbbbd6b46baf606f87ce9197d367cdbd9960d9e167c438cbb
7
- data.tar.gz: 604923887ce5168558fce6a30d39b56cf66d727a8862e9ae59299ace1f8851ec1f340ddc9783054bcb34c05435c6f7c5ce6b78dc3098302793adf4f08a9d249e
6
+ metadata.gz: dd912988d5e371defaf25597e97b8f6b3b1c162be6f43e4c543eff1db2eca2d5af096de63cccd3982b350e91182137368f7306307b6e3550ed6323788e69cb15
7
+ data.tar.gz: 249aa513b6aa047032c23732900986e009e4b3090834c85b94995f2c10946c217a462f74a4cba043b7f504a925249547b0e8b37f3f93811dc3ea91f4f0801125
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Rql
1
+ # RQL
2
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/rql`. 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
3
+ RQL is a DSL for active record designed to allow derived/calculated attributes on models to be used in database queries.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,27 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ You can define a derived attribute in your model like this:
24
+ ```ruby
25
+ class Book < ActiveRecord::Base
26
+ include Rql::Queryable
27
+
28
+ derived_attr :leaves do
29
+ pages / 2
30
+ end
31
+ end
32
+ ```
33
+
34
+ and use it in a query like this:
35
+ ```ruby
36
+ Book.rql.where(leaves: 100)
37
+ ```
38
+
39
+ or
40
+
41
+ ```ruby
42
+ Book.rql.where{leaves > 100}
43
+ ```
26
44
 
27
45
  ## Development
28
46
 
@@ -32,7 +50,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
50
 
33
51
  ## Contributing
34
52
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rql.
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/liefni/rql.
36
54
 
37
55
  ## License
38
56
 
@@ -7,39 +7,44 @@ module Rql
7
7
  end
8
8
 
9
9
  def respond_to_missing?(method_name, include_private = false)
10
- derived(method_name) || model?(method_name) || attribute?(method_name) || super
10
+ derived?(method_name) || model?(method_name) || attribute?(method_name) || super
11
11
  end
12
12
 
13
13
  def method_missing(method_name, *params)
14
14
  if respond_to_missing?(method_name)
15
15
  raise ArgumentError.new("wrong number of arguments for `#{method_name}' (given #{params.length}, expected 0)") if (params.length > 0)
16
16
 
17
- derived(method_name) || build_context(method_name)
17
+ derive(method_name) || build_context(method_name)
18
18
  else
19
19
  super
20
20
  end
21
21
  end
22
22
 
23
- def build_context(name)
24
- Context.new(@model, @model.arel_table[name], name)
25
- end
23
+ protected
24
+ def build_context(name)
25
+ Context.new(@model, @model.arel_table[name], name)
26
+ end
26
27
 
27
- def derived(attribute)
28
- derived = @model.try(:derived_attributes)&.fetch(attribute, nil)
29
- if derived
30
- derived = instance_eval(&derived)
31
- derived = derived.as(attribute.to_s) if derived && @alias_derived_attr
28
+ def derive(attribute)
29
+ derived = @model.try(:derived_attributes)&.fetch(attribute, nil)
30
+ if derived
31
+ derived = instance_eval(&derived)
32
+ derived = derived.as(attribute.to_s) if derived && @alias_derived_attr
33
+ end
34
+ derived
32
35
  end
33
- derived
34
- end
35
36
 
36
- def model?(attribute)
37
- (attribute.to_s.classify.constantize rescue nil)&.ancestors&.include?(ActiveRecord::Base)
38
- end
37
+ def derived?(attribute)
38
+ !!@model.try(:derived_attributes)&.fetch(attribute, nil)
39
+ end
39
40
 
40
- def attribute?(attribute)
41
- @model.column_names.include?(attribute.to_s)
42
- end
41
+ def model?(attribute)
42
+ (attribute.to_s.classify.constantize rescue nil)&.ancestors&.include?(ActiveRecord::Base)
43
+ end
44
+
45
+ def attribute?(attribute)
46
+ @model.column_names.include?(attribute.to_s)
47
+ end
43
48
  end
44
49
  end
45
50
  end
@@ -7,11 +7,11 @@ module Rql
7
7
  include Orders
8
8
  include Logic
9
9
 
10
- def initialize(model, arel, name = nil, scope = nil)
11
- @model = model
10
+ def initialize(scope, arel, name = nil)
11
+ @scope = scope
12
+ @model = scope.unscoped
12
13
  @arel = arel
13
14
  @name = name
14
- @scope = scope || model
15
15
  end
16
16
 
17
17
  def arel
@@ -28,11 +28,13 @@ module Rql
28
28
  end
29
29
 
30
30
  def rql
31
- Context.new(@model, @arel, @name, @scope.rql)
31
+ Context.new(@scope.rql, @arel, @name)
32
32
  end
33
33
 
34
34
  def where(*attributes, &block)
35
- Context.new(@model, @arel, @name, @scope.where(*attributes, &block))
35
+ model = @name.to_s.classify.constantize
36
+ model = model.rql if @scope.is_a?(Scope::RqlScope)
37
+ Context.new(@scope.merge(model.where(*attributes, &block)), @arel, @name)
36
38
  end
37
39
  end
38
40
  end
@@ -9,8 +9,8 @@ module Rql
9
9
  return Scope::RqlScope.new(all)
10
10
  end
11
11
 
12
- def eval_rql(&query)
13
- Dsl::Base.new(self).instance_eval(&query)
12
+ def eval_rql(alias_derived_attr = false, &query)
13
+ Dsl::Base.new(self, alias_derived_attr).instance_eval(&query)
14
14
  end
15
15
 
16
16
  def derive_attr(name, &query)
@@ -6,7 +6,7 @@ module Rql
6
6
  end
7
7
 
8
8
  def select(&block)
9
- scope.select(*build_attributes(&block))
9
+ scope.select(*build_attributes(true, &block))
10
10
  end
11
11
 
12
12
  def order(&block)
@@ -28,8 +28,8 @@ module Rql
28
28
  joins.map(&:to_h)
29
29
  end
30
30
 
31
- def build_attributes(&block)
32
- attributes = scope.eval_rql(&block)
31
+ def build_attributes(alias_derived_attr = false, &block)
32
+ attributes = scope.eval_rql(alias_derived_attr, &block)
33
33
  attributes = [attributes] unless attributes.is_a?(Array)
34
34
  attributes.map(&:arel)
35
35
  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]&.arel&.as(attr.to_s) || attr})
9
+ scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(true, &scope.derived_attributes[attr]).arel : attr})
10
10
  end
11
11
 
12
12
  def order(*attributes)
@@ -23,10 +23,19 @@ module Rql
23
23
 
24
24
  def respond_to_missing?(method_name, include_private = false)
25
25
  @block_methods.respond_to?(method_name, include_private) ||
26
- @param_methods.send(method_name, include_private) ||
26
+ @param_methods.respond_to?(method_name, include_private) ||
27
27
  super.respond_to?(method_name, include_private) ||
28
28
  super
29
29
  end
30
+
31
+ def merge(other)
32
+ other = other.scope if other.is_a?(RqlScope)
33
+ RqlScope.new(scope.merge(other))
34
+ end
35
+
36
+ def to_a
37
+ scope.to_a
38
+ end
30
39
  end
31
40
  end
32
41
  end
@@ -1,3 +1,3 @@
1
1
  module Rql
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.16"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
- spec.add_development_dependency "sqlite3"
27
+ spec.add_development_dependency "pg"
28
28
 
29
- spec.add_runtime_dependency "activerecord", "> 5.0"
29
+ spec.add_runtime_dependency "activerecord", ">= 5.0"
30
30
  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.1.0
4
+ version: 0.1.1
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-13 00:00:00.000000000 Z
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sqlite3
56
+ name: pg
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -70,14 +70,14 @@ dependencies:
70
70
  name: activerecord
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '5.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
83
  description: RQL is a DSL that allows derived attributes in Active Record models to