aws-record 1.0.0.pre.6 → 1.0.0.pre.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09d82333932905be1917f2ae907e994673a52cc1
4
- data.tar.gz: 2795843b24c5f42a1501dd79b6114155d8cf4d03
3
+ metadata.gz: 808122fbcc8d09789f250b6ab5f7bfa4af8a511f
4
+ data.tar.gz: 006ed1f3c955fe8370cd4d2ae81adb17195cc77e
5
5
  SHA512:
6
- metadata.gz: 6a9f1b9e229940214d6f1be2fcfcdce24fda842a6b9892d091520be2ef382c974fc62d9d2b656422854b08320820d931e35fda4cd6011b872381a1efea69f47f
7
- data.tar.gz: 9f4b99c7090f18f636fa0d0ae7c6dfbe11ed1074cce76485e1ea7f10095b2004c7a931d034f5c976202ed8cedc997f0080275135346e81a0644b375650761c6b
6
+ metadata.gz: 6586ed1a3add7fbe31dcd37b59f3798a0671b1ec25c0886f68c8c7545f5798fb4d6639c7c0ea14206ccba1d29b75d505ab34c211540026963a4c675542a37b4c
7
+ data.tar.gz: ab2ecaf0359165f4c179af7df4d96bd142ca29f630a65bc9c9985fd7be8987f44fec050b41342ee6eec8ff1d0f5592b93d36f460b170f6943eab6c68d8b410fd
@@ -24,8 +24,6 @@ module Aws
24
24
  # So long as you provide a marshaler which implements +#type_cast+ and
25
25
  # +#serialize+ that consume raw values as expected, you can bring your
26
26
  # own marshaler type.
27
- # @option options [Array] :validators An array of validator classes that
28
- # will be run when an attribute is checked for validity.
29
27
  # @option options [String] :database_attribute_name Optional attribute
30
28
  # used to specify a different name for database persistence than the
31
29
  # `name` parameter. Must be unique (you can't have overlap between
@@ -40,7 +38,6 @@ module Aws
40
38
  @database_name = options[:database_attribute_name] || name.to_s
41
39
  @dynamodb_type = options[:dynamodb_type]
42
40
  @marshaler = options[:marshaler] || DefaultMarshaler
43
- @validators = options[:validators] || []
44
41
  end
45
42
 
46
43
  # Attempts to type cast a raw value into the attribute's type. This call
@@ -62,19 +59,6 @@ module Aws
62
59
  @marshaler.serialize(raw_value)
63
60
  end
64
61
 
65
- # Checks if the raw value is valid for this attribute. This is done by
66
- # type casting the raw value, then running that value through each
67
- # validator present for this attribute.
68
- #
69
- # @return [Boolean] true if the raw value is valid for this attribute,
70
- # false if it is not.
71
- def valid?(raw_value)
72
- value = type_cast(raw_value)
73
- @validators.all? do |validator|
74
- validator.validate(value)
75
- end
76
- end
77
-
78
62
  # @api private
79
63
  def extract(dynamodb_item)
80
64
  dynamodb_item[@database_name]
@@ -58,8 +58,6 @@ module Aws
58
58
  # +#serialize+ that consume raw values as expected, you can bring your
59
59
  # own marshaler type. Convenience methods will provide this for you.
60
60
  # @param [Hash] opts
61
- # @option opts [Array] :validators An array of validator classes that
62
- # will be run when an attribute is checked for validity.
63
61
  # @option opts [String] :database_attribute_name Optional attribute
64
62
  # used to specify a different name for database persistence than the
65
63
  # `name` parameter. Must be unique (you can't have overlap between
@@ -20,6 +20,7 @@ module Aws
20
20
  class KeyMissing < RecordError; end
21
21
  class NotFound < RecordError; end
22
22
  class ItemAlreadyExists < RecordError; end
23
+ class ValidationError < RecordError; end
23
24
 
24
25
  class NameCollision < RuntimeError; end
25
26
  class ReservedName < RuntimeError; end
@@ -18,7 +18,6 @@ module Aws
18
18
  # @api private
19
19
  def self.included(sub_class)
20
20
  sub_class.extend(ItemOperationsClassMethods)
21
- sub_class.instance_variable_set("@errors", [])
22
21
  end
23
22
 
24
23
  # Saves this instance of an item to Amazon DynamoDB. If this item is "new"
@@ -42,35 +41,15 @@ module Aws
42
41
  # does not have a value within this item instance.
43
42
  # @raise [Aws::Record::Errors::ItemAlreadyExists] if a conditional put
44
43
  # fails because the item exists on the remote end.
