rubyql 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +71 -0
- data/lib/rubyql.rb +0 -2
- data/test/user_query_spectr.rb +27 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ebe793cb41788c0005aea403a8163ee8e1a3d5e
|
4
|
+
data.tar.gz: 6c055b0202a59cb35f06db26d906dc3ac37f631d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a97cc880a462bbb7474d01d6279b51c198af8e1aa9bb2f2c904ccfa0034f961c6bf61c637bc9ae93d7c9175dafa5dc6581ec2e880340e5f54c29d0d12ca85f84
|
7
|
+
data.tar.gz: 9a3c8cf06369b801006ca7601546b7575bcc67281e7a6c068893964296353cc8d677a8cf4f5cb9dda2c867458e9ead26c94e435f568286eadd4b0c3bf54b9905
|
data/README.md
CHANGED
@@ -0,0 +1,71 @@
|
|
1
|
+
# RubyQL
|
2
|
+
## A query language for ruby hashes
|
3
|
+
|
4
|
+
RubyQL is a query language designed for very flexible rest apis or plain ruby hashes.
|
5
|
+
|
6
|
+
RubyQL loves it when its used together with ActiveRecord 🤤
|
7
|
+
|
8
|
+
Example:
|
9
|
+
``` ruby
|
10
|
+
require 'rubyql'
|
11
|
+
|
12
|
+
class UserQuery < RubyQL
|
13
|
+
def query
|
14
|
+
User.find_by(query_params).attributes
|
15
|
+
end
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
The class we created overwrites the query method, to provide a custom mechanism which should return a hash. `query_params` is
|
20
|
+
the hash which holds the provided parameter, like `WHERE` in SQL.
|
21
|
+
|
22
|
+
**IMPORTANT**: `.attributes` is necessary for ActiveRecord, only returning the actual hash, not the ActiveModel object.
|
23
|
+
|
24
|
+
Usage:
|
25
|
+
```
|
26
|
+
UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
27
|
+
=> {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
|
28
|
+
|
29
|
+
UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
30
|
+
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
|
31
|
+
|
32
|
+
UserQuery.new({"lastname"=>"", "email"=>"", "id"=>1}).execute
|
33
|
+
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
34
|
+
```
|
35
|
+
|
36
|
+
As you can see we left the wanted attributes blank, so rubyql will fill them out, if they exist.
|
37
|
+
|
38
|
+
For a better understanding another plain ruby example:
|
39
|
+
|
40
|
+
``` ruby
|
41
|
+
require 'rubyql
|
42
|
+
|
43
|
+
class PlainQuery < RubyQL
|
44
|
+
def query
|
45
|
+
{"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
The method is only returning a simple hash. Now we can execute queries:
|
51
|
+
|
52
|
+
```
|
53
|
+
PlainQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com", "id"=>""}).execute
|
54
|
+
=> {"firstname"=>"Hanft", "lastname"=>"Niklas", "email"=>"niklas.hanft@outlook.com", "id"=>1337}
|
55
|
+
|
56
|
+
PlainQuery.new({"email"=>"niklas.hanft@outlook.com", "id"=>"", "another_attribute"=>""}).execute
|
57
|
+
=> {"email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
|
58
|
+
```
|
59
|
+
|
60
|
+
As this is static, it doesn't really make sense, but for understanding it might be ok to use it this way. But mainly it
|
61
|
+
should be used with a database orm or some dynamic functions which return hashes.
|
62
|
+
|
63
|
+
Advanced usage:
|
64
|
+
|
65
|
+
*Coming soon*
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
data/lib/rubyql.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spectr'
|
2
|
+
require_relative '../lib/rubyql'
|
3
|
+
|
4
|
+
Spectr.new.test 'Test the queryable class' do |test|
|
5
|
+
|
6
|
+
class UserQuery < RubyQL
|
7
|
+
def query
|
8
|
+
{"username"=>"paradoxxger", "firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
test.assume('The response is a hash', true) do
|
13
|
+
UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute.is_a?(Hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
test.assume('The response equals the given hash', {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}) do
|
17
|
+
UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
18
|
+
end
|
19
|
+
|
20
|
+
test.assume('The response equals the given hash, removing some attributes', {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}) do
|
21
|
+
UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
22
|
+
end
|
23
|
+
|
24
|
+
test.assume('The query_params are only the email', {"email" => "niklas.hanft@outlook.com"}) do
|
25
|
+
UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).query_params
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Niklas Hanft
|
@@ -18,6 +18,7 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- README.md
|
20
20
|
- lib/rubyql.rb
|
21
|
+
- test/user_query_spectr.rb
|
21
22
|
homepage: https://github.com/ParadoXxGER/rubyql
|
22
23
|
licenses:
|
23
24
|
- MIT
|