Module: Aws::Record::RecordClassMethods

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

Instance Method Summary collapse

Instance Method Details

#configure_client(opts = {}) ⇒ Object

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 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.

Parameters:

  • opts (Hash) (defaults to: {})

    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 AWS SDK for Ruby V2.

Options Hash (opts):

  • :client (Aws::DynamoDB::Client)

    allows you to pass in your own pre-configured client.



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

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_trackingObject

Turns off mutation tracking for all attributes in the model.



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

def disable_mutation_tracking
  @track_mutations = false
end

#dynamodb_clientAws::DynamoDB::Client

Gets the 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.

Returns:

  • (Aws::DynamoDB::Client)

    the Amazon DynamoDB client instance.



183
184
185
# File 'lib/aws-record/record.rb', line 183

def dynamodb_client
  @dynamodb_client ||= configure_client
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.



197
198
199
# File 'lib/aws-record/record.rb', line 197

def enable_mutation_tracking
  @track_mutations = true
end

#model_valid?Boolean

Returns:

  • (Boolean)


207
208
209
210
211
# File 'lib/aws-record/record.rb', line 207

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



203
204
205
# File 'lib/aws-record/record.rb', line 203

def mutation_tracking_enabled?
  @track_mutations == false ? false : true
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