inquery 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.releaser_config +4 -0
- data/.rubocop.yml +42 -0
- data/.travis.yml +9 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +288 -0
- data/RUBY_VERSION +1 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/doc/Inquery.html +119 -0
- data/doc/Inquery/Exceptions.html +115 -0
- data/doc/Inquery/Exceptions/Base.html +127 -0
- data/doc/Inquery/Exceptions/InvalidRelation.html +131 -0
- data/doc/Inquery/Exceptions/UnknownCallSignature.html +131 -0
- data/doc/Inquery/Mixins.html +117 -0
- data/doc/Inquery/Mixins/RelationValidation.html +334 -0
- data/doc/Inquery/Mixins/RelationValidation/ClassMethods.html +190 -0
- data/doc/Inquery/Mixins/SchemaValidation.html +124 -0
- data/doc/Inquery/Mixins/SchemaValidation/ClassMethods.html +192 -0
- data/doc/Inquery/Query.html +736 -0
- data/doc/Inquery/Query/Chainable.html +476 -0
- data/doc/_index.html +254 -0
- data/doc/class_list.html +58 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +339 -0
- data/doc/file.README.html +365 -0
- data/doc/file_list.html +60 -0
- data/doc/frames.html +26 -0
- data/doc/index.html +365 -0
- data/doc/js/app.js +219 -0
- data/doc/js/full_list.js +181 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +147 -0
- data/doc/top-level-namespace.html +112 -0
- data/inquery.gemspec +58 -0
- data/lib/inquery.rb +10 -0
- data/lib/inquery/exceptions.rb +7 -0
- data/lib/inquery/mixins/relation_validation.rb +100 -0
- data/lib/inquery/mixins/schema_validation.rb +27 -0
- data/lib/inquery/query.rb +50 -0
- data/lib/inquery/query/chainable.rb +53 -0
- data/test/db/models.rb +20 -0
- data/test/db/schema.rb +20 -0
- data/test/inquery/query/chainable_test.rb +67 -0
- data/test/inquery/query_test.rb +47 -0
- data/test/queries/group/fetch_as_json.rb +13 -0
- data/test/queries/group/fetch_green.rb +11 -0
- data/test/queries/group/fetch_red.rb +11 -0
- data/test/queries/group/filter_with_color.rb +12 -0
- data/test/queries/user/fetch_all.rb +9 -0
- data/test/queries/user/fetch_in_group.rb +13 -0
- data/test/queries/user/fetch_in_group_rel.rb +17 -0
- data/test/test_helper.rb +26 -0
- metadata +265 -0
@@ -0,0 +1,365 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
6
|
+
<title>
|
7
|
+
File: README
|
8
|
+
|
9
|
+
— Documentation by YARD 0.8.7.6
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
hasFrames = window.top.frames.main ? true : false;
|
19
|
+
relpath = '';
|
20
|
+
framesUrl = "frames.html#!file.README.html";
|
21
|
+
</script>
|
22
|
+
|
23
|
+
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
25
|
+
|
26
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
27
|
+
|
28
|
+
|
29
|
+
</head>
|
30
|
+
<body>
|
31
|
+
<div id="header">
|
32
|
+
<div id="menu">
|
33
|
+
|
34
|
+
<a href="_index.html">Index</a> »
|
35
|
+
<span class="title">File: README</span>
|
36
|
+
|
37
|
+
|
38
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
39
|
+
</div>
|
40
|
+
|
41
|
+
<div id="search">
|
42
|
+
|
43
|
+
<a class="full_list_link" id="class_list_link"
|
44
|
+
href="class_list.html">
|
45
|
+
Class List
|
46
|
+
</a>
|
47
|
+
|
48
|
+
<a class="full_list_link" id="method_list_link"
|
49
|
+
href="method_list.html">
|
50
|
+
Method List
|
51
|
+
</a>
|
52
|
+
|
53
|
+
<a class="full_list_link" id="file_list_link"
|
54
|
+
href="file_list.html">
|
55
|
+
File List
|
56
|
+
</a>
|
57
|
+
|
58
|
+
</div>
|
59
|
+
<div class="clear"></div>
|
60
|
+
</div>
|
61
|
+
|
62
|
+
<iframe id="search_frame"></iframe>
|
63
|
+
|
64
|
+
<div id="content"><div id='filecontents'>
|
65
|
+
<h1 id="label-Inquery">Inquery</h1>
|
66
|
+
|
67
|
+
<p>A skeleton that allows extracting queries into atomic, reusable classes.</p>
|
68
|
+
|
69
|
+
<h2 id="label-Installation">Installation</h2>
|
70
|
+
|
71
|
+
<p>To install the <strong>Inquery</strong> gem:</p>
|
72
|
+
|
73
|
+
<pre class="code ruby"><code class="ruby">$ gem install inquery</code></pre>
|
74
|
+
|
75
|
+
<p>To install it using <code>bundler</code> (recommended for any application),
|
76
|
+
add it to your <code>Gemfile</code>:</p>
|
77
|
+
|
78
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>inquery</span><span class='tstring_end'>'</span></span>
|
79
|
+
</code></pre>
|
80
|
+
|
81
|
+
<h2 id="label-Basic+usage">Basic usage</h2>
|
82
|
+
|
83
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>FetchUsersWithACar</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span>
|
84
|
+
<span class='id identifier rubyid_schema'>schema</span><span class='lparen'>(</span>
|
85
|
+
<span class='label'>color:</span> <span class='symbol'>:symbol</span>
|
86
|
+
<span class='rparen'>)</span>
|
87
|
+
|
88
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
89
|
+
<span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_joins'>joins</span><span class='lparen'>(</span><span class='symbol'>:cars</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>cars:</span> <span class='lbrace'>{</span> <span class='label'>color:</span> <span class='id identifier rubyid_osparams'>osparams</span><span class='period'>.</span><span class='id identifier rubyid_color'>color</span> <span class='rbrace'>}</span><span class='rparen'>)</span>
|
90
|
+
<span class='kw'>end</span>
|
91
|
+
<span class='kw'>end</span>
|
92
|
+
|
93
|
+
<span class='const'>FetchUsersWithACar</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span>
|
94
|
+
<span class='comment'># => [<User id: 1 ...]
|
95
|
+
</span></code></pre>
|
96
|
+
|
97
|
+
<p>Inquery offers its functionality trough two query base classes:
|
98
|
+
<span class='object_link'><a href="Inquery/Query.html" title="Inquery::Query (class)">Inquery::Query</a></span> and <span class='object_link'><a href="Inquery/Query/Chainable.html" title="Inquery::Query::Chainable (class)">Inquery::Query::Chainable</a></span>. See the following
|
99
|
+
sections for detailed explanations.</p>
|
100
|
+
|
101
|
+
<h2 id="label-Basic+queries">Basic queries</h2>
|
102
|
+
|
103
|
+
<p>Basic queries inherit from <span class='object_link'><a href="Inquery/Query.html" title="Inquery::Query (class)">Inquery::Query</a></span>. They receive an optional set
|
104
|
+
of parameters and commonly return a relation / AR result. An optional
|
105
|
+
<code>process</code> method lets you perform additional result processing
|
106
|
+
steps if needed (i.e. converting the result to a hash or similar).</p>
|
107
|
+
|
108
|
+
<p>For this basic functionality, inherit from <span class='object_link'><a href="Inquery/Query.html" title="Inquery::Query (class)">Inquery::Query</a></span> and overwrite
|
109
|
+
the <code>call</code> and optionally the <code>process</code> method:</p>
|
110
|
+
|
111
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>FetchRedCarsAsJson</span>
|
112
|
+
<span class='comment'># The `call` method must be overwritten for every query. It is usually called
|
113
|
+
</span> <span class='comment'># via `run`.
|
114
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
115
|
+
<span class='const'>Car</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>color:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>red</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
116
|
+
<span class='kw'>end</span>
|
117
|
+
|
118
|
+
<span class='comment'># The `process` method can optionally be overwritten. The base implementation
|
119
|
+
</span> <span class='comment'># just returns the unprocessed `results` argument.
|
120
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_process'>process</span><span class='lparen'>(</span><span class='id identifier rubyid_results'>results</span><span class='rparen'>)</span>
|
121
|
+
<span class='id identifier rubyid_results'>results</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
|
122
|
+
<span class='kw'>end</span>
|
123
|
+
<span class='kw'>end</span>
|
124
|
+
</code></pre>
|
125
|
+
|
126
|
+
<p>Queries can be called in various ways:</p>
|
127
|
+
|
128
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># Instantiates the query class and runs `call` and `process`.
|
129
|
+
</span><span class='const'>FetchRedCarsAsJson</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
130
|
+
|
131
|
+
<span class='comment'># Instantiates the query class and runs `call`. No result processing
|
132
|
+
</span><span class='comment'># is done.
|
133
|
+
</span><span class='const'>FetchRedCarsAsJson</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
134
|
+
|
135
|
+
<span class='comment'># You can also instantiate the query class manually.
|
136
|
+
</span><span class='const'>FetchRedCarsAsJson</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span>
|
137
|
+
|
138
|
+
<span class='comment'># Or just run the `call` method without `process`.
|
139
|
+
</span><span class='const'>FetchRedCarsAsJson</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span>
|
140
|
+
</code></pre>
|
141
|
+
|
142
|
+
<p>Note that it's perfectly fine for some queries to return
|
143
|
+
<code>nil</code>, i.e. if they're writing queries that don't fetch
|
144
|
+
any results.</p>
|
145
|
+
|
146
|
+
<h2 id="label-Chainable+queries">Chainable queries</h2>
|
147
|
+
|
148
|
+
<p>Chainable queries are queries that input and output an Active Record
|
149
|
+
relation. You can access the given relation using the method
|
150
|
+
<code>relation</code>:</p>
|
151
|
+
|
152
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Queries</span><span class='op'>::</span><span class='const'>User</span><span class='op'>::</span><span class='const'>FetchActive</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span><span class='op'>::</span><span class='const'>Chainable</span>
|
153
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
154
|
+
<span class='id identifier rubyid_relation'>relation</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>active:</span> <span class='int'>1</span><span class='rparen'>)</span>
|
155
|
+
<span class='kw'>end</span>
|
156
|
+
<span class='kw'>end</span>
|
157
|
+
</code></pre>
|
158
|
+
|
159
|
+
<p>Input and output relations may or may not be of the same AR class (i.e. you
|
160
|
+
could pass a relation of <code>Group</code>s and receive back a relation of
|
161
|
+
corresponding <code>User</code>s).</p>
|
162
|
+
|
163
|
+
<h3 id="label-Relation+validation">Relation validation</h3>
|
164
|
+
|
165
|
+
<p>Chainable queries allow you to further specify and validate the relation it
|
166
|
+
receives. This is done using the static <code>relation</code> method:</p>
|
167
|
+
|
168
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Queries</span><span class='op'>::</span><span class='const'>User</span><span class='op'>::</span><span class='const'>FetchActive</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span><span class='op'>::</span><span class='const'>Chainable</span>
|
169
|
+
<span class='comment'># This will raise an exception when passing a relation which does not
|
170
|
+
</span> <span class='comment'># correspond to the `User` model.
|
171
|
+
</span> <span class='id identifier rubyid_relation'>relation</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>User</span><span class='tstring_end'>'</span></span>
|
172
|
+
|
173
|
+
<span class='comment'># ....
|
174
|
+
</span><span class='kw'>end</span>
|
175
|
+
</code></pre>
|
176
|
+
|
177
|
+
<p>The <code>relation</code> method accepts the following options:</p>
|
178
|
+
<ul><li>
|
179
|
+
<p><code>class</code></p>
|
180
|
+
|
181
|
+
<p>Allows to restrict the class (attribute <code>klass</code>) of the
|
182
|
+
relation. Use <code>nil</code> to not perform any checks. The
|
183
|
+
<code>class</code> attribute will also be taken to infer a default if no
|
184
|
+
relation is given and you didn't specify any <code>default</code>.</p>
|
185
|
+
</li><li>
|
186
|
+
<p><code>default</code></p>
|
187
|
+
|
188
|
+
<p>This allows to specify a default relation that will be taken if no relation
|
189
|
+
is given. This must be specified as a Proc returning the relation. Set this
|
190
|
+
to <code>false</code> for no default. If this is set to <code>nil</code>,
|
191
|
+
it will try to infer the default from the option <code>class</code> (if
|
192
|
+
given).</p>
|
193
|
+
</li><li>
|
194
|
+
<p><code>fields</code></p>
|
195
|
+
|
196
|
+
<p>Allows to restrict the number of fields / values the relation must select.
|
197
|
+
This is particularly useful if you're using the query as a subquery and
|
198
|
+
need it to return exactly one field. Use <code>nil</code> to not perform
|
199
|
+
any checks.</p>
|
200
|
+
</li><li>
|
201
|
+
<p><code>default_select</code></p>
|
202
|
+
|
203
|
+
<p>If this is set to a symbol, the relation does not have any select fields
|
204
|
+
specified (<code>select_values</code> is empty) and <code>fields</code> is
|
205
|
+
> 0, it will automatically select the given field. This option defaults
|
206
|
+
to <code>:id</code>. Use <code>nil</code> to disable this behavior.</p>
|
207
|
+
</li></ul>
|
208
|
+
|
209
|
+
<h3 id="label-Using+query+classes+as+regular+scopes">Using query classes as regular scopes</h3>
|
210
|
+
|
211
|
+
<p>Chainable queries can also be used as regular AR model scopes:</p>
|
212
|
+
|
213
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'><</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'>Base</span>
|
214
|
+
<span class='id identifier rubyid_scope'>scope</span> <span class='symbol'>:active</span><span class='comma'>,</span> <span class='const'>Queries</span><span class='op'>::</span><span class='const'>User</span><span class='op'>::</span><span class='const'>FetchActive</span>
|
215
|
+
<span class='kw'>end</span>
|
216
|
+
|
217
|
+
<span class='kw'>class</span> <span class='const'>Queries</span><span class='op'>::</span><span class='const'>User</span><span class='op'>::</span><span class='const'>FetchActive</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span><span class='op'>::</span><span class='const'>Chainable</span>
|
218
|
+
<span class='comment'># Note that specifying either `class` or `default` is mandatory when using
|
219
|
+
</span> <span class='comment'># this query class as a scope. The reason for this is that, if the scope is
|
220
|
+
</span> <span class='comment'># otherwise empty, the class will receive `nil` from AR and therefore has no
|
221
|
+
</span> <span class='comment'># way of knowing which default class to take.
|
222
|
+
</span> <span class='id identifier rubyid_relation'>relation</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>User</span><span class='tstring_end'>'</span></span>
|
223
|
+
|
224
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
225
|
+
<span class='id identifier rubyid_relation'>relation</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>active:</span> <span class='int'>1</span><span class='rparen'>)</span>
|
226
|
+
<span class='kw'>end</span>
|
227
|
+
<span class='kw'>end</span>
|
228
|
+
</code></pre>
|
229
|
+
|
230
|
+
<p>This approach allows to you use short and descriptive code like
|
231
|
+
<code>User.active</code> but have the possibly complex query code hidden in
|
232
|
+
a separate, reusable class.</p>
|
233
|
+
|
234
|
+
<p>Note that when using classes as scopes, the <code>process</code> method
|
235
|
+
will be ignored.</p>
|
236
|
+
|
237
|
+
<h3 id="label-Using+the+given+relation+as+subquery">Using the given relation as subquery</h3>
|
238
|
+
|
239
|
+
<p>In simple cases and all the examples above, we just extend the given
|
240
|
+
relation and return it again. It is also possible however to just use the
|
241
|
+
given relation as a subquery and return a completely new relation:</p>
|
242
|
+
|
243
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>FetchUsersInGroup</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span><span class='op'>::</span><span class='const'>Chainable</span>
|
244
|
+
<span class='comment'># Here we do not specify any specific class, as we don't care for it as long
|
245
|
+
</span> <span class='comment'># as the relation returns exactly one field.
|
246
|
+
</span> <span class='id identifier rubyid_relation'>relation</span> <span class='label'>fields:</span> <span class='int'>1</span>
|
247
|
+
|
248
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
249
|
+
<span class='kw'>return</span> <span class='op'>::</span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>%(</span><span class='tstring_content'>
|
250
|
+
id IN (
|
251
|
+
SELECT user_id FROM GROUPS_USERS WHERE group_id IN (
|
252
|
+
</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_relation'>relation</span><span class='period'>.</span><span class='id identifier rubyid_to_sql'>to_sql</span><span class='embexpr_end'>}</span><span class='tstring_content'>
|
253
|
+
)
|
254
|
+
)
|
255
|
+
</span><span class='tstring_end'>)</span></span><span class='rparen'>)</span>
|
256
|
+
<span class='kw'>end</span>
|
257
|
+
<span class='kw'>end</span>
|
258
|
+
</code></pre>
|
259
|
+
|
260
|
+
<p>This query could then be called in the following ways:</p>
|
261
|
+
|
262
|
+
<pre class="code ruby"><code class="ruby"><span class='const'>FetchUsersInGroup</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span><span class='lparen'>(</span>
|
263
|
+
<span class='const'>GroupsUser</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>user_id:</span> <span class='int'>1</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span><span class='lparen'>(</span><span class='symbol'>:group_id</span><span class='rparen'>)</span>
|
264
|
+
<span class='rparen'>)</span>
|
265
|
+
|
266
|
+
<span class='comment'># In this example, we're not specifying any select for the relation we pass to
|
267
|
+
</span><span class='comment'># the query class. This is fine because the query automatically defaults to
|
268
|
+
</span><span class='comment'># selecting `id` if exactly one field is required (`fields: 1`) and no select is
|
269
|
+
</span><span class='comment'># specifyed. You can control this further with the option `default_select`.
|
270
|
+
</span><span class='const'>FetchUsersInGroup</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span><span class='lparen'>(</span><span class='const'>Group</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>color:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>red</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
|
271
|
+
</code></pre>
|
272
|
+
|
273
|
+
<h2 id="label-Parameters">Parameters</h2>
|
274
|
+
|
275
|
+
<p>Both query classes can be parameterized using a hash called
|
276
|
+
<code>params</code>. It is recommended to specify and validate input
|
277
|
+
parameters in every query. For this purpose, Inquery provides the
|
278
|
+
<code>schema</code> method witch integrates the <a
|
279
|
+
href="https://github.com/sitrox/schemacop">Schemacop</a> validation Gem:</p>
|
280
|
+
|
281
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>SomeQueryClass</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span>
|
282
|
+
<span class='id identifier rubyid_schema'>schema</span><span class='lparen'>(</span>
|
283
|
+
<span class='label'>some_param:</span> <span class='symbol'>:integer</span><span class='comma'>,</span>
|
284
|
+
<span class='label'>some_other_param:</span> <span class='lbrace'>{</span>
|
285
|
+
<span class='label'>hash:</span> <span class='lbrace'>{</span>
|
286
|
+
<span class='label'>some_field:</span> <span class='symbol'>:string</span>
|
287
|
+
<span class='rbrace'>}</span>
|
288
|
+
<span class='rbrace'>}</span>
|
289
|
+
<span class='rparen'>)</span>
|
290
|
+
|
291
|
+
<span class='comment'># ...
|
292
|
+
</span><span class='kw'>end</span>
|
293
|
+
</code></pre>
|
294
|
+
|
295
|
+
<p>The schema is validated at query class instantiation. An exception will be
|
296
|
+
raised if the given params do not match the schema specified. See
|
297
|
+
documentation of the Schemacop Gem for more information on how to specify
|
298
|
+
schemas.</p>
|
299
|
+
|
300
|
+
<p>Parameters can be accessed using either <code>params</code> or
|
301
|
+
<code>osparams</code>. The method <code>osparams</code> automatically wraps
|
302
|
+
<code>params</code> in an <code>OpenStruct</code> for more convenient
|
303
|
+
access.</p>
|
304
|
+
|
305
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>SomeQueryClass</span> <span class='op'><</span> <span class='const'>Inquery</span><span class='op'>::</span><span class='const'>Query</span>
|
306
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_run'>run</span>
|
307
|
+
<span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span>
|
308
|
+
<span class='label'>active:</span> <span class='id identifier rubyid_params'>params</span><span class='lbracket'>[</span><span class='symbol'>:active</span><span class='rbracket'>]</span><span class='comma'>,</span>
|
309
|
+
<span class='label'>username:</span> <span class='id identifier rubyid_osparams'>osparams</span><span class='period'>.</span><span class='id identifier rubyid_search'>search</span>
|
310
|
+
<span class='rparen'>)</span>
|
311
|
+
<span class='kw'>end</span>
|
312
|
+
<span class='kw'>end</span>
|
313
|
+
</code></pre>
|
314
|
+
|
315
|
+
<h2 id="label-Rails+integration">Rails integration</h2>
|
316
|
+
|
317
|
+
<p>While it is optional, Inquery has been written from the ground up to be
|
318
|
+
perfectly integrated into any Rails application. It has proven to be a
|
319
|
+
winning concept to extract all complex queries into separate classes that
|
320
|
+
are independently executable and testable.</p>
|
321
|
+
|
322
|
+
<h3 id="label-Directory+structure">Directory structure</h3>
|
323
|
+
|
324
|
+
<p>While not enforced, it is encouraged to use the following structure for
|
325
|
+
storing your query classes:</p>
|
326
|
+
<ul><li>
|
327
|
+
<p>All domain-specific query classes reside in <code>app/queries</code>.</p>
|
328
|
+
</li><li>
|
329
|
+
<p>They're in the module <code>Queries</code>.</p>
|
330
|
+
</li><li>
|
331
|
+
<p>Queries are further grouped by the model they return (and not the model
|
332
|
+
they receive). For instance, a class fetching all active users could be
|
333
|
+
located at <code>Queries::User::FetchActive</code> and would reside under
|
334
|
+
<code>app/queries/user/fetch_active.rb</code>.</p>
|
335
|
+
</li></ul>
|
336
|
+
|
337
|
+
<p>There are some key benefits to this approach:</p>
|
338
|
+
<ul><li>
|
339
|
+
<p>As it should, domain-specific code is located within <code>app/</code>.</p>
|
340
|
+
</li><li>
|
341
|
+
<p>As queries are grouped by the model they return and consistently named,
|
342
|
+
they're easy to locate and it does not take much thought where to put
|
343
|
+
and how to name new query classes.</p>
|
344
|
+
</li><li>
|
345
|
+
<p>As there is a single file per query class, it's a breeze to list all
|
346
|
+
queries, i.e. to check their naming for consistency.</p>
|
347
|
+
</li><li>
|
348
|
+
<p>If you're using the same layout for your unit tests, it is absolutely
|
349
|
+
clear where to find the corresponding unit tests for each one of your
|
350
|
+
query classes.</p>
|
351
|
+
</li></ul>
|
352
|
+
|
353
|
+
<h2 id="label-Copyright">Copyright</h2>
|
354
|
+
|
355
|
+
<p>Copyright © 2016 Sitrox. See <code>LICENSE</code> for further details.</p>
|
356
|
+
</div></div>
|
357
|
+
|
358
|
+
<div id="footer">
|
359
|
+
Generated on Thu Jun 9 10:26:31 2016 by
|
360
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
361
|
+
0.8.7.6 (ruby-2.2.3).
|
362
|
+
</div>
|
363
|
+
|
364
|
+
</body>
|
365
|
+
</html>
|
data/doc/file_list.html
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
|
+
|
7
|
+
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" charset="utf-8" />
|
8
|
+
|
9
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
14
|
+
|
15
|
+
<script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
|
16
|
+
|
17
|
+
|
18
|
+
<title>File List</title>
|
19
|
+
<base id="base_target" target="_parent" />
|
20
|
+
</head>
|
21
|
+
<body>
|
22
|
+
<script type="text/javascript" charset="utf-8">
|
23
|
+
var hasFrames = false;
|
24
|
+
try {
|
25
|
+
hasFrames = window.top.frames.main ? true : false;
|
26
|
+
} catch (e) { }
|
27
|
+
if (hasFrames) {
|
28
|
+
document.getElementById('base_target').target = 'main';
|
29
|
+
document.body.className = 'frames';
|
30
|
+
}
|
31
|
+
</script>
|
32
|
+
<div id="content">
|
33
|
+
<h1 id="full_list_header">File List</h1>
|
34
|
+
<div id="nav">
|
35
|
+
|
36
|
+
<span><a target="_self" href="class_list.html">
|
37
|
+
Classes
|
38
|
+
</a></span>
|
39
|
+
|
40
|
+
<span><a target="_self" href="method_list.html">
|
41
|
+
Methods
|
42
|
+
</a></span>
|
43
|
+
|
44
|
+
<span><a target="_self" href="file_list.html">
|
45
|
+
Files
|
46
|
+
</a></span>
|
47
|
+
|
48
|
+
</div>
|
49
|
+
<div id="search">Search: <input type="text" /></div>
|
50
|
+
|
51
|
+
<ul id="full_list" class="file">
|
52
|
+
|
53
|
+
|
54
|
+
<li class="r1"><span class="object_link"><a href="index.html" title="README">README</a></a></li>
|
55
|
+
|
56
|
+
|
57
|
+
</ul>
|
58
|
+
</div>
|
59
|
+
</body>
|
60
|
+
</html>
|
data/doc/frames.html
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
|
3
|
+
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
+
<title>Documentation by YARD 0.8.7.6</title>
|
8
|
+
</head>
|
9
|
+
<script type="text/javascript" charset="utf-8">
|
10
|
+
window.onload = function() {
|
11
|
+
var match = unescape(window.location.hash).match(/^#!(.+)/);
|
12
|
+
var name = match ? match[1] : 'index.html';
|
13
|
+
name = name.replace(/^(\w+):\/\//, '').replace(/^\/\//, '');
|
14
|
+
document.writeln('<frameset cols="20%,*">' +
|
15
|
+
'<frame name="list" src="class_list.html" />' +
|
16
|
+
'<frame name="main" src="' + escape(name) + '" />' +
|
17
|
+
'</frameset>');
|
18
|
+
}
|
19
|
+
</script>
|
20
|
+
<noscript>
|
21
|
+
<frameset cols="20%,*">
|
22
|
+
<frame name="list" src="class_list.html" />
|
23
|
+
<frame name="main" src="index.html" />
|
24
|
+
</frameset>
|
25
|
+
</noscript>
|
26
|
+
</html>
|