query_relation 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 632b61d4ce3da388ed9f41a5d15e7a342e231d8a
4
- data.tar.gz: c51f0796dfd50d5ad6b885d49ca5c2a68c83c789
3
+ metadata.gz: cb98f89437c07c2bc344a161bab779426ec38e5a
4
+ data.tar.gz: 106a56ab8a18141722be7e7439ef54bde25a6d59
5
5
  SHA512:
6
- metadata.gz: b34c6259edb7a188d7af432ea3f42deda29e0e99b272dfdbb87058ea39a0b7803f6369576ce2dcafeaf9b1d63983e152120038299704702f53cb862e577b4aee
7
- data.tar.gz: 7c8c3c4df2595c1b6e7bc4f5a938a1e73f3bb34f070bcda549e0eebd9cd3b7254910ee9dd22ad84b5257e91901bcc1df467e9e6d63d0f21e88bee2169dbaeca1
6
+ metadata.gz: e4a682b80e9b737cb889c7e3ba5a4cdfc23b93694327c67307bdd1f47171fa11b24015b0b05bd2424ba9b594a51404f9b1cda924c3af83f498cb093d21fd8e45
7
+ data.tar.gz: 6363baddbf6fde7e2a40b209250a59f4a9223c2bb37863b73b1111020cfb85344df32245b9b7dc2c660629c462be01336d8bfc84f549a1bd1d19358fe6568297
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.1]
10
+ ### Added
11
+ - Add CHANGELOG.md
12
+ - Update README with usage instructions.
13
+
14
+ [Unreleased]: https://github.com/ManageIQ/query_relation/compare/v0.1.1...HEAD
15
+ [0.1.0]: https://github.com/ManageIQ/query_relation/compare/v0.1.0...v0.1.1
data/README.md CHANGED
@@ -18,6 +18,69 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install query_relation
20
20
 
21
+ ## Usage
22
+
23
+ The `QueryRelation` class is provided to encapsulate building a relation. It is
24
+ a chainable interface, upon which query methods can be called, and final
25
+ execution is delayed until the last possible moment. It can be instantiated
26
+ directly, but the more common way to get access to one is to mixin the
27
+ `QueryRelation::Queryable` module into your collection.
28
+
29
+ `QueryRelation::Queryable` expects a method named `search` to be implemented by
30
+ the mixee. `search` should be written to take two parameters
31
+
32
+ - `mode`: one of `first`, `last`, or `all`
33
+ - `options`: a Hash containing the choices that were made by the caller. It
34
+ will contain the keys `:where`, `:select`, `:limit`, `:offset`, `:order`,
35
+ `group`, `:includes`, and/or `:references`
36
+
37
+ The `search` method should be written to process this set of options and return
38
+ an Array of the selection. It can use the options and mode in any way it sees
39
+ fit to optimally return the requested values.
40
+
41
+ ### Example
42
+
43
+ Here's a simple example that supports limit and offset over a simple collection
44
+ to give an idea of how one might write a `search` method.
45
+
46
+ ```ruby
47
+ class MyCollection
48
+ include QueryRelation::Queryable
49
+
50
+ private
51
+
52
+ THE_COLLECTION = [1, 2, 3, 4].freeze
53
+
54
+ def search(mode, options)
55
+ collection = filter_collection_by_options(options)
56
+
57
+ case mode
58
+ when :first then collection.first
59
+ when :last then collection.last
60
+ when :all then collection
61
+ end
62
+ end
63
+
64
+ def filter_collection_by_options(options)
65
+ collection = THE_COLLECTION
66
+ collection = collection.drop(options[:offset]) if options[:offset]
67
+ collection = collection.take(options[:limit]) if options[:limit]
68
+ collection
69
+ end
70
+ end
71
+
72
+ MyCollection.new.all.to_a
73
+ # => [1, 2, 3, 4]
74
+ MyCollection.new.limit(2).to_a
75
+ # => [1, 2]
76
+ MyCollection.new.limit(1).offset(2).to_a
77
+ # => [3]
78
+ MyCollection.new.first
79
+ # => 1
80
+ MyCollection.new.last
81
+ # => 4
82
+ ```
83
+
21
84
  ## Development
22
85
 
23
86
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,3 +1,3 @@
1
1
  class QueryRelation
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keenan Brock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-11-14 00:00:00.000000000 Z
12
+ date: 2016-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -92,6 +92,7 @@ files:
92
92
  - ".gitignore"
93
93
  - ".rspec"
94
94
  - ".travis.yml"
95
+ - CHANGELOG.md
95
96
  - CODE_OF_CONDUCT.md
96
97
  - Gemfile
97
98
  - LICENSE.txt