Method: PEROBS::DynamoDB#initialize

Defined in:
lib/perobs/DynamoDB.rb

#initialize(db_name, options = {}) ⇒ DynamoDB

Create a new DynamoDB object.

Parameters:

  • db_name (String)

    name of the DB directory

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

    options to customize the behavior. Currently only the following options are supported: :serializer : Can be :json and :yaml :aws_id : AWS credentials ID :aws_key : AWS credentials key :aws_region : AWS region to host the data



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/perobs/DynamoDB.rb', line 46

def initialize(db_name, options = {})
  # :marshal serialization results in a binary format that cannot easily
  # be stored in DynamoDB. We fall back to :yaml.
  if options[:serializer] == :marshal
    options[:serializer] = :yaml
  end

  super(options[:serializer] || :json)

  if options.include?(:aws_id) && options.include?(:aws_key)
    Aws.config[:credentials] = Aws::Credentials.new(options[:aws_id],
                                                    options[:aws_key])
  end
  if options.include?(:aws_region)
    Aws.config[:region] = options[:aws_region]
  end

  @dynamodb = Aws::DynamoDB::Client.new
  @table_name = db_name
  ensure_table_exists(@table_name)

  # Read the existing DB config.
  @config = get_hash('config')
  check_option('serializer')
  put_hash('config', @config)
end