44
+ # @raise [Aws::Record::Errors::ValidationError] if the item responds to
45
+ # +:valid?+ and that call returned false. In such a case, checking root
46
+ # cause is dependent on the validation library you are using.
45
47
  def save!(opts = {})
46
- force = opts[:force]
47
- expect_new = expect_new_item?
48
- if force
49
- dynamodb_client.put_item(
50
- table_name: self.class.table_name,
51
- item: build_item_for_save
52
- )
53
- elsif expect_new
54
- put_opts = {
55
- table_name: self.class.table_name,
56
- item: build_item_for_save
57
- }.merge(prevent_overwrite_expression)
58
- begin
59
- dynamodb_client.put_item(put_opts)
60
- rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e
61
- raise Errors::ItemAlreadyExists.new(
62
- "Conditional #put_item call failed, an item with the same key"\
63
- " already exists in the table. Either load and update this"\
64
- " item, or include the :force option to clobber the remote"\
65
- " item."
66
- )
67
- end
48
+ ret = save(opts)
49
+ if ret
50
+ ret
68
51
  else
69
- dynamodb_client.update_item(
70
- table_name: self.class.table_name,
71
- key: key_values,
72
- attribute_updates: dirty_changes_for_update
73
- )
52
+ raise Errors::ValidationError.new("Validation hook returned false!")
74
53
  end
75
54
  end
76
55
 
@@ -87,27 +66,19 @@ module Aws
87
66
  # You can use the +:force+ option to perform a simple put/overwrite
88
67
  # without conditional validation or update logic.
89
68
  #
90
- # In the case where persistence fails, will populate the +errors+ array
91
- # with any generated error messages, and will cause +#valid?+ to return
92
- # false until there is a successful save.
93
- #
94
69
  # @param [Hash] opts
95
70
  # @option opts [Boolean] :force if true, will save as a put operation and
96
71
  # overwrite any existing item on the remote end. Otherwise, and by
97
72
  # default, will either perform a conditional put or an update call.
73
+ # @return false if the record is invalid as defined by an attempt to call
74
+ # +valid?+ on this item, if that method exists. Otherwise, returns client
75
+ # call return value.
98
76
  def save(opts = {})
99
- result = save!(opts)
100
- errors.clear
101
- result
102
- rescue Errors::RecordError => e
103
- errors << e.message
104
- false
105
- end
106
-
107
- # Checks if the record is a valid record. +false+ if most recent +#save+
108
- # call raised errors, or if there are missing keys. +true+ otherwise.
109
- def valid?
110
- errors.empty? && missing_key_values.empty?
77
+ if _invalid_record?(opts)
78
+ false
79
+ else
80
+ _perform_save(opts)
81
+ end
111
82
  end
112
83
 
113
84
  # Deletes the item instance that matches the key values of this item
@@ -122,11 +93,51 @@ module Aws
122
93
  true
123
94
  end
124
95
 
125
- def errors
126
- self.class.instance_variable_get("@errors")
96
+ private
97
+ def _invalid_record?(opts)
98
+ if self.respond_to?(:valid?)
99
+ if !self.valid?
100
+ true
101
+ else
102
+ false
103
+ end
104
+ else
105
+ false
106
+ end
107
+ end
108
+
109
+ def _perform_save(opts)
110
+ force = opts[:force]
111
+ expect_new = expect_new_item?
112
+ if force
113
+ dynamodb_client.put_item(
114
+ table_name: self.class.table_name,
115
+ item: build_item_for_save
116
+ )
117
+ elsif expect_new
118
+ put_opts = {
119
+ table_name: self.class.table_name,
120
+ item: build_item_for_save
121
+ }.merge(prevent_overwrite_expression)
122
+ begin
123
+ dynamodb_client.put_item(put_opts)
124
+ rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e
125
+ raise Errors::ItemAlreadyExists.new(
126
+ "Conditional #put_item call failed, an item with the same key"\
127
+ " already exists in the table. Either load and update this"\
128
+ " item, or include the :force option to clobber the remote"\
129
+ " item."
130
+ )
131
+ end
132
+ else
133
+ dynamodb_client.update_item(
134
+ table_name: self.class.table_name,
135
+ key: key_values,
136
+ attribute_updates: dirty_changes_for_update
137
+ )
138
+ end
127
139
  end
128
140
 
129
- private
130
141
  def build_item_for_save
131
142
  validate_key_values
132
143
  attributes = self.class.attributes
@@ -13,6 +13,6 @@
13
13
 
14
14
  module Aws
15
15
  module Record
16
- VERSION = '1.0.0.pre.6'
16
+ VERSION = '1.0.0.pre.7'
17
17
  end
18
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.6
4
+ version: 1.0.0.pre.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2016-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-resources