Module: Dynamo::Record::Model::ClassMethods

Defined in:
lib/dynamo/record/model.rb

Instance Method Summary collapse

Instance Method Details

#composite_key(*args) ⇒ Object



101
102
103
# File 'lib/dynamo/record/model.rb', line 101

def composite_key(*args)
  args.join(COMPOSITE_DELIMITER)
end

#find(opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamo/record/model.rb', line 27

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



47
48
49
50
51
52
53
54
# File 'lib/dynamo/record/model.rb', line 47

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



42
43
44
45
# File 'lib/dynamo/record/model.rb', line 42

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



38
39
40
# File 'lib/dynamo/record/model.rb', line 38

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dynamo/record/model.rb', line 70

def find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts = {}, index_name = nil)
  query_options = {
    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
  }
  query_options[:index_name] = index_name if index_name
  query_options.merge!(opts)
  query(query_options)
end

#find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil, scan_index_forward: true, limit: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dynamo/record/model.rb', line 84

def find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil,
  scan_index_forward: true, limit: nil)
  range_expression = range_config[:expression] || "#{range_config[:name]} = :rkv"
  query_options = {
    select: 'ALL_ATTRIBUTES',
    key_condition_expression: "#{hash_config[:name]} = :hkv AND #{range_expression}",
    expression_attribute_values: {
      ':hkv': hash_config[:value],
      ':rkv': range_config[:value]
    },
    scan_index_forward: scan_index_forward,
    limit: limit
  }
  query_options[:index_name] = index_name if index_name
  query(query_options)
end

#find_all_by_lsi_hash_and_range_keys(lsi_name, hash_key_value, range_key_value) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/dynamo/record/model.rb', line 61

def find_all_by_lsi_hash_and_range_keys(lsi_name, hash_key_value, range_key_value)
  lsi_config = local_secondary_indexes[lsi_name.to_sym]
  find_all_by_index_hash_and_range_keys(
    hash_config: { name: lsi_config[:hash_key], value: hash_key_value },
    range_config: { name: lsi_config[:range_key], value: range_key_value },
    index_name: lsi_name.to_s
  )
end

#find_all_by_lsi_hash_key(lsi_name, hash_key_value, opts = {}) ⇒ Object



56
57
58
59
# File 'lib/dynamo/record/model.rb', line 56

def find_all_by_lsi_hash_key(lsi_name, hash_key_value, opts = {})
  hash_key_name = local_secondary_indexes[lsi_name.to_sym][:hash_key]
  find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts, lsi_name.to_s)
end

#max_retriesObject



109
110
111
# File 'lib/dynamo/record/model.rb', line 109

def max_retries
  DEFAULT_MAX_RETRIES
end

#scanObject



22
23
24
25
# File 'lib/dynamo/record/model.rb', line 22

def scan
  raise 'no scanning in production' if Rails.env.production?
  super
end

#split_composite(string) ⇒ Object



105
106
107
# File 'lib/dynamo/record/model.rb', line 105

def split_composite(string)
  string.split(COMPOSITE_DELIMITER)
end

#table_nameObject



18
19
20
# File 'lib/dynamo/record/model.rb', line 18

def table_name
  [Rails.configuration.dynamo['prefix'], name.tableize].join('-').tr('/', '.')
end