Skip to content

Commit 147efaf

Browse files
committed
add key_type option to table method
1 parent a74b629 commit 147efaf

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/dynamoid/fields.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def range(name, type = :string, options = {})
200200
# @param options [Hash] options to override default table settings
201201
# @option options [Symbol] :name name of a table
202202
# @option options [Symbol] :key name of a hash key attribute
203+
# @option options [Symbol] :key_type type of a hash key attribute
203204
# @option options [Symbol] :inheritance_field name of an attribute used for STI
204205
# @option options [Symbol] :capacity_mode table billing mode - either +provisioned+ or +on_demand+
205206
# @option options [Integer] :write_capacity table write capacity units
@@ -210,11 +211,11 @@ def range(name, type = :string, options = {})
210211
# @since 0.4.0
211212
def table(options)
212213
self.options = options
213-
214214
# a default 'id' column is created when Dynamoid::Document is included
215215
unless attributes.key? hash_key
216216
remove_field :id
217-
field(hash_key)
217+
key_type = options[:key_type] || :string
218+
field(hash_key, key_type)
218219
end
219220

220221
# The created_at/updated_at fields are declared in the `included` callback first.

spec/dynamoid/fields_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,16 @@ def name=(str)
448448
expect(obj.name_changed?).to eq false
449449
end
450450
end
451+
452+
describe '.table' do
453+
context 'when specify key option and key_type' do
454+
it 'hash_key attribute is defined by key_type type' do
455+
klass = new_class do
456+
table key: :hash_key, key_type: :integer
457+
end
458+
459+
expect(klass.attributes[:hash_key][:type]).to eq(:integer)
460+
end
461+
end
462+
end
451463
end

0 commit comments

Comments
 (0)