Module: Aws::Record::RecordClassMethods

Included in:
Aws::Record
Defined in:
lib/aws-record/record.rb

Instance Method Summary collapse

Instance Method Details

#disable_mutation_trackingObject

Turns off mutation tracking for all attributes in the model.

Note: disable_mutation_tracking is inherited from a parent model when it is explicitly specified in the parent.



215
216
217
# File 'lib/aws-record/record.rb', line 215

def disable_mutation_tracking
  @track_mutations = false
end

#enable_mutation_trackingObject

Turns on mutation tracking for all attributes in the model. Note that mutation tracking is on by default, so you generally would not need to call this. It is provided in case there is a need to dynamically turn this feature on and off, though that would be generally discouraged and could cause inaccurate mutation tracking at runtime.

Note: enable_mutation_tracking is inherited from a parent model when it is explicitly specified in the parent.



227
228
229
# File 'lib/aws-record/record.rb', line 227

def enable_mutation_tracking
  @track_mutations = true
end

#model_valid?Boolean

Returns:

  • (Boolean)

Raises:



241
242
243
# File 'lib/aws-record/record.rb', line 241

def model_valid?
  raise Errors::InvalidModel, 'Table models must include a hash key' if @keys.hash_key.nil?
end

#mutation_tracking_enabled?Boolean

level, false otherwise.

Returns:

  • (Boolean)

    true if mutation tracking is enabled at the model



233
234
235
236
237
238
239
# File 'lib/aws-record/record.rb', line 233

def mutation_tracking_enabled?
  if defined?(@track_mutations)
    @track_mutations
  else
    @track_mutations = true
  end
end

#provisioned_throughputHash

Fetches the table’s provisioned throughput from the associated Amazon DynamoDB table.

Returns:

  • (Hash)

    a hash containing the :read_capacity_units and :write_capacity_units of your remote table.

Raises:



190
191
192
193
194
195
196
197
198
199
# File 'lib/aws-record/record.rb', line 190

def provisioned_throughput
  resp = dynamodb_client.describe_table(table_name: table_name)
  throughput = resp.table.provisioned_throughput
  {
    read_capacity_units: throughput.read_capacity_units,
    write_capacity_units: throughput.write_capacity_units
  }
rescue DynamoDB::Errors::ResourceNotFoundException
  raise Record::Errors::TableDoesNotExist
end

#set_table_name(name) ⇒ Object

Allows you to set a custom Amazon DynamoDB table name for this model class.

Inheritance Support

table_name is inherited from a parent model when it is explicitly specified in the parent.

The parent model will need to have set_table_name defined in their model for the child model to inherit the table_name. If no set_table_name is defined, the parent and child models will have separate table names based on their class name.

If both parent and child models have defined set_table_name in their model, the child model will override the table_name with theirs.

Examples:

Setting custom table name for model class

class MyTable
  include Aws::Record
  set_table_name "prod_MyTable"
end

class MyOtherTable
  include Aws::Record
  set_table_name "test_MyTable"
end

MyTable.table_name      # => "prod_MyTable"
MyOtherTable.table_name # => "test_MyTable"

Child model inherits table name from Parent model

class Animal
  include Aws::Record
  set_table_name "AnimalTable"
end

class Dog < Animal
  include Aws::Record
end

Dog.table_name      # => "AnimalTable"

Child model overrides table name from Parent model

class Animal
  include Aws::Record
  set_table_name "AnimalTable"
end

class Dog < Animal
  include Aws::Record
  set_table_name "DogTable"
end

Dog.table_name      # => "DogTable"


179
180
181
# File 'lib/aws-record/record.rb', line 179

def set_table_name(name) # rubocop:disable Naming/AccessorMethodName
  @table_name = name
end

#table_exists?Boolean

Checks if the model’s table name exists in Amazon DynamoDB.

Returns:

  • (Boolean)

    true if the table does exist, false if it does not.



204
205
206
207
208
209
# File 'lib/aws-record/record.rb', line 204

def table_exists?
  resp = dynamodb_client.describe_table(table_name: table_name)
  resp.table.table_status == 'ACTIVE'
rescue DynamoDB::Errors::ResourceNotFoundException
  false
end

#table_nameObject

Returns the Amazon DynamoDB table name for this model class.

By default, this will simply be the name of the class. However, you can also define a custom table name at the class level to be anything that you want.

Note: table_name is inherited from a parent model when #set_table_name is explicitly specified in the parent.

Examples:

class MyTable
  include Aws::Record
end

class MyOtherTable
  include Aws::Record
  set_table_name "test_MyTable"
end

MyTable.table_name      # => "MyTable"
MyOtherTable.table_name # => "test_MyTable"


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aws-record/record.rb', line 118

def table_name
  # rubocop:disable Style/RedundantSelf
  @table_name ||= if Aws::Record.extends_record?(self) &&
                     default_table_name(self.superclass) != self.superclass.table_name
                    self.superclass.instance_variable_get('@table_name')
                  else
                    default_table_name(self)
                  end

  # rubocop:enable Style/RedundantSelf
end