rql 0.2.1 → 0.2.2

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: da24e3445d8f2175928c882b2cabe6b43e54452b
4
- data.tar.gz: 6b0326c51d3d243cbebfc2d4a9a2b44fa1f1b943
3
+ metadata.gz: 377409fcba26dcedaba557963e2b24c98aaeb947
4
+ data.tar.gz: 161b3d6a9495f23f604c85ee1a6622f925682e5b
5
5
  SHA512:
6
- metadata.gz: 4e055053881fef20bf297c193a7efab0304816676a2a457cf369984aa22f420aa2fb3f5e4c7882fc8229289262d1eaed45364c635150b5067b3fc8e4951b1aa5
7
- data.tar.gz: d564ddb101947806963720c92bdc6c62ffadd987e3ca2bdd13d2994600f04248fb234c9903eb18cd99b397ac582e688b4b4c31a29fea369ffd49c8e7a1ccb8ed
6
+ metadata.gz: 0ce38ebc39256f5b07243409eed49efe1501209a41282756f193c812a0f0a74c7425788083ad056c3aca8996b44885088c4bc188091c94d992999c4e738e25c9
7
+ data.tar.gz: 5717564360f289e9a86bb0f1f204f966d63f366807e786d644b2c28fb7d7835123bb72124f5d63f2ad6ca21797328d065e8fe721a052d97c7dcd83c1532ec160
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RQL [![Build Status](https://secure.travis-ci.org/liefni/rql.svg)](http://travis-ci.org/liefni/rql) [![Gem Version](https://badge.fury.io/rb/rql.svg)](https://badge.fury.io/rb/rql)
2
2
 
3
- RQL is a DSL for ActiveRecord designed to allow derived/calculated attributes on models to be used in database queries.
3
+ RQL is a DSL for Ruby on Rails' ActiveRecord, designed to allow derived/calculated attributes on models to be used in database queries.
4
4
 
5
5
  [Api Documentation](https://www.rubydoc.info/gems/rql/)
6
6
 
@@ -81,6 +81,32 @@ If a derived attribute has not been preloaded then the code will be evaluated in
81
81
  accessed. For instance calling `Book.first.leaves` will return `pages / 2` just as a normal method would, without having
82
82
  to query the database.
83
83
 
84
+ ### Case Sensitivity and Regexp
85
+
86
+ As strings in ruby are case sensitive, to get consistent results when derived attributes are both run is the context of
87
+ model and in the database you will need to configure your database to be case sensitive.
88
+
89
+ To perform case-insensitive queries downcase the values you are comparing e.g.
90
+
91
+ ```ruby
92
+ Authors.where{name.downcase == my_string.downcase}
93
+ ```
94
+
95
+ Alternatively you can use regexp matching with the =~ operator. This is case sensitive by default but supports the case
96
+ insensitive flag.
97
+
98
+ Case sensitive regexp match:
99
+
100
+ ```ruby
101
+ Authors.where{name =~ /[A-Za-z]/}
102
+ ```
103
+
104
+ Case insensitive match using i flag:
105
+
106
+ ```ruby
107
+ Authors.where{name =~ /[A-Z]/i}
108
+ ```
109
+
84
110
  ### Query DSL
85
111
 
86
112
  #### Operators
@@ -94,6 +120,7 @@ to query the database.
94
120
  |< |`Book.rql.where {pages < 200}` |`SELECT * FROM "book" WHERE "book"."pages" < 200` |
95
121
  |>= |`Book.rql.where {pages >= 200}` |`SELECT * FROM "book" WHERE "book"."pages" >= 200` |
96
122
  |<= |`Book.rql.where {pages <= 200}` |`SELECT * FROM "book" WHERE "book"."pages" <= 200` |
123
+ |=~ |`Book.rql.where {name =~ /^[A-Z]*$/}` |`SELECT * FROM "book" WHERE "book"."name" ~ '^[A-Z]*$'` |
97
124
  |+ |`Book.rql.where {pages + 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" + 2) = 200` |
98
125
  |- |`Book.rql.where {pages - 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" - 2) = 200` |
99
126
  |* |`Book.rql.where {pages * 2 == 200}` |`SELECT * FROM "book" WHERE "book"."pages" * 2 = 200` |
@@ -108,7 +135,8 @@ to query the database.
108
135
  |`start_with?` |`Book.rql.where {name.start_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE 'A%'` |
109
136
  |`end_with?` |`Book.rql.where {name.end_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE '%A'` |
110
137
  |`include?` |`Book.rql.where {name.include?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE '%A%'` |
111
- |`in? ` |`Book.rql.where {name.in?(['A', 'B'])}` |`SELECT * FROM "book" WHERE "book"."name" IN ('A', 'B')`|
138
+ |`in?` |`Book.rql.where {name.in?(['A', 'B'])}` |`SELECT * FROM "book" WHERE "book"."name" IN ('A', 'B')`|
139
+ |`downcase` |`Book.rql.where {name.downcase == 'a'}` |`SELECT * FROM "book" WHERE LOWER("book"."name") = 'a'` |
112
140
 
113
141
  |Aggregates |Example |
114
142
  |---------------------------------|----------------------------------------------------------|
@@ -3,6 +3,7 @@ require "rql/dsl/comparisons"
3
3
  require "rql/dsl/logic"
4
4
  require "rql/dsl/maths"
5
5
  require "rql/dsl/orders"
6
+ require "rql/dsl/functions"
6
7
  require "rql/dsl/base"
7
8
  require "rql/dsl/context"
8
9
 
@@ -31,16 +31,20 @@ module Rql
31
31
  Context.new(@model, arel.gteq(value))
32
32
  end
33
33
 
34
+ def =~(value)
35
+ Context.new(@model, arel.matches_regexp(value.source, !value.casefold?))
36
+ end
37
+
34
38
  def start_with?(value)
35
- Context.new(@model, arel.matches("#{value}%"))
39
+ Context.new(@model, arel.matches("#{value}%", nil, true))
36
40
  end
37
41
 
38
42
  def end_with?(value)
39
- Context.new(@model, arel.matches("%#{value}"))
43
+ Context.new(@model, arel.matches("%#{value}", nil, true))
40
44
  end
41
45
 
42
46
  def include?(value)
43
- Context.new(@model, arel.matches("%#{value}%"))
47
+ Context.new(@model, arel.matches("%#{value}%", nil, true))
44
48
  end
45
49
 
46
50
  def in?(collection)
@@ -6,6 +6,7 @@ module Rql
6
6
  include Maths
7
7
  include Orders
8
8
  include Logic
9
+ include Functions
9
10
 
10
11
  attr_accessor :arel, :scope, :model
11
12
 
@@ -0,0 +1,9 @@
1
+ module Rql
2
+ module Dsl
3
+ module Functions
4
+ def downcase
5
+ Context.new(@model, arel.lower, @name)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -18,7 +18,7 @@ module Rql
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
- Dsl::Base.new(self, alias_derived_attr).instance_eval(&block)
21
+ Dsl::Base.new(self.unscoped, alias_derived_attr).instance_eval(&block)
22
22
  end
23
23
 
24
24
  # Defines a derived attribute on a model
@@ -17,6 +17,14 @@ module Rql
17
17
  scope.select(*build_attributes(true, &block))
18
18
  end
19
19
 
20
+ # Plucks the specified attributes, creating and array of values
21
+ #
22
+ # @yield RQL block defining the attributes to be selected
23
+ # @return [Array] an array of values if plucking one value or an array of arrays if plucking multiple values
24
+ def pluck(&block)
25
+ scope.pluck(*build_attributes(true, &block))
26
+ end
27
+
20
28
  # Orders by specified attributes
21
29
  #
22
30
  # @yield RQL block defining the attributes order by
@@ -17,6 +17,14 @@ module Rql
17
17
  scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
18
18
  end
19
19
 
20
+ # Plucks the specified attributes, creating and array of values
21
+ #
22
+ # @param attributes [Array] the attributes to select
23
+ # @return [Array] an array of values if plucking one value or an array of arrays if plucking multiple values
24
+ def pluck(*attributes)
25
+ scope.pluck(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
26
+ end
27
+
20
28
  # Orders by specified attributes
21
29
  #
22
30
  # @param attributes [Array] the attributes to order by. Attributes may be a symbol, sql string or a hash specifying attribute and order.
@@ -1,3 +1,3 @@
1
1
  module Rql
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  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.2.1
4
+ version: 0.2.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-07-04 00:00:00.000000000 Z
11
+ date: 2018-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -137,6 +137,7 @@ files:
137
137
  - lib/rql/dsl/base.rb
138
138
  - lib/rql/dsl/comparisons.rb
139
139
  - lib/rql/dsl/context.rb
140
+ - lib/rql/dsl/functions.rb
140
141
  - lib/rql/dsl/logic.rb
141
142
  - lib/rql/dsl/maths.rb
142
143
  - lib/rql/dsl/orders.rb