commutator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Changelog.md +15 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +55 -0
- data/README.md +86 -0
- data/Rakefile +6 -0
- data/bin/console +26 -0
- data/bin/setup +8 -0
- data/commutator.gemspec +29 -0
- data/lib/commutator/collection.rb +78 -0
- data/lib/commutator/expressions/attribute_names.rb +24 -0
- data/lib/commutator/expressions/attribute_values.rb +21 -0
- data/lib/commutator/expressions/condition_expression.rb +101 -0
- data/lib/commutator/expressions/projection_expression.rb +50 -0
- data/lib/commutator/expressions/statement.rb +38 -0
- data/lib/commutator/expressions/update_expression.rb +61 -0
- data/lib/commutator/item_modifiers.rb +24 -0
- data/lib/commutator/model/attributes.rb +72 -0
- data/lib/commutator/model/hooks.rb +41 -0
- data/lib/commutator/model/table_configuration.rb +41 -0
- data/lib/commutator/model.rb +239 -0
- data/lib/commutator/options/delete_item.rb +48 -0
- data/lib/commutator/options/get_item.rb +41 -0
- data/lib/commutator/options/proxy.rb +90 -0
- data/lib/commutator/options/put_item.rb +46 -0
- data/lib/commutator/options/query.rb +73 -0
- data/lib/commutator/options/scan.rb +60 -0
- data/lib/commutator/options/update_item.rb +55 -0
- data/lib/commutator/simple_client.rb +27 -0
- data/lib/commutator/util/fluent.rb +67 -0
- data/lib/commutator/util/placeholders.rb +607 -0
- data/lib/commutator/version.rb +3 -0
- data/lib/commutator.rb +64 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a212280a2711c6edae1077d491db74fb9186104
|
4
|
+
data.tar.gz: ea5708c8299dd8cc5446e90417c3e6824b2b70b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bdbe600527356e277e86af158e387d0c78250254ee7a07af6e92d2a91a663e44340c0c9f442049174aaa9123ab921bc9df7cc103e11e0a363ee2bb242ce7d778
|
7
|
+
data.tar.gz: 1dd47ee873c1fe1447eeac8ab25a5c99ae4cf51bd257c065a1537caf77ca8756aa1add7a385cdd98656683fedb40c6b65e2cf9b88c83a0cb1175ca82b5bf76fb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Changelog.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
### Development
|
2
|
+
[Full Changelog](http://github.com/tablexi/commutator/compare/v0.1.0...master)
|
3
|
+
|
4
|
+
|
5
|
+
### 0.1.0 / 2016-01-11
|
6
|
+
|
7
|
+
Initial release!
|
8
|
+
|
9
|
+
* No tests, not guaranteed to be fully functional (sorry!)
|
10
|
+
* Operations include Query, Scan, PutItem, UpdateItem, DeleteItem, GetItem
|
11
|
+
* Support for AR-like 'scopes' via defining a `Scopes` module within a model
|
12
|
+
* `modify_collection_items_with proc, factory: [boolean](default: false)` gives
|
13
|
+
ability to decorate items in a collection (e.g. for caching relation lookups)…
|
14
|
+
it's a tricky feature to explain :/
|
15
|
+
* collection supports pagination or 'normal' iteration via `#each_page` and `#each_item`
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
Copyright (c) 2016 Bradley Schaefer
|
2
|
+
|
3
|
+
Apache License
|
4
|
+
|
5
|
+
Version 2.0, January 2004
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/
|
8
|
+
|
9
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
10
|
+
|
11
|
+
1. Definitions.
|
12
|
+
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
14
|
+
|
15
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
16
|
+
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
18
|
+
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
20
|
+
|
21
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
22
|
+
|
23
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
24
|
+
|
25
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
26
|
+
|
27
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
28
|
+
|
29
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
30
|
+
|
31
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
32
|
+
|
33
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
34
|
+
|
35
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
36
|
+
|
37
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
38
|
+
|
39
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
40
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
42
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
43
|
+
|
44
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
46
|
+
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
48
|
+
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
50
|
+
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
52
|
+
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
54
|
+
|
55
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Commutator
|
2
|
+
|
3
|
+
Commutator is a Model object interface for Amazon DynamoDB.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'commutator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install commutator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Needs work
|
24
|
+
|
25
|
+
Basic usage example..
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Paint
|
29
|
+
include Commutator::Model
|
30
|
+
|
31
|
+
table_name "paint_products"
|
32
|
+
|
33
|
+
attribute :name, :hex_color, type: :string
|
34
|
+
attribute :created_ts, :updated_at, type: :integer
|
35
|
+
|
36
|
+
validates :name, :hex_color, presence: true
|
37
|
+
|
38
|
+
primary_key hash: :name, range: :created_ts
|
39
|
+
|
40
|
+
before_put_item :prevent_dupes, :add_timestamps
|
41
|
+
|
42
|
+
module Scopes
|
43
|
+
def by_hex(hex)
|
44
|
+
index_name('hex-index')
|
45
|
+
with_key_condition_expression do |expression|
|
46
|
+
expression.where('#? = :?', names: %w(hex_color), values: [hex_color.downcase])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def prevent_dupes(options)
|
54
|
+
options.condition_expression.where_not do |expression|
|
55
|
+
expression.where('name = :?', values: [name])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_timestamps(options)
|
60
|
+
now = Time.now.to_i
|
61
|
+
|
62
|
+
options.item['updated_at'] = now
|
63
|
+
options.item['created_at'] ||= now
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Color.by_hex("#BBAADD").first
|
68
|
+
Color.create(name: "Black", hex_color: "#000000")
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
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.
|
75
|
+
|
76
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tablexi/commutator.
|
81
|
+
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [Apache License, Version 2.0](http://opensource.org/licenses/Apache-2.0).
|
86
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "commutator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
its_irb_time = true
|
14
|
+
|
15
|
+
begin
|
16
|
+
require "pry"
|
17
|
+
its_irb_time = false
|
18
|
+
Pry.start
|
19
|
+
rescue LoadError
|
20
|
+
# do nothing
|
21
|
+
end
|
22
|
+
|
23
|
+
if its_irb_time
|
24
|
+
require "irb"
|
25
|
+
IRB.start
|
26
|
+
end
|
data/bin/setup
ADDED
data/commutator.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'commutator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "commutator"
|
8
|
+
spec.version = Commutator::VERSION
|
9
|
+
spec.authors = ["Bradley Schaefer", "Ben Kimpel"]
|
10
|
+
spec.email = ["bradley.schaefer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Model object interface for Amazon DynamoDB.}
|
13
|
+
spec.description = %q{Model object interface for Amazon DynamoDB.}
|
14
|
+
spec.homepage = "https://github.com/tablexi/commutator"
|
15
|
+
spec.license = "Apache License, Version 2.0"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
25
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
26
|
+
spec.add_dependency "activemodel", "~> 4.0"
|
27
|
+
spec.add_dependency "aws-sdk", "~> 2.1"
|
28
|
+
spec.add_dependency "ice_nine"
|
29
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Commutator
|
2
|
+
# Wraps a DynamoDB response from query or scan operations to provide
|
3
|
+
# a collection of instances of the given class.
|
4
|
+
#
|
5
|
+
# NOTE: This can't use SimpleDelegator because `Seahorse::Client::Response` does
|
6
|
+
# not implement `#respond_to?` as needed.
|
7
|
+
class Collection
|
8
|
+
delegate :count,
|
9
|
+
:scanned_count,
|
10
|
+
:last_evaluated_key,
|
11
|
+
:consumed_capacity,
|
12
|
+
:next_page?,
|
13
|
+
to: :response
|
14
|
+
|
15
|
+
def initialize(response, klass, modifiers: [])
|
16
|
+
@response = response
|
17
|
+
@klass = klass
|
18
|
+
@modifiers = modifiers.map(&:expand_proc_modifiers).freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
def items
|
22
|
+
return [] if response.items.nil?
|
23
|
+
|
24
|
+
response.items.map do |item|
|
25
|
+
item = klass.new(item)
|
26
|
+
modify_item(item)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Caution! This advances a page pointer in the response and there's no
|
31
|
+
# going back. It will affect how each_page and each_item behaves.
|
32
|
+
# In most cases it's better to use one of the each methods.
|
33
|
+
def next_page
|
34
|
+
self.class.new(response.next_page, klass)
|
35
|
+
end
|
36
|
+
|
37
|
+
def each_page
|
38
|
+
return enum_for(:each_page) unless block_given?
|
39
|
+
|
40
|
+
response.each do |page|
|
41
|
+
next unless page.items.present?
|
42
|
+
page = self.class.new(page, klass, modifiers: @modifiers)
|
43
|
+
yield(page)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def each_item
|
48
|
+
return enum_for(:each_item) unless block_given?
|
49
|
+
|
50
|
+
each_page do |page|
|
51
|
+
page.items.each { |item| yield(item) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def all_items
|
56
|
+
each_item.each_with_object([]) { |item, arr| arr << item }
|
57
|
+
end
|
58
|
+
|
59
|
+
def modify_with(*modifiers, factory: false)
|
60
|
+
modifier = ItemModifiers.new(modifiers, factory: factory)
|
61
|
+
# preserves decorator ordering with minimal object creation
|
62
|
+
new_modifiers = [modifier].unshift(*@modifiers)
|
63
|
+
|
64
|
+
self.class.new(response, klass, modifiers: new_modifiers)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def modify_item(item)
|
70
|
+
@modifiers.each do |modifier|
|
71
|
+
modifier.modify(item)
|
72
|
+
end
|
73
|
+
item
|
74
|
+
end
|
75
|
+
|
76
|
+
attr_reader :response, :klass
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
class AttributeNames
|
4
|
+
def initialize
|
5
|
+
@names = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(name)
|
9
|
+
name = name.to_s
|
10
|
+
placeholder = Util::Placeholders.name(name)
|
11
|
+
|
12
|
+
names[placeholder] = name unless placeholder == name
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_h
|
16
|
+
Marshal.load(Marshal.dump(names))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :names
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
class AttributeValues
|
4
|
+
def initialize
|
5
|
+
@values = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(value)
|
9
|
+
values[Util::Placeholders.value(value)] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_h
|
13
|
+
Marshal.load(Marshal.dump(values))
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :values
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
# See: http://goo.gl/bhIUgS
|
4
|
+
#
|
5
|
+
# Constructs composable ConditionExpression for use in
|
6
|
+
# DynamoDB API calls. Call to_s when sending to DynamoDB.
|
7
|
+
#
|
8
|
+
# Note:
|
9
|
+
# where, and, or, and_not, or_not, where_not pass their arguments to
|
10
|
+
# the Statement constructor. Read the docs there for placeholder syntax.
|
11
|
+
#
|
12
|
+
# Usage:
|
13
|
+
# exp = Commutator::Expressions::ConditionExpression.new
|
14
|
+
# .where('token_channel = :?', values: ['123'])
|
15
|
+
# .and('ts > :?', values: [1344])
|
16
|
+
#
|
17
|
+
# exp.or do |expression|
|
18
|
+
# expression
|
19
|
+
# .where('attribute_exists(#?)', names: ['count'])
|
20
|
+
# .and('attribute_exists(something)')
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# Later...
|
24
|
+
#
|
25
|
+
# exp.and { |e| e.where('ts BETWEEN :? AND :?', values: [1, 2]) }
|
26
|
+
#
|
27
|
+
# The expression above would produce the following with `to_s`
|
28
|
+
#
|
29
|
+
# "token_channel = :VALUE_757d204b68e8e1c419288694ab908f55 AND
|
30
|
+
# ts > :VALUE_a50abba8132a77191791390c3eb19fe7 OR
|
31
|
+
# (attribute_exists(#NAME_d61a1061060c9e9691027c42d6766b90) AND attribute_exists(something)) AND
|
32
|
+
# (ts BETWEEN :VALUE_c4ca4238a0b923820dcc509a6f75849b AND :VALUE_c81e728d9d4c2f636f067f89cc14862c)"
|
33
|
+
class ConditionExpression
|
34
|
+
attr_reader :attribute_names, :attribute_values
|
35
|
+
|
36
|
+
def initialize(attribute_names: nil, attribute_values: nil, &block)
|
37
|
+
@attribute_names = attribute_names || AttributeNames.new
|
38
|
+
@attribute_values = attribute_values || AttributeValues.new
|
39
|
+
end
|
40
|
+
|
41
|
+
def statements
|
42
|
+
@statements ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
def or(*args, &block)
|
46
|
+
add_statement('OR', *args, &block)
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def or_not(*args, &block)
|
51
|
+
add_statement(%w(OR NOT), *args, &block)
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def and(*args, &block)
|
56
|
+
add_statement('AND', *args, &block)
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def and_not(*args, &block)
|
61
|
+
add_statement(%w(AND NOT), *args, &block)
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
alias_method :where, :and
|
66
|
+
alias_method :where_not, :and_not
|
67
|
+
|
68
|
+
def to_s(wrap: true)
|
69
|
+
str = statements.map(&:to_s).join(' ')
|
70
|
+
wrap ? "(#{str})" : str
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def add_statement(operator, exp = nil, values: [], names: [], &block)
|
76
|
+
return self if exp.nil? && block.nil?
|
77
|
+
|
78
|
+
statement =
|
79
|
+
if block
|
80
|
+
exp = self.class.new(attribute_names: attribute_names,
|
81
|
+
attribute_values: attribute_values)
|
82
|
+
block.call(exp)
|
83
|
+
else
|
84
|
+
Statement.new(exp, values: values, names: names)
|
85
|
+
end
|
86
|
+
|
87
|
+
if statements.present?
|
88
|
+
operator = operator.join(' ') if operator.is_a?(Array)
|
89
|
+
else
|
90
|
+
operator = operator.is_a?(Array) ? operator.last : nil
|
91
|
+
end
|
92
|
+
|
93
|
+
statements << operator if operator.present?
|
94
|
+
statements << statement
|
95
|
+
|
96
|
+
values.each { |v| attribute_values.add(v) }
|
97
|
+
names.each { |n| attribute_names.add(n) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
# See: http://goo.gl/bOKUjK
|
4
|
+
#
|
5
|
+
# Constructs composable ProjectionExpression for use in
|
6
|
+
# DynamoDB API calls. Call to_s when sending to DynamoDB.
|
7
|
+
#
|
8
|
+
# Note:
|
9
|
+
# path passes its arguments to the Statement constructor. Read the
|
10
|
+
# docs there for placeholder syntax.
|
11
|
+
#
|
12
|
+
# Usage:
|
13
|
+
# exp = Commutator::Expressions::ProjectionExpression.new
|
14
|
+
# .path('token_channel')
|
15
|
+
# .path('ts.items[2]')
|
16
|
+
# .path('#?', names: ['count'])
|
17
|
+
# .path('#?.hey', names: ['size'])
|
18
|
+
#
|
19
|
+
# Later...
|
20
|
+
#
|
21
|
+
# exp.path('another_one')
|
22
|
+
#
|
23
|
+
# The expression above would produce the following with `to_s`
|
24
|
+
#
|
25
|
+
# "token_channel, ts.items[2], #NAME_d61a1061060c9e9691027c42d6766b90,
|
26
|
+
# #NAME_1eb3b08a347050ee467a2e24e6c15349.hey, another_one"
|
27
|
+
class ProjectionExpression
|
28
|
+
attr_reader :attribute_names
|
29
|
+
|
30
|
+
def initialize(attribute_names: nil, &block)
|
31
|
+
@attribute_names = attribute_names || AttributeNames.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def statements
|
35
|
+
@statements ||= []
|
36
|
+
end
|
37
|
+
|
38
|
+
def path(p, names: [])
|
39
|
+
names.each { |n| attribute_names.add(n) }
|
40
|
+
statements << Statement.new(p, names: names)
|
41
|
+
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
statements.join(', ')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
# Statement simply takes a string and substitutes '#?' for name placeholders
|
4
|
+
# and ':?' for value placeholders.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# Statement.new('#? BETWEEN :? AND :?', names: ['ts'], values: [100, 200])
|
8
|
+
#
|
9
|
+
# produces...
|
10
|
+
#
|
11
|
+
# "ts BETWEEN :VALUE_f899139df5e1059396431415e770c6dd AND :VALUE_3644a684f98ea8fe223c713b77189a77"
|
12
|
+
# (Note: ts was not replaced because it's not a reserved word)
|
13
|
+
#
|
14
|
+
# Another example:
|
15
|
+
# Statement.new('attribute_exists(#?)', names: ['count']).to_s
|
16
|
+
#
|
17
|
+
# produces...
|
18
|
+
#
|
19
|
+
# "attribute_exists(#NAME_d61a1061060c9e9691027c42d6766b90)"
|
20
|
+
class Statement
|
21
|
+
attr_accessor :exp, :names, :values
|
22
|
+
|
23
|
+
def initialize(exp, names: [], values: [])
|
24
|
+
@exp = exp
|
25
|
+
@names = names
|
26
|
+
@values = values
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
exp
|
31
|
+
.gsub('#?')
|
32
|
+
.each_with_index { |_v, i| Util::Placeholders.name(names[i]) }
|
33
|
+
.gsub(':?')
|
34
|
+
.each_with_index { |_v, i| Util::Placeholders.value(values[i]) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Commutator
|
2
|
+
module Expressions
|
3
|
+
# See: http://goo.gl/qiHKO3
|
4
|
+
#
|
5
|
+
# Constructs composable UpdateExpression for use in
|
6
|
+
# DynamoDB API calls. Call to_s when sending to DynamoDB.
|
7
|
+
#
|
8
|
+
# Note:
|
9
|
+
# SET, REMOVE, ADD, and DELETE pass their arguments to
|
10
|
+
# the Statement constructor. Read the docs there for placeholder syntax.
|
11
|
+
#
|
12
|
+
# Usage:
|
13
|
+
# exp = Commutator::Expressions::UpdateExpression.new
|
14
|
+
# .set('username = :?', values: ['user1'])
|
15
|
+
# .set('email = :?', values: ['a@b.c'])
|
16
|
+
# .add('login_count :?', values: [1])
|
17
|
+
# .remove('some_attribute')
|
18
|
+
# .delete('some_set :?', values: [10])
|
19
|
+
#
|
20
|
+
# The expression above would produce the following with `to_s`
|
21
|
+
#
|
22
|
+
# "SET username = :VALUE_91120aaf65529fbdc83e66ea160f17bc,
|
23
|
+
# email = :VALUE_73a839aafcf0207ab931d9edd6369a9a
|
24
|
+
# ADD login_count :VALUE_c4ca4238a0b923820dcc509a6f75849b
|
25
|
+
# REMOVE some_attribute
|
26
|
+
# DELETE some_set :VALUE_d3d9446802a44259755d38e6d163e820"
|
27
|
+
class UpdateExpression
|
28
|
+
attr_reader :attribute_names, :attribute_values
|
29
|
+
|
30
|
+
def initialize(attribute_names: nil, attribute_values: nil, &block)
|
31
|
+
@attribute_names = attribute_names || AttributeNames.new
|
32
|
+
@attribute_values = attribute_values || AttributeValues.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def sections
|
36
|
+
@sections ||= Hash.new { |h, v| h[v] = [] }
|
37
|
+
end
|
38
|
+
|
39
|
+
%w(set remove add delete).each do |method_name|
|
40
|
+
define_method method_name do |*args|
|
41
|
+
add_to_section(method_name.downcase.to_sym, *args)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_to_section(name, exp, names: [], values: [])
|
46
|
+
names.each { |n| attribute_names.add(n) }
|
47
|
+
values.each { |v| attribute_values.add(v) }
|
48
|
+
|
49
|
+
sections[name] << Statement.new(exp, names: names, values: values)
|
50
|
+
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
sections
|
56
|
+
.map { |name, actions| "#{name.upcase} #{actions.join(', ')}" }
|
57
|
+
.join(' ')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|