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.



233
234
235
# File 'lib/aws-record/record.rb', line 233

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.



245
246
247
# File 'lib/aws-record/record.rb', line 245

def enable_mutation_tracking
  @track_mutations = true
end

#model_valid?Boolean

Returns:

  • (Boolean)


259
260
261
262
263
# File 'lib/aws-record/record.rb', line 259

def model_valid?
  if @keys.hash_key.nil?
    raise Errors::InvalidModel.new("Table models must include a hash key")
  end
end

#mutation_tracking_enabled?Boolean

level, false otherwise.

Returns:

  • (Boolean)

    true if mutation tracking is enabled at the model



251
252
253
254
255
256
257
# File 'lib/aws-record/record.rb', line 251

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:



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/aws-record/record.rb', line 200

def provisioned_throughput
  begin
    resp = dynamodb_client.describe_table(table_name: table_name)
    throughput = resp.table.provisioned_throughput
    return {
      read_capacity_units: throughput.read_capacity_units,
      write_capacity_units: throughput.write_capacity_units
    }
  rescue DynamoDB::Errors::ResourceNotFoundException
    raise Record::Errors::TableDoesNotExist
  end
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"


189
190
191
# File 'lib/aws-record/record.rb', line 189

def set_table_name(name)
  @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.



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/aws-record/record.rb', line 216

def table_exists?
  begin
    resp = dynamodb_client.describe_table(table_name: table_name)
    if resp.table.table_status == "ACTIVE"
      true
    else
      false
    end
  rescue DynamoDB::Errors::ResourceNotFoundException
    false
  end
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"


129
130
131
132
133
134
135
136
137
138
# File 'lib/aws-record/record.rb', line 129

def table_name
  @table_name ||= begin
    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
  end
end