Module: DynamoRecord::Model::ClassMethods
- Defined in:
- lib/dynamo-record/model.rb
Instance Method Summary collapse
- #composite_key(*args) ⇒ Object
- #find(opts) ⇒ Object
- #find_all_by_gsi_hash_and_range_keys(gsi_name, hash_key_value, range_key_value) ⇒ Object
- #find_all_by_gsi_hash_key(gsi_name, hash_key_value, opts = {}) ⇒ Object
- #find_all_by_hash_key(hash_key_value, opts = {}) ⇒ Object
- #find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts = {}, index_name = nil) ⇒ Object
- #find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil, scan_index_forward: true, limit: nil) ⇒ Object
- #find_all_by_model(model, opts = {}) ⇒ Object
- #find_all_by_model_paginated(model, opts = {}) ⇒ Object
- #pagination_opts(opts = {}) ⇒ Object
- #scan ⇒ Object
- #split_composite(string) ⇒ Object
- #table_name ⇒ Object
Instance Method Details
#composite_key(*args) ⇒ Object
83 84 85 |
# File 'lib/dynamo-record/model.rb', line 83 def composite_key(*args) args.join(COMPOSITE_DELIMITER) end |
#find(opts) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dynamo-record/model.rb', line 24 def find(opts) super(opts).tap do |record| unless record name = self.name.demodulize conditions = opts.map { |k, v| "#{k}=#{v}" }.join(', ') error = "Couldn't find #{name} with #{conditions}" raise Aws::Record::Errors::NotFound, error end end end |
#find_all_by_gsi_hash_and_range_keys(gsi_name, hash_key_value, range_key_value) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/dynamo-record/model.rb', line 44 def find_all_by_gsi_hash_and_range_keys(gsi_name, hash_key_value, range_key_value) gsi_config = global_secondary_indexes[gsi_name.to_sym] find_all_by_index_hash_and_range_keys( hash_config: { name: gsi_config[:hash_key], value: hash_key_value }, range_config: { name: gsi_config[:range_key], value: range_key_value }, index_name: gsi_name.to_s ) end |
#find_all_by_gsi_hash_key(gsi_name, hash_key_value, opts = {}) ⇒ Object
39 40 41 42 |
# File 'lib/dynamo-record/model.rb', line 39 def find_all_by_gsi_hash_key(gsi_name, hash_key_value, opts = {}) hash_key_name = global_secondary_indexes[gsi_name.to_sym][:hash_key] find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts, gsi_name.to_s) end |
#find_all_by_hash_key(hash_key_value, opts = {}) ⇒ Object
35 36 37 |
# File 'lib/dynamo-record/model.rb', line 35 def find_all_by_hash_key(hash_key_value, opts = {}) find_all_by_index_and_hash_key(hash_key, hash_key_value, opts) end |
#find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts = {}, index_name = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dynamo-record/model.rb', line 53 def find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts = {}, index_name = nil) = { select: 'ALL_ATTRIBUTES', key_condition_expression: "#{hash_key_name} = :hash_key_value", expression_attribute_values: { ':hash_key_value': hash_key_value }, scan_index_forward: true } [:index_name] = index_name if index_name .merge!(opts) query() end |
#find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil, scan_index_forward: true, limit: nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dynamo-record/model.rb', line 67 def find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil, scan_index_forward: true, limit: nil) = { select: 'ALL_ATTRIBUTES', key_condition_expression: "#{hash_config[:name]} = :hkv AND #{range_config[:name]} = :rkv", expression_attribute_values: { ':hkv': hash_config[:value], ':rkv': range_config[:value] }, scan_index_forward: scan_index_forward, limit: limit } [:index_name] = index_name if index_name query() end |
#find_all_by_model(model, opts = {}) ⇒ Object
91 92 93 |
# File 'lib/dynamo-record/model.rb', line 91 def find_all_by_model(model, opts = {}) find_all_by_hash_key(model.dynamo_key, opts) end |
#find_all_by_model_paginated(model, opts = {}) ⇒ Object
95 96 97 |
# File 'lib/dynamo-record/model.rb', line 95 def find_all_by_model_paginated(model, opts = {}) find_all_by_model(model, pagination_opts(opts)) end |
#pagination_opts(opts = {}) ⇒ Object
99 100 101 |
# File 'lib/dynamo-record/model.rb', line 99 def pagination_opts(opts = {}) { limit: opts[:limit], exclusive_start_key: opts[:exclusive_start_key] } end |
#scan ⇒ Object
19 20 21 22 |
# File 'lib/dynamo-record/model.rb', line 19 def scan raise 'no scanning in production' if Rails.env.production? super end |
#split_composite(string) ⇒ Object
87 88 89 |
# File 'lib/dynamo-record/model.rb', line 87 def split_composite(string) string.split(COMPOSITE_DELIMITER) end |
#table_name ⇒ Object
15 16 17 |
# File 'lib/dynamo-record/model.rb', line 15 def table_name [Rails.configuration.dynamo['prefix'], name.tableize].join('-').tr('/', '.') end |