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.



152
153
154
# File 'lib/aws-record/record.rb', line 152

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.



161
162
163
# File 'lib/aws-record/record.rb', line 161

def enable_mutation_tracking
  @track_mutations = true
end

#model_valid?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
# File 'lib/aws-record/record.rb', line 175

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



167
168
169
170
171
172
173
# File 'lib/aws-record/record.rb', line 167

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:



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/aws-record/record.rb', line 122

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.

Examples:

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

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

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


111
112
113
# File 'lib/aws-record/record.rb', line 111

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.



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aws-record/record.rb', line 138

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.

Examples:

class MyTable
  include Aws::Record
end

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

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


87
88
89
90
91
92
93
# File 'lib/aws-record/record.rb', line 87

def table_name
  if @table_name
    @table_name
  else
    @table_name = self.name.split("::").join("_")
  end
end