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
84 85 86 |
# File 'lib/dynamo-record/model.rb', line 84 def composite_key(*args) args.join(COMPOSITE_DELIMITER) end |
#find(opts) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dynamo-record/model.rb', line 25 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
45 46 47 48 49 50 51 52 |
# File 'lib/dynamo-record/model.rb', line 45 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
40 41 42 43 |
# File 'lib/dynamo-record/model.rb', line 40 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
36 37 38 |
# File 'lib/dynamo-record/model.rb', line 36 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
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dynamo-record/model.rb', line 54 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dynamo-record/model.rb', line 68 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
92 93 94 |
# File 'lib/dynamo-record/model.rb', line 92 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
96 97 98 |
# File 'lib/dynamo-record/model.rb', line 96 def find_all_by_model_paginated(model, opts = {}) find_all_by_model(model, pagination_opts(opts)) end |
#pagination_opts(opts = {}) ⇒ Object
100 101 102 |
# File 'lib/dynamo-record/model.rb', line 100 def pagination_opts(opts = {}) { limit: opts[:limit], exclusive_start_key: opts[:exclusive_start_key] } end |
#scan ⇒ Object
20 21 22 23 |
# File 'lib/dynamo-record/model.rb', line 20 def scan raise 'no scanning in production' if Rails.env.production? super end |
#split_composite(string) ⇒ Object
88 89 90 |
# File 'lib/dynamo-record/model.rb', line 88 def split_composite(string) string.split(COMPOSITE_DELIMITER) end |
#table_name ⇒ Object
16 17 18 |
# File 'lib/dynamo-record/model.rb', line 16 def table_name [Rails.configuration.dynamo['prefix'], name.tableize].join('-').tr('/', '.') end |