rquery-activerecord 0.1.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.
- data/lib/rquery_ar.rb +118 -0
- metadata +75 -0
data/lib/rquery_ar.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RQuery
|
4
|
+
module ClassMethods
|
5
|
+
def rquery(cmd=nil)
|
6
|
+
if cmd
|
7
|
+
ar_statement = ''
|
8
|
+
# TODO includes_clause(JSON.parse(cmd[:includes])) if cmd[:includes]
|
9
|
+
# TODO joins_clause(JSON.parse(cmd[:joins])) if cmd[:joins]
|
10
|
+
ar_statement += where_clause(JSON.parse(cmd[:where])) if cmd[:where]
|
11
|
+
ar_statement += order_clause(JSON.parse(cmd[:order])) if cmd[:order]
|
12
|
+
ar_statement += limit_clause(cmd[:limit]) if cmd[:limit]
|
13
|
+
ar_statement += skip_clause(cmd[:skip]) if cmd[:skip]
|
14
|
+
ar_statement = (ar_statement == '' ? {:results => eval('all').to_a} : {:results => eval(ar_statement[1..-1]).to_a})
|
15
|
+
ar_statement = count_clause(cmd[:count], ar_statement) if cmd[:count]
|
16
|
+
ar_statement
|
17
|
+
else
|
18
|
+
{}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
## WHERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
24
|
+
def where_clause(cmd)
|
25
|
+
ar_statement, clause = ""
|
26
|
+
cmd.each do |key, value|
|
27
|
+
clause = value.kind_of?(Hash) ? where_key_value(key, value) : "#{key} = \"" + value + "\""
|
28
|
+
ar_statement += ".where('#{clause}')"
|
29
|
+
end
|
30
|
+
ar_statement
|
31
|
+
end
|
32
|
+
|
33
|
+
def where_key_value(key, value)
|
34
|
+
action, val = ''
|
35
|
+
value.each do |k, v|
|
36
|
+
action = replace(k) if k.match(/^\$/)
|
37
|
+
val = v
|
38
|
+
end
|
39
|
+
where_build_clause(key, action, val)
|
40
|
+
end
|
41
|
+
|
42
|
+
def where_build_clause(key, action, val)
|
43
|
+
if action == 'IS'
|
44
|
+
"#{key} #{action} #{val == '1' ? 'NOT NULL' : 'NULL'}"
|
45
|
+
elsif action == "IN" || action == "NOT IN"
|
46
|
+
"#{key} #{action} #{val.gsub("\'", "\"")}"
|
47
|
+
else
|
48
|
+
"#{key} #{action} \"" + val + "\""
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
## ORDER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
53
|
+
def order_clause(cmd)
|
54
|
+
ar_statement = ""
|
55
|
+
cmd.each do |value|
|
56
|
+
ar_statement += ".order('#{value}')"
|
57
|
+
end
|
58
|
+
ar_statement
|
59
|
+
end
|
60
|
+
|
61
|
+
## LIMIT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
62
|
+
def limit_clause(cmd)
|
63
|
+
ar_statement = ""
|
64
|
+
cmd.each { |value| ar_statement += ".limit(#{value})" }
|
65
|
+
ar_statement
|
66
|
+
end
|
67
|
+
|
68
|
+
## COUNT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
69
|
+
def count_clause(cmd, ar_statement)
|
70
|
+
if cmd == "1"
|
71
|
+
ar_statement.merge(:count => ar_statement[:results].count)
|
72
|
+
else
|
73
|
+
ar_statement
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
## SKIP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
78
|
+
def skip_clause(cmd)
|
79
|
+
ar_statement = ""
|
80
|
+
cmd.each { |value| ar_statement += ".offset(#{value})" }
|
81
|
+
ar_statement
|
82
|
+
end
|
83
|
+
|
84
|
+
#TODO
|
85
|
+
## INCLUDES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
86
|
+
def includes_clause(cmd)
|
87
|
+
includes cmd
|
88
|
+
end
|
89
|
+
|
90
|
+
#TODO
|
91
|
+
## JOINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
92
|
+
def joins_clause(cmd)
|
93
|
+
joins cmd
|
94
|
+
end
|
95
|
+
|
96
|
+
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
97
|
+
def replace(match_data)
|
98
|
+
case match_data
|
99
|
+
when '$gt' then '>'
|
100
|
+
when '$lt' then '<'
|
101
|
+
when '$gte' then '>='
|
102
|
+
when '$lte' then '<='
|
103
|
+
when '$ne' then '!='
|
104
|
+
when '$in' then 'IN'
|
105
|
+
when '$nin' then 'NOT IN'
|
106
|
+
when '$exists' then 'IS'
|
107
|
+
when '$like' then 'LIKE'
|
108
|
+
when '$ilike' then 'ILIKE'
|
109
|
+
when '$regexp' then 'REGEXP'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.included(base)
|
116
|
+
base.extend(ClassMethods)
|
117
|
+
end
|
118
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rquery-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tom Wilson, Kris Windham, Andrew Kennedy
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-03-12 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 6
|
30
|
+
- 3
|
31
|
+
version: 1.6.3
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Rquery is a ruby gem that will allow you to pass queries into your restful api as json. It will take the json formatted queries and convert them into ActiveRecord queries.
|
35
|
+
email: team@jackrussellsoftware.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- ./lib/rquery_ar.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: https://github.com/jackruss/rquery
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.6
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Queries for your restful api!
|
74
|
+
test_files: []
|
75
|
+
|