ocean-dynamo 0.1.13 → 0.1.14

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: 74c7e10d51de039a64fc18d60408e3a1a0c9b7f4
4
- data.tar.gz: e71adf734d5c6cbde2b2ade2b0b251514c2a9abc
3
+ metadata.gz: 5e10e3f4a197a97537b6afa0eee14b831c6fbacf
4
+ data.tar.gz: af58bdb937981f25306d6db7c7b43da97cfd8217
5
5
  SHA512:
6
- metadata.gz: b50cc6aeac65b2a4c1f51e390e0f6d443559507bffcee8f9a13aee347e0e8b06e4aa49d10af2afec8787a6ada8eb82f6902089b68835d07a380444813b8ad0cb
7
- data.tar.gz: a12992af3f67eb5d237096e9877506b9ba0b01d101b4c7e2a8e37ad51c8c80c8f07812fc19cc7c3cc61bb70e69c398041a8169a9b8db90f4032e853a4278f88a
6
+ metadata.gz: 87f23170e015fb2592d98e43ed264f06d97e1c147c01be3d006601b7159871be36b4a98e591e2baaa5693964fd2040c1441de873d897c60cb1cb621545d14c17
7
+ data.tar.gz: 5649dbf1e4513a19af1387566531fa64f10ce04c70910d0e0970c3d19e0d3b0cfe26144d0bac103d8373c1f663c91c01afd3d2f78fd9087a5f9dedadda05dc58
data/README.rdoc CHANGED
@@ -8,6 +8,11 @@ OceanDynamo requires Ruby 2.0 and Ruby on Rails 4.0.0 or later.
8
8
  {<img src="https://badge.fury.io/rb/ocean-dynamo.png" alt="Gem Version" />}[http://badge.fury.io/rb/ocean-dynamo]
9
9
 
10
10
 
11
+ ==== Installation
12
+
13
+ gem install ocean-dynamo
14
+
15
+
11
16
  ==== Features
12
17
 
13
18
  As one important use case for OceanDynamo is to facilitate the conversion of SQL based
@@ -31,6 +36,48 @@ which means it will scale without limits. (NB: this is a pre-release which as ye
31
36
  implement relations, but they are underway.)
32
37
 
33
38
 
39
+ ==== Example
40
+
41
+ The following example shows the syntax.
42
+
43
+ class AsyncJob < OceanDynamo::Base
44
+
45
+ dynamo_schema do
46
+ attribute :credentials, :string
47
+ attribute :token
48
+ attribute :steps, :serialized, default: []
49
+ attribute :max_seconds_in_queue, :integer, default: 1.day
50
+ attribute :default_poison_limit, :integer, default: 5
51
+ attribute :default_step_time, :integer, default: 30
52
+ attribute :started_at, :datetime
53
+ attribute :last_completed_step, :integer
54
+ attribute :finished_at, :datetime
55
+ attribute :destroy_at, :datetime
56
+ attribute :created_by
57
+ attribute :updated_by
58
+ attribute :succeeded, :boolean, default: false
59
+ attribute :failed, :boolean, default: false
60
+ attribute :poison, :boolean, default: false
61
+ end
62
+
63
+ end
64
+
65
+ Each attribute has a name, a type (:string, :integer, :float, :datetime, :boolean, or
66
+ :serialized) where :string is the default. Each attribute also optionally has a default
67
+ value, which can be a Proc.
68
+
69
+ The :string, :integer, :float and :datetime types can also store sets of their type.
70
+ Sets are represented as arrays and may not contain duplicates and may not be empty.
71
+
72
+ All attributes except the :string type can take the value nil. Storing nil for a string
73
+ value will return the empty string, "".
74
+
75
+ Also, dynamo_schema takes args and many options. Detailed documentation is forthcoming.
76
+
77
+ At the moment, OceanDynamo is fully usable as an ActiveModel and can be used by Rails
78
+ controllers. Relations are not yet implemented.
79
+
80
+
34
81
  === Documentation
35
82
 
36
83
  * Ocean-dynamo gem API: http://rdoc.info/github/OceanDev/ocean-dynamo/frames
@@ -93,10 +140,6 @@ The Rails console is available from the built-in dummy application:
93
140
  cd spec/dummy
94
141
  rails console
95
142
 
96
- You may need to initialise the table connection (for each table):
97
-
98
- CloudModel.establish_db_connection
99
-
100
143
  This will, amongst other things, also create the CloudModel table if it doesn't already
101
144
  exist. On Amazon, this will take a little while. With +fake_dynamo+, it's practically
102
145
  instant.
@@ -30,6 +30,26 @@ module OceanDynamo
30
30
  end
31
31
 
32
32
 
33
+ def [](attribute)
34
+ read_attribute attribute
35
+ end
36
+
37
+
38
+ def []=(attribute, value)
39
+ write_attribute attribute, value
40
+ end
41
+
42
+
43
+ def id
44
+ read_attribute(table_hash_key)
45
+ end
46
+
47
+
48
+ def id=(value)
49
+ write_attribute(table_hash_key, value)
50
+ end
51
+
52
+
33
53
  def read_attribute_for_validation(key)
34
54
  @attributes[key.to_s]
35
55
  end
@@ -55,6 +75,28 @@ module OceanDynamo
55
75
  end
56
76
 
57
77
 
78
+ def to_key
79
+ return nil unless persisted?
80
+ key = respond_to?(:id) && id
81
+ return nil unless key
82
+ table_range_key ? [key, read_attribute(table_range_key)] : [key]
83
+ end
84
+
85
+
86
+ def assign_attributes(values)
87
+ return if values.blank?
88
+ values = values.stringify_keys
89
+ # if values.respond_to?(:permitted?)
90
+ # unless values.permitted?
91
+ # raise ActiveModel::ForbiddenAttributesError
92
+ # end
93
+ # end
94
+ values.each do |k, v|
95
+ _assign_attribute(k, v)
96
+ end
97
+ end
98
+
99
+
58
100
  def type_cast_attribute_for_write(name, value, metadata=fields[name],
59
101
  type: metadata[:type])
60
102
  case type
@@ -161,52 +203,9 @@ module OceanDynamo
161
203
  end
162
204
 
163
205
 
164
- def [](attribute)
165
- read_attribute attribute
166
- end
167
-
168
-
169
- def []=(attribute, value)
170
- write_attribute attribute, value
171
- end
172
-
173
-
174
- def id
175
- read_attribute(table_hash_key)
176
- end
177
-
178
-
179
- def id=(value)
180
- write_attribute(table_hash_key, value)
181
- end
182
-
183
-
184
- def to_key
185
- return nil unless persisted?
186
- key = respond_to?(:id) && id
187
- return nil unless key
188
- table_range_key ? [key, read_attribute(table_range_key)] : [key]
189
- end
190
-
191
-
192
- def assign_attributes(values)
193
- return if values.blank?
194
- values = values.stringify_keys
195
- # if values.respond_to?(:permitted?)
196
- # unless values.permitted?
197
- # raise ActiveModel::ForbiddenAttributesError
198
- # end
199
- # end
200
- values.each do |k, v|
201
- #send("#{k}=", v)
202
- _assign_attribute(k, v)
203
- end
204
- end
205
-
206
-
207
206
  private
208
207
 
209
- def _assign_attribute(k, v)
208
+ def _assign_attribute(k, v) # :nodoc:
210
209
  public_send("#{k}=", v)
211
210
  rescue ActiveModel::NoMethodError
212
211
  if respond_to?("#{k}=")
@@ -219,7 +218,7 @@ module OceanDynamo
219
218
 
220
219
  protected
221
220
 
222
- def evaluate_default(default, type)
221
+ def evaluate_default(default, type) # :nodoc:
223
222
  return default.call if default.is_a?(Proc)
224
223
  return "" if default == nil && type == :string
225
224
  return default.clone if default.is_a?(Array) || default.is_a?(String) # Instances need their own copies
@@ -75,7 +75,8 @@ module OceanDynamo
75
75
 
76
76
  def save!(options={})
77
77
  if perform_validations(options)
78
- create_or_update || raise(RecordNotSaved)
78
+ options[:validate] = false
79
+ create_or_update(options) || raise(RecordNotSaved)
79
80
  else
80
81
  raise RecordInvalid.new(self)
81
82
  end
@@ -94,14 +95,14 @@ module OceanDynamo
94
95
  end
95
96
 
96
97
 
97
- def create_or_update
98
- result = new_record? ? create : update
98
+ def create_or_update(options={})
99
+ result = new_record? ? create(options) : update(options)
99
100
  result != false
100
101
  end
101
102
 
102
103
 
103
- def create
104
- return false unless valid?(:create)
104
+ def create(options={})
105
+ return false if options[:validate] != false && !valid?(:create)
105
106
  run_callbacks :commit do
106
107
  run_callbacks :save do
107
108
  run_callbacks :create do
@@ -118,8 +119,8 @@ module OceanDynamo
118
119
  end
119
120
 
120
121
 
121
- def update
122
- return false unless valid?(:update)
122
+ def update(options={})
123
+ return false if options[:validate] != false && !valid?(:update)
123
124
  run_callbacks :commit do
124
125
  run_callbacks :save do
125
126
  run_callbacks :update do
@@ -189,13 +190,13 @@ module OceanDynamo
189
190
  end
190
191
 
191
192
 
192
- def dynamo_persist
193
+ def dynamo_persist # :nodoc:
193
194
  @dynamo_item = dynamo_items.put(serialized_attributes)
194
195
  @new_record = false
195
196
  end
196
197
 
197
198
 
198
- def post_instantiate(item, consistent)
199
+ def post_instantiate(item, consistent) # :nodoc:
199
200
  @dynamo_item = item
200
201
  @new_record = false
201
202
  assign_attributes(deserialized_attributes(
@@ -1,6 +1,38 @@
1
1
  module OceanDynamo
2
2
  class Base
3
3
 
4
+
5
+ def self.dynamo_schema(table_hash_key=:uuid, table_range_key=nil,
6
+ table_name: compute_table_name,
7
+ table_name_prefix: nil,
8
+ table_name_suffix: nil,
9
+ read_capacity_units: 10,
10
+ write_capacity_units: 5,
11
+ connect: true,
12
+ &block)
13
+ # Set class vars
14
+ self.dynamo_client = nil
15
+ self.dynamo_table = nil
16
+ self.dynamo_items = nil
17
+ self.table_hash_key = table_hash_key
18
+ self.table_range_key = table_range_key
19
+ self.table_name = table_name
20
+ self.table_name_prefix = table_name_prefix
21
+ self.table_name_suffix = table_name_suffix
22
+ self.table_read_capacity_units = read_capacity_units
23
+ self.table_write_capacity_units = write_capacity_units
24
+ # Init
25
+ self.fields = HashWithIndifferentAccess.new
26
+ attribute table_hash_key, :string, default: ''
27
+ DEFAULT_ATTRIBUTES.each { |name, type, **pairs| attribute name, type, **pairs }
28
+ block.call
29
+ # Connect to AWS
30
+ establish_db_connection if connect
31
+ # Finally return the full table name
32
+ table_full_name
33
+ end
34
+
35
+
4
36
  def self.set_table_name(name)
5
37
  self.table_name = name
6
38
  true
@@ -29,24 +61,6 @@ module OceanDynamo
29
61
  end
30
62
 
31
63
 
32
- #
33
- # This is where the class is initialized
34
- #
35
- def self.primary_key(hash_key, range_key=nil)
36
- self.dynamo_client = nil
37
- self.dynamo_table = nil
38
- self.dynamo_items = nil
39
- self.table_name = compute_table_name
40
- self.table_name_prefix = nil
41
- self.table_name_suffix = nil
42
- self.fields = HashWithIndifferentAccess.new
43
- self.table_hash_key = hash_key
44
- self.table_range_key = range_key
45
- DEFAULT_ATTRIBUTES.each { |k, name, **pairs| attribute k, name, **pairs }
46
- nil
47
- end
48
-
49
-
50
64
  def self.read_capacity_units(units)
51
65
  self.table_read_capacity_units = units
52
66
  end
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson