Module: DynamodbModel::DbConfig::ClassMethods

Defined in:
lib/dynamodb_model/db_config.rb

Constant Summary collapse

@@db =
nil

Instance Method Summary collapse

Instance Method Details

#dbObject



22
23
24
25
26
27
28
29
30
# File 'lib/dynamodb_model/db_config.rb', line 22

def db
  return @@db if @@db

  config = db_config
  endpoint = ENV['DYNAMODB_ENDPOINT'] || config['endpoint']
  Aws.config.update(endpoint: endpoint) if endpoint

  @@db ||= Aws::DynamoDB::Client.new
end

#db=(db) ⇒ Object

useful for specs



33
34
35
# File 'lib/dynamodb_model/db_config.rb', line 33

def db=(db)
  @@db = db
end

#db_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dynamodb_model/db_config.rb', line 37

def db_config
  return @db_config if @db_config

  if defined?(Jets)
    config_path = "#{Jets.root}config/dynamodb.yml"
    env = Jets.env
  else
    config_path = ENV['DYNAMODB_MODEL_CONFIG'] || "./config/dynamodb.yml"
    env = ENV['DYNAMODB_MODEL_ENV'] || "development"
  end

  config = YAML.load(erb_result(config_path))
  @db_config ||= config[env] || {}
end

#erb_result(path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dynamodb_model/db_config.rb', line 72

def erb_result(path)
  template = IO.read(path)
  begin
    ERB.new(template, nil, "-").result(binding)
  rescue Exception => e
    puts e
    puts e.backtrace if ENV['DEBUG']

    # how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051
    # syntax errors have the (erb):xxx info in e.message
    # undefined variables have (erb):xxx info in e.backtrac
    error_info = e.message.split("\n").grep(/\(erb\)/)[0]
    error_info ||= e.backtrace.grep(/\(erb\)/)[0]
    raise unless error_info # unable to find the (erb):xxx: error line
    line = error_info.split(':')[1].to_i
    puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '').colorize(:green)}"

    template_lines = template.split("\n")
    context = 5 # lines of context
    top, bottom = [line-context-1, 0].max, line+context-1
    spacing = template_lines.size.to_s.size
    template_lines[top..bottom].each_with_index do |line_content, index|
      line_number = top+index+1
      if line_number == line
        printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
      else
        printf("%#{spacing}d %s\n", line_number, line_content)
      end
    end
    exit 1 unless ENV['TEST']
  end
end

#get_table_namespaceObject



61
62
63
64
65
66
# File 'lib/dynamodb_model/db_config.rb', line 61

def get_table_namespace
  return @table_namespace if defined?(@table_namespace)

  config = db_config
  @table_namespace = config['table_namespace']
end

#set_table_namespace(value) ⇒ Object



68
69
70
# File 'lib/dynamodb_model/db_config.rb', line 68

def set_table_namespace(value)
  @table_namespace = value
end

#table_namespace(*args) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/dynamodb_model/db_config.rb', line 52

def table_namespace(*args)
  case args.size
  when 0
    get_table_namespace
  when 1
    set_table_namespace(args[0])
  end
end