rql 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73bfb99910e53072a8efb800ec7e1d16ce2a6c9c
4
- data.tar.gz: 70e3691955083453373248274ed088aa18298290
3
+ metadata.gz: da24e3445d8f2175928c882b2cabe6b43e54452b
4
+ data.tar.gz: 6b0326c51d3d243cbebfc2d4a9a2b44fa1f1b943
5
5
  SHA512:
6
- metadata.gz: 1f22889e8798227e85c0fcba82f12151b88ee40879053d2063ec9aa986783acbf0dec5f61c06f9c80fdae58a2215696238ee812feabcf6116eee8aeb1470ecb9
7
- data.tar.gz: 286d67606f9049498a480d88b4f3e1826abede59c14f12354d57990cc5d3b6ff4b6475e94c8e4b63741f8cfb48b67761412f2f607c9a46ff685ec34ddc603e98
6
+ metadata.gz: 4e055053881fef20bf297c193a7efab0304816676a2a457cf369984aa22f420aa2fb3f5e4c7882fc8229289262d1eaed45364c635150b5067b3fc8e4951b1aa5
7
+ data.tar.gz: d564ddb101947806963720c92bdc6c62ffadd987e3ca2bdd13d2994600f04248fb234c9903eb18cd99b397ac582e688b4b4c31a29fea369ffd49c8e7a1ccb8ed
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RQL [![Build Status](https://secure.travis-ci.org/liefni/rql.png)](http://travis-ci.org/liefni/rql)
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
3
  RQL is a DSL for ActiveRecord designed to allow derived/calculated attributes on models to be used in database queries.
4
4
 
@@ -68,10 +68,10 @@ class Author < ActiveRecord::Base
68
68
  end
69
69
  ```
70
70
 
71
- ### Preloading
71
+ ### Preloading/Eager-Loading
72
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:
73
+ Derived attributes can be preloaded to prevent multiple database calls when
74
+ deriving attributes that reference associated models. To preload derived attributes:
75
75
 
76
76
  ```ruby
77
77
  Authors.derive(:total_books, :total_pages)
@@ -81,6 +81,54 @@ 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
+ ### Query DSL
85
+
86
+ #### Operators
87
+
88
+ |Operator |Example |SQL (In Postgres) |
89
+ |----------|--------------------------------------|--------------------------------------------------------|
90
+ |== |`Book.rql.where {pages == 200}` |`SELECT * FROM "book" WHERE "book"."pages" = 200` |
91
+ |!= |`Book.rql.where {pages != 200}` |`SELECT * FROM "book" WHERE "book"."pages" != 200` |
92
+ |=== |`Book.rql.where {pages === (200..300)}`|`SELECT * FROM "book" WHERE "book"."pages" >= 200 AND "book"."pages" >= 200` |
93
+ |> |`Book.rql.where {pages > 200}` |`SELECT * FROM "book" WHERE "book"."pages" > 200` |
94
+ |< |`Book.rql.where {pages < 200}` |`SELECT * FROM "book" WHERE "book"."pages" < 200` |
95
+ |>= |`Book.rql.where {pages >= 200}` |`SELECT * FROM "book" WHERE "book"."pages" >= 200` |
96
+ |<= |`Book.rql.where {pages <= 200}` |`SELECT * FROM "book" WHERE "book"."pages" <= 200` |
97
+ |+ |`Book.rql.where {pages + 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" + 2) = 200` |
98
+ |- |`Book.rql.where {pages - 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" - 2) = 200` |
99
+ |* |`Book.rql.where {pages * 2 == 200}` |`SELECT * FROM "book" WHERE "book"."pages" * 2 = 200` |
100
+ |/ |`Book.rql.where {pages / 2 == 200}` |`SELECT * FROM "book" WHERE "book"."pages" / 2 = 200` |
101
+ |\| |`Book.rql.where {(pages == 200) \| (pages == 300)}` |`SELECT * FROM "book" WHERE ("book"."pages" = 200) OR ( "book"."pages" = 300)`|
102
+ |& |`Book.rql.where {(pages == 200) & (pages == 300)}` |`SELECT * FROM "book" WHERE "book"."pages" = 200 AND "book"."pages" = 300` |
103
+
104
+ #### Functions
105
+
106
+ |Function |Example |SQL (In Postgres) |
107
+ |--------------|------------------------------------------|--------------------------------------------------------|
108
+ |`start_with?` |`Book.rql.where {name.start_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE 'A%'` |
109
+ |`end_with?` |`Book.rql.where {name.end_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE '%A'` |
110
+ |`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')`|
112
+
113
+ |Aggregates |Example |
114
+ |---------------------------------|----------------------------------------------------------|
115
+ |`calculate(attribute, operation)`|`Author.rql.where {books.calculate(:pages, :sum) > 500}` |
116
+ |`sum(attribute)` |`Author.rql.where {books.sum(:pages) > 500}` |
117
+ |`average(attribute)` |`Author.rql.where {books.average(:pages) > 250}` |
118
+ |`minimum(attribute)` |`Author.rql.where {books.minimum(:pages) > 300}` |
119
+ |`maximum(attribute)` |`Author.rql.where {books.maximum(:pages) > 200}` |
120
+ |`count(attribute)` |`Author.rql.where {books.count(:name) > 5}` |
121
+ |`count` |`Author.rql.where {books.count > 5}` |
122
+
123
+ #### Order
124
+
125
+ |Order |Example |SQL (In Postgres) |
126
+ |---------------|------------------------------------|--------------------------------------------------------|
127
+ |asc |`Book.rql.order {name.asc}` |`SELECT * FROM "book" ORDER BY "book"."name" ASC` |
128
+ |desc |`Book.rql.order {name.desc}` |`SELECT * FROM "book" ORDER BY "book"."name" DESC` |
129
+ |asc(flag) |`Book.rql.order {name.asc(false)}` |`SELECT * FROM "book" ORDER BY "book"."name" DESC` |
130
+ |desc(flag) |`Book.rql.order {name.desc(false)}` |`SELECT * FROM "book" ORDER BY "book"."name" ASC` |
131
+
84
132
  ## Development
85
133
 
86
134
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
@@ -5,6 +5,10 @@ module Rql
5
5
  Context.new(@model, arel.eq(value))
6
6
  end
7
7
 
8
+ def !=(value)
9
+ Context.new(@model, arel.not_eq(value))
10
+ end
11
+
8
12
  def ===(range)
9
13
  min_query = (range.min != -Float::INFINITY) ? arel.gt(range.min) : Arel.sql('1').eq(1)
10
14
  max_query = (range.max != Float::INFINITY) ? arel.lt(range.max) : Arel.sql('1').eq(1)
data/lib/rql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rql
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lief Nikkel