class Dynamodb::Base

Attributes

data[RW]
errors[R]

Public Class Methods

new(data = {}, new_record = true) click to toggle source
# File lib/dynamodb/base.rb, line 22
def initialize(data = {}, new_record = true)
  self.data = data
  @new_record = new_record
  @errors = []
end

Private Class Methods

not_found() click to toggle source
# File lib/dynamodb/base.rb, line 87
def self.not_found
   { error: "Not Found" }
end

Public Instance Methods

add_error(e) click to toggle source
# File lib/dynamodb/base.rb, line 57
def add_error(e)
  @errors << e
end
data=(val) click to toggle source
# File lib/dynamodb/base.rb, line 28
def data=(val)
  @data = val.deep_stringify_keys
end
generate_timestamps() click to toggle source
# File lib/dynamodb/base.rb, line 61
def generate_timestamps
  self.schedule_time_to_live if self.respond_to? :schedule_time_to_live
  @data["updated_at"] = Time.now.utc.iso8601
  @data["created_at"] = Time.now.utc.iso8601 if new_record?
end
handle_error(e) click to toggle source
# File lib/dynamodb/base.rb, line 53
def handle_error(e)
  @errors << e.message
end
new_record?() click to toggle source
# File lib/dynamodb/base.rb, line 37
def new_record?
  @new_record.nil? ? false : @new_record
end
valid?() click to toggle source
# File lib/dynamodb/base.rb, line 41
def valid?
  # Checks to make sure the data is in the proper format and includes the
  #   hash_key Primary Key, and range_key if has one
  # TODO need to validate data is a hash earlier
  #   @data = 'a string'
  @errors = [] # start fresh
  return true if valid_data_format? && valid_hash_key? && valid_range_key?

  @errors << "Incorrect format of data"
  false # is not valid
end

Private Instance Methods

normalize_hash_key() click to toggle source
# File lib/dynamodb/base.rb, line 69
def normalize_hash_key
  return unless @data[hash_key].is_a? BigDecimal
  @data[hash_key] = @data[hash_key].to_i
end
valid_data_format?() click to toggle source
# File lib/dynamodb/base.rb, line 74
def valid_data_format?
  data.is_a? Hash
end
valid_hash_key?() click to toggle source
# File lib/dynamodb/base.rb, line 78
def valid_hash_key?
  data.key?(hash_key)
end
valid_range_key?() click to toggle source
# File lib/dynamodb/base.rb, line 82
def valid_range_key?
  return true if range_key.nil?
  data.key?(range_key)
end