rubyql 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +28 -1
- data/lib/rubyql.rb +8 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e001320b038e8c6316fe99ed61f706fd89027f9
|
4
|
+
data.tar.gz: 0024b94ff7ed2c24f1519de8458c703d0858087b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3274005fde19a0dbcd06b3c99d4c5d8665a195772864a1ae2015178ada2c447676c6dc110f0488c9483c5bf9e737cecccde4823e7289ef587b032d7531c6dd05
|
7
|
+
data.tar.gz: 57ba32c92e096e0aba05fd27f60b1f1a0ae668ed4610575822f994d6f083902a4a35c708eacca2aa18eac03a03b9b3f4edb3ec5faf4e86f9f2fed0cad9dc5166
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# RubyQL
|
2
2
|
## A query language for ruby hashes
|
3
3
|
|
4
|
+
[](https://badge.fury.io/rb/rubyql)
|
5
|
+
|
4
6
|
RubyQL is a query language designed for very flexible rest apis or plain ruby hashes.
|
5
7
|
|
6
8
|
RubyQL loves it when its used together with ActiveRecord 🤤
|
@@ -60,8 +62,33 @@ The method is only returning a simple hash. Now we can execute queries:
|
|
60
62
|
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
63
|
should be used with a database orm or some dynamic functions which return hashes.
|
62
64
|
|
63
|
-
Advanced usage:
|
65
|
+
Advanced usage: In a Rails Project:
|
66
|
+
|
67
|
+
```
|
68
|
+
require 'rubyql'
|
64
69
|
|
70
|
+
class UserQuery < RubyQL
|
71
|
+
def query
|
72
|
+
User.find_by(query_params).attributes
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
```
|
78
|
+
irb(main):007:0> UserQuery.new({ "firstname"=>"", "id"=>1}).execute
|
79
|
+
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
80
|
+
=> {"firstname"=>"Niklas", "id"=>1}
|
81
|
+
irb(main):008:0> UserQuery.new({ "lastname"=>"", "id"=>1}).execute
|
82
|
+
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
83
|
+
=> {"lastname"=>"Hanft", "id"=>1}
|
84
|
+
irb(main):009:0> UserQuery.new({ "lastname"=>"", "id"=>1, "email"=>""}).execute
|
85
|
+
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
86
|
+
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
87
|
+
irb(main):010:0> UserQuery.new({ "lastname"=>"", "id"=>1, "email"=>"", "firstname"=>""}).execute
|
88
|
+
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
89
|
+
=> {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
90
|
+
|
91
|
+
```
|
65
92
|
*Coming soon*
|
66
93
|
|
67
94
|
|
data/lib/rubyql.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
class RubyQL
|
2
4
|
attr_accessor :params
|
3
5
|
|
4
|
-
def initialize(params
|
5
|
-
if
|
6
|
+
def initialize(params)
|
7
|
+
if params.is_a?(JSON)
|
6
8
|
@params = JSON.parse(params)
|
7
|
-
|
9
|
+
elsif params.is_a?(Hash)
|
8
10
|
@params = params
|
11
|
+
else
|
12
|
+
raise
|
9
13
|
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def execute
|
17
|
+
return {} if query.nil?
|
13
18
|
query.select do |key, value|
|
14
19
|
response_attr.key? key
|
15
20
|
end.merge query_params
|