mystic 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mystic.rb +7 -1
- data/lib/mystic/model.rb +25 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d72462dbb17bb77c1cf67fd294286f52636bd68f
|
4
|
+
data.tar.gz: e78d213ceb6133c03b24c00c959403bb51f3cc99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ef965891e3f3dc4c1617511707bf6af8cf3e747512cd96f44d20964142c7e81f13793dba7350a1c60eec681ae58f533e894dac57c85c8beb84e77eaf785ede9
|
7
|
+
data.tar.gz: 731b449bdc535b57cd0c22fd6bbc216c9f25b371b01e7a2e51e7386cf99701092c1358f93f4d0ce340915963b04e942644b712f14cf54c3e5eb81d239b57f05d
|
data/lib/mystic.rb
CHANGED
@@ -54,11 +54,17 @@ module Mystic
|
|
54
54
|
conf[:user] = conf.delete :username
|
55
55
|
raise MysticError, "Mystic only supports Postgres." unless /^postg.+$/i =~ conf[:adapter]
|
56
56
|
|
57
|
-
@postgres = Postgres.new
|
57
|
+
@postgres = Postgres.new conf
|
58
58
|
|
59
59
|
@env
|
60
60
|
end
|
61
61
|
|
62
|
+
def manual_conn conf={}
|
63
|
+
load_env
|
64
|
+
@env = (ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development").to_s
|
65
|
+
@postgres = Postgres.new(conf)
|
66
|
+
end
|
67
|
+
|
62
68
|
def root path=Pathname.new(Dir.pwd)
|
63
69
|
raise RootError, "Failed to find the application's root." if path == path.parent
|
64
70
|
mystic_path = path.join "config", "database.yml"
|
data/lib/mystic/model.rb
CHANGED
@@ -8,7 +8,7 @@ module Mystic
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def table_name
|
11
|
-
to_s.downcase
|
11
|
+
to_s.split("::").last.downcase
|
12
12
|
end
|
13
13
|
|
14
14
|
def visible_cols
|
@@ -26,19 +26,16 @@ module Mystic
|
|
26
26
|
plural = opts[:plural] && op != "INSERT"
|
27
27
|
|
28
28
|
sql << " RETURNING #{visible_cols*','}" if return_rows && op != "SELECT"
|
29
|
-
|
29
|
+
|
30
|
+
return sql unless return_json
|
31
|
+
|
30
32
|
s = []
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
s << "FROM res"
|
38
|
-
else
|
39
|
-
s << sql
|
40
|
-
end
|
41
|
-
|
33
|
+
s << "WITH res AS (#{sql}) SELECT"
|
34
|
+
s << "array_to_json(array_agg(res))" if plural
|
35
|
+
s << "row_to_json(res)" unless plural
|
36
|
+
s << "AS #{Mystic::JSON_COL}"
|
37
|
+
s << "FROM res"
|
38
|
+
s << "LIMIT 1" unless plural
|
42
39
|
s*' '
|
43
40
|
end
|
44
41
|
|
@@ -60,7 +57,7 @@ module Mystic
|
|
60
57
|
:sql => sql.join(' '),
|
61
58
|
:return_rows => true,
|
62
59
|
:return_json => sym_opts[:return_json],
|
63
|
-
:plural =>
|
60
|
+
:plural => sym_opts[:fetch] == false
|
64
61
|
)
|
65
62
|
end
|
66
63
|
|
@@ -73,7 +70,8 @@ module Mystic
|
|
73
70
|
wrapper_sql(
|
74
71
|
:sql => "UPDATE #{table_name} SET #{set.sqlize*','} WHERE #{where.sqlize*' AND '}",
|
75
72
|
:return_rows => sym_opts[:return_rows],
|
76
|
-
:return_json => sym_opts[:return_json]
|
73
|
+
:return_json => sym_opts[:return_json],
|
74
|
+
:plural => sym_opts.member?(:plural) ? sym_opts[:plural] : true
|
77
75
|
)
|
78
76
|
end
|
79
77
|
|
@@ -85,7 +83,8 @@ module Mystic
|
|
85
83
|
wrapper_sql(
|
86
84
|
:sql => "INSERT INTO #{table_name} (#{params.keys*','}) VALUES (#{params.values.sqlize*','})",
|
87
85
|
:return_rows => sym_opts[:return_rows],
|
88
|
-
:return_json => sym_opts[:return_json]
|
86
|
+
:return_json => sym_opts[:return_json],
|
87
|
+
:plural => sym_opts.member?(:plural) ? sym_opts[:plural] : true
|
89
88
|
)
|
90
89
|
end
|
91
90
|
|
@@ -97,7 +96,8 @@ module Mystic
|
|
97
96
|
wrapper_sql(
|
98
97
|
:sql => "DELETE FROM #{table_name} WHERE #{params.sqlize*' AND '}",
|
99
98
|
:return_rows => sym_opts[:return_rows],
|
100
|
-
:return_json => sym_opts[:return_json]
|
99
|
+
:return_json => sym_opts[:return_json],
|
100
|
+
:plural => sym_opts.member?(:plural) ? sym_opts[:plural] : true
|
101
101
|
)
|
102
102
|
end
|
103
103
|
|
@@ -106,23 +106,27 @@ module Mystic
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def fetch params={}, opts={}
|
109
|
-
res = select params, opts.merge({:count => 1})
|
109
|
+
res = select params, opts.merge({:count => 1, :fetch => true})
|
110
110
|
return res if res.is_a? String
|
111
111
|
res.first rescue nil
|
112
112
|
end
|
113
113
|
|
114
114
|
def create params={}, opts={}
|
115
|
-
res = Mystic.execute insert_sql(params, opts)
|
115
|
+
res = Mystic.execute insert_sql(params, opts.merge({ :return_rows => true }))
|
116
116
|
return res if res.is_a? String
|
117
117
|
res.first rescue nil
|
118
118
|
end
|
119
119
|
|
120
120
|
def update where={}, set={}, opts={}
|
121
|
-
Mystic.execute update_sql(where, set, opts.merge({ :return_rows => true }))
|
121
|
+
res = Mystic.execute update_sql(where, set, opts.merge({ :return_rows => true }))
|
122
|
+
return res.first unless opts[:plural]
|
123
|
+
res
|
122
124
|
end
|
123
125
|
|
124
126
|
def delete params={}, opts={}
|
125
|
-
Mystic.execute delete_sql(params, opts)
|
127
|
+
res = Mystic.execute delete_sql(params, opts)
|
128
|
+
return res.first if !opts[:plural] && !res.nil?
|
129
|
+
res
|
126
130
|
end
|
127
131
|
|
128
132
|
def exec_func funcname, *params
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mystic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathaniel Symer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: densify
|