module Aws::Record::RecordClassMethods

Public Instance Methods

configure_client(opts = {}) click to toggle source

Configures the Amazon DynamoDB client used by this class and all instances of this class.

Please note that this method is also called internally when you first attempt to perform an operation against the remote end, if you have not already configured a client. As such, please read and understand the documentation in the AWS SDK for Ruby V2 around {docs.aws.amazon.com/sdkforruby/api/index.html#Configuration configuration} to ensure you understand how default configuration behavior works. When in doubt, call this method to ensure your client is configured the way you want it to be configured.

@param [Hash] opts the options you wish to use to create the client.

Note that if you include the option +:client+, all other options
will be ignored. See the documentation for other options in the
{http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#initialize-instance_method AWS SDK for Ruby V2}.

@option opts [Aws::DynamoDB::Client] :client allows you to pass in your

own pre-configured client.
# File lib/aws-record/record.rb, line 168
def configure_client(opts = {})
  provided_client = opts.delete(:client)
  opts[:user_agent_suffix] = _user_agent(opts.delete(:user_agent_suffix))
  client = provided_client || Aws::DynamoDB::Client.new(opts)
  @dynamodb_client = client
end
disable_mutation_tracking() click to toggle source

Turns off mutation tracking for all attributes in the model.

# File lib/aws-record/record.rb, line 187
def disable_mutation_tracking
  @track_mutations = false
end
dynamodb_client() click to toggle source

Gets the {docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html Aws::DynamoDB::Client} instance that this model uses. When called for the first time, if {#configure_client} has not yet been called, will configure a new client for you with default parameters.

@return [Aws::DynamoDB::Client] the Amazon DynamoDB client instance.

# File lib/aws-record/record.rb, line 182
def dynamodb_client
  @dynamodb_client ||= configure_client
end
enable_mutation_tracking() click to toggle source

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.

# File lib/aws-record/record.rb, line 196
def enable_mutation_tracking
  @track_mutations = true
end
model_valid?() click to toggle source
# File lib/aws-record/record.rb, line 210
def model_valid?
  if @keys.hash_key.nil?
    raise Errors::InvalidModel.new("Table models must include a hash key")
  end
end
mutation_tracking_enabled?() click to toggle source

@return [Boolean] true if mutation tracking is enabled at the model level, false otherwise.

# File lib/aws-record/record.rb, line 202
def mutation_tracking_enabled?
  if defined?(@track_mutations)
    @track_mutations
  else
    @track_mutations = true
  end
end
provisioned_throughput() click to toggle source

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

@return [Hash] a hash containing the :read_capacity_units and

+:write_capacity_units+ of your remote table.

@raise [Aws::Record::Errors::TableDoesNotExist] if the table name does

not exist in DynamoDB.
# File lib/aws-record/record.rb, line 121
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) click to toggle source

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

@example

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"
# File lib/aws-record/record.rb, line 110
def set_table_name(name)
  @table_name = name
end
table_exists?() click to toggle source

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

@return [Boolean] true if the table does exist, false if it does not.

# File lib/aws-record/record.rb, line 137
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_name() click to toggle source

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.

@example

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"
# File lib/aws-record/record.rb, line 86
def table_name
  if @table_name
    @table_name
  else
    @table_name = self.name.split("::").join("_")
  end
end

Private Instance Methods

_user_agent(custom) click to toggle source
# File lib/aws-record/record.rb, line 217
def _user_agent(custom)
  if custom
    custom
  else
    " aws-record/#{VERSION}"
  end
end