tiny_dyno 0.1.11 → 0.1.12
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.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +19 -5
- data/lib/tiny_dyno/document.rb +11 -5
- data/lib/tiny_dyno/errors/hash_key_errors.rb +27 -0
- data/lib/tiny_dyno/fields/range_key.rb +35 -0
- data/lib/tiny_dyno/fields.rb +9 -2
- data/lib/tiny_dyno/hash_key.rb +4 -10
- data/lib/tiny_dyno/version.rb +1 -1
- metadata +3 -4
- data/lib/tiny_dyno/range_attributes.rb +0 -15
- data/lib/tiny_dyno/range_key.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 042ea3b53fd10c8a3a6b8ab228d568e501b1b843
|
4
|
+
data.tar.gz: 5222389a219e25dc9a40b88a6ed748e54fb381d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 051dabb980854bc99bcb736ea43bea31df12c0eecdbf5e848c5e40f1a20e5bc6b8338e4239b218552597fb0929bfd1c1e427850e6ca1495d9315e45f77df47e5
|
7
|
+
data.tar.gz: 7df845254448752c5eb6c16ea6d1ed7fa954adc55b2dbe69e11fba1b5f47f024d01508cf6b5277b2fc1a871c297cb9ddeb240462be3d06de5b94ec7b64c74ad7
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -81,18 +81,32 @@ Or install it yourself as:
|
|
81
81
|
|
82
82
|
## Usage
|
83
83
|
|
84
|
+
It is highly recommended to work through the specs and also the fabricators and models in the spec folder to see example implementations.
|
85
|
+
|
84
86
|
```
|
85
87
|
|
88
|
+
require 'securerandom'
|
86
89
|
require 'tiny_dyno'
|
87
90
|
|
88
|
-
|
91
|
+
|
92
|
+
class Account
|
89
93
|
include TinyDyno::Document
|
90
94
|
|
91
|
-
hash_key :id, type:
|
95
|
+
hash_key :id, type: String
|
96
|
+
|
97
|
+
field :email, type: String, range_key: true
|
98
|
+
field :label, type: String
|
99
|
+
|
100
|
+
validates_presence_of :id, :email, :label
|
101
|
+
|
102
|
+
def initialize(attrs = nil)
|
103
|
+
super
|
104
|
+
set_id if id.nil?
|
105
|
+
end
|
92
106
|
|
93
|
-
|
94
|
-
|
95
|
-
|
107
|
+
def set_id
|
108
|
+
self.id ||= SecureRandom.uuid
|
109
|
+
end
|
96
110
|
|
97
111
|
end
|
98
112
|
|
data/lib/tiny_dyno/document.rb
CHANGED
@@ -53,8 +53,9 @@ module TinyDyno
|
|
53
53
|
|
54
54
|
module ClassMethods
|
55
55
|
|
56
|
+
# TODO, extract into its own class to allow better testing
|
56
57
|
def where(options = {})
|
57
|
-
|
58
|
+
validate_option_keys(options)
|
58
59
|
get_query = build_where_query(options)
|
59
60
|
attributes = TinyDyno::Adapter.get_item(get_item_request: get_query)
|
60
61
|
if attributes.nil?
|
@@ -67,16 +68,21 @@ module TinyDyno
|
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
71
|
+
|
70
72
|
private
|
71
73
|
|
72
74
|
# minimimum implementation for now
|
73
75
|
# check that each option key relates to a hash_key present on the model
|
74
76
|
# do not permit scan queries
|
75
|
-
def
|
77
|
+
def validate_option_keys(options)
|
76
78
|
raise TinyDyno::Errors::HashKeyOnly.new(klass: self.class, name: 'primary_key') if primary_key.nil?
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
options.keys.each do |key|
|
80
|
+
raise TinyDyno::Errors::InvalidSelector.new(klass: self.class, name: key) unless valid_field_selector?(field_name: key.to_s)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def valid_field_selector?(field_name:)
|
85
|
+
key_schema.map {|k| k[:attribute_name] }.include?(field_name)
|
80
86
|
end
|
81
87
|
|
82
88
|
# minimimum implementation for now
|
@@ -102,3 +102,30 @@ module TinyDyno
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
|
106
|
+
# encoding: utf-8
|
107
|
+
module TinyDyno
|
108
|
+
module Errors
|
109
|
+
|
110
|
+
# This error is raised, when a query is performed with fields specified
|
111
|
+
# that are not HashKeys, which would result in a table scan
|
112
|
+
class InvalidSelector < TinyDynoError
|
113
|
+
|
114
|
+
# Create the new error.
|
115
|
+
#
|
116
|
+
# @example Instantiate the error.
|
117
|
+
# InvalidSelector.new(Person, "gender")
|
118
|
+
#
|
119
|
+
# @param [ Class ] klass The model class.
|
120
|
+
# @param [ String, Symbol ] name The name of the attribute.
|
121
|
+
#
|
122
|
+
# @since 3.0.0
|
123
|
+
def initialize(klass:, name:)
|
124
|
+
super(
|
125
|
+
compose_message("look up only by key fields", { klass: klass.name, name: name })
|
126
|
+
)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module TinyDyno
|
2
|
+
module Fields
|
3
|
+
class RangeKey
|
4
|
+
|
5
|
+
# Defines the behaviour for defined fields in the document.
|
6
|
+
# Set readers for the instance variables.
|
7
|
+
attr_accessor :default_val, :label, :name, :options
|
8
|
+
|
9
|
+
# Create the new field with a name and optional additional options.
|
10
|
+
#
|
11
|
+
# @example Create the new field.
|
12
|
+
# Field.new(:name, :type => String)
|
13
|
+
#
|
14
|
+
# @param [ Hash ] options The field options.
|
15
|
+
#
|
16
|
+
# @option options [ Class ] :type The class of the field.
|
17
|
+
# @option options [ Object ] :default The default value for the field.
|
18
|
+
# @option options [ String ] :label The field's label.
|
19
|
+
#
|
20
|
+
# @since 3.0.0
|
21
|
+
def initialize(name, options = {})
|
22
|
+
@name = name
|
23
|
+
@options = options
|
24
|
+
@label = options[:label]
|
25
|
+
@default_val = options[:default]
|
26
|
+
@type = options[:type]
|
27
|
+
end
|
28
|
+
|
29
|
+
def as_selector
|
30
|
+
binding.pry
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/tiny_dyno/fields.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'tiny_dyno/fields/standard'
|
2
|
+
require 'tiny_dyno/fields/range_key'
|
2
3
|
|
3
4
|
module TinyDyno
|
4
5
|
module Fields
|
@@ -166,7 +167,6 @@ module TinyDyno
|
|
166
167
|
# @param [ Field ] field the field to process
|
167
168
|
def process_options(field)
|
168
169
|
field_options = field.options
|
169
|
-
|
170
170
|
Fields.options.each_pair do |option_name, handler|
|
171
171
|
if field_options.key?(option_name)
|
172
172
|
handler.call(self, field, field_options[option_name])
|
@@ -176,7 +176,14 @@ module TinyDyno
|
|
176
176
|
|
177
177
|
def field_for(name, options)
|
178
178
|
opts = options.merge(klass: self)
|
179
|
-
|
179
|
+
if opts.has_key?(:range_key) && opts[:range_key] == true
|
180
|
+
named = name.to_s
|
181
|
+
attribute_definitions << build_attribute_definition(named,options[:type])
|
182
|
+
key_schema << { attribute_name: named, key_type: 'RANGE' }
|
183
|
+
Fields::RangeKey.new(name, opts)
|
184
|
+
else
|
185
|
+
Fields::Standard.new(name, opts)
|
186
|
+
end
|
180
187
|
end
|
181
188
|
|
182
189
|
# Create the field accessors.
|
data/lib/tiny_dyno/hash_key.rb
CHANGED
@@ -37,7 +37,7 @@ module TinyDyno
|
|
37
37
|
raise TinyDyno::Errors::OnlyOneHashKeyPermitted.new(klass: self.class, name: name) unless primary_key.empty?
|
38
38
|
named = name.to_s
|
39
39
|
attribute_definition = build_attribute_definition(named,options[:type])
|
40
|
-
key_schema =
|
40
|
+
key_schema = hash_key_schema(named)
|
41
41
|
unless attribute_definition_meets_spec?(attribute_definition)
|
42
42
|
raise TinyDyno::Errors::InvalidHashKey.new(klass: self.class, name: name)
|
43
43
|
end
|
@@ -52,12 +52,6 @@ module TinyDyno
|
|
52
52
|
}
|
53
53
|
end
|
54
54
|
|
55
|
-
# convert a hash key into a format as expected by
|
56
|
-
# put_item and update_item request
|
57
|
-
def as_item_entry(hash_key)
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
55
|
private
|
62
56
|
|
63
57
|
# Return true or false, depending on whether the attribute_definitions on the model
|
@@ -80,17 +74,17 @@ module TinyDyno
|
|
80
74
|
def build_attribute_definition(name, key_type)
|
81
75
|
{
|
82
76
|
attribute_name: name,
|
83
|
-
attribute_type:
|
77
|
+
attribute_type: determine_key_class(key_type)
|
84
78
|
}
|
85
79
|
end
|
86
80
|
|
87
|
-
def
|
81
|
+
def determine_key_class(key_type = nil)
|
88
82
|
return 'S' if key_type == String
|
89
83
|
return 'N' if key_type == Fixnum or key_type == Integer
|
90
84
|
return nil
|
91
85
|
end
|
92
86
|
|
93
|
-
def
|
87
|
+
def hash_key_schema(name)
|
94
88
|
{
|
95
89
|
attribute_name: name,
|
96
90
|
key_type: 'HASH'
|
data/lib/tiny_dyno/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_dyno
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Gerschner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -190,13 +190,12 @@ files:
|
|
190
190
|
- lib/tiny_dyno/extensions.rb
|
191
191
|
- lib/tiny_dyno/extensions/module.rb
|
192
192
|
- lib/tiny_dyno/fields.rb
|
193
|
+
- lib/tiny_dyno/fields/range_key.rb
|
193
194
|
- lib/tiny_dyno/fields/standard.rb
|
194
195
|
- lib/tiny_dyno/hash_key.rb
|
195
196
|
- lib/tiny_dyno/hash_keys.rb
|
196
197
|
- lib/tiny_dyno/loggable.rb
|
197
198
|
- lib/tiny_dyno/persistable.rb
|
198
|
-
- lib/tiny_dyno/range_attributes.rb
|
199
|
-
- lib/tiny_dyno/range_key.rb
|
200
199
|
- lib/tiny_dyno/stateful.rb
|
201
200
|
- lib/tiny_dyno/tables.rb
|
202
201
|
- lib/tiny_dyno/version.rb
|
data/lib/tiny_dyno/range_key.rb
DELETED
File without changes
|