module DynaModel::Document

Constants

GUID_DELIMITER_PRECEDENCE

These delimiters are also reserved characters and should not be used in hash or range keys

MAX_ITEM_SIZE

Public Instance Methods

[](attribute_name) click to toggle source
Calls superclass method
# File lib/dyna_model/document.rb, line 27
def [] attribute_name
  # Warn if using attributes that were not part of the :select (common with GSI/LSI projections)
  #   we do not want to give the impression they are nil
  if (selected_attrs = self.instance_variable_get("@_selected_attributes"))
    raise "Attribute '#{attribute_name}' was not part of the select '#{self.instance_variable_get("@_select")}' (available attributes: #{selected_attrs})" unless selected_attrs.include?(attribute_name)
  end
  super
end
all_attributes_loaded?() click to toggle source
# File lib/dyna_model/document.rb, line 144
def all_attributes_loaded?
  self.instance_variable_get("@_select").nil? && self.instance_variable_get("@_select") == :all
end
attributes() click to toggle source
# File lib/dyna_model/document.rb, line 40
def attributes
  attributes = AWS::Core::IndifferentHash.new
  (self.instance_variable_get("@_selected_attributes") || self.class.attributes.keys).inject(attributes) do |hash,attr_name|
    hash.merge(attr_name => __send__(attr_name))
  end
end
create(options={}) click to toggle source
# File lib/dyna_model/document.rb, line 89
def create(options={})
  # Not implemented
  #populate_id
  touch_timestamps('created_at', 'updated_at')
  # Not implemented
  #increment_optimistic_lock_value
  @_persisted = true
  create_storage(options)
end
delete(options={}) click to toggle source
# File lib/dyna_model/document.rb, line 70
def delete(options={})
  if persisted?
    if deleted?
      raise 'unable to delete, this object has already been deleted'
    else
      resp = delete_storage(options)
      @_deleted = true
      resp
    end
  else
    raise 'unable to delete, this object has not been saved yet'
  end
end
dynamo_db_guid() click to toggle source
# File lib/dyna_model/document.rb, line 132
def dynamo_db_guid
  _guid = [self.dynamo_db_item_key_values[:hash_value]]
  _guid << self.dynamo_db_item_key_values[:range_value] if self.dynamo_db_item_key_values[:range_value]
  _guid.join(self.class.guid_delimiter)
end
dynamo_db_item_key_values() click to toggle source
# File lib/dyna_model/document.rb, line 138
def dynamo_db_item_key_values
  key_values = { hash_value: self[self.class.hash_key[:attribute_name]] }
  key_values.merge!(range_value: self[self.class.range_key[:attribute_name]]) if self.class.range_key
  key_values
end
id() click to toggle source
# File lib/dyna_model/document.rb, line 124
def id
  self.dynamo_db_guid
end
load_attributes!() click to toggle source

When only partial attributes were selected (via GSI or projected attributes on an index)

# File lib/dyna_model/document.rb, line 149
def load_attributes!
  raise "All attributes already loaded!" if self.instance_variable_get("@_select") == :all
  options = { shard_name: self.shard }
  if self.class.range_key
    obj = self.class.read(dynamo_db_item_key_values[:hash_value], dynamo_db_item_key_values[:range_value], options)
  else
    obj = self.class.read(dynamo_db_item_key_values[:hash_value], options)
  end
  raise "Could not find object" unless obj
  self.instance_variable_set("@_select", :all)
  self.remove_instance_variable("@_selected_attributes")
  self.instance_variable_set("@_data", obj.instance_variable_get("@_data"))
  self
end
save(opts = {}) click to toggle source
# File lib/dyna_model/document.rb, line 51
def save opts = {}
  if valid?(opts)
    write_response = persisted? ? update(opts) : create(opts)
    clear_changes!
    if opts[:return_values] && opts[:return_values] != :none
      # return the ReturnValues if the user wants them
      write_response
    else
      true
    end
  else
    false
  end
end
to_param() click to toggle source
# File lib/dyna_model/document.rb, line 128
def to_param
  self.dynamo_db_guid
end
touch() click to toggle source
# File lib/dyna_model/document.rb, line 164
def touch
  self.send(:touch_timestamps, "updated_at")
end
touch!() click to toggle source
# File lib/dyna_model/document.rb, line 168
def touch!
  self.touch
  self.save
end
update(options={}) click to toggle source
# File lib/dyna_model/document.rb, line 104
def update(options={})
  #return unless changed?
  touch_timestamps('updated_at')
  # Not implemented
  #increment_optimistic_lock_value
  update_storage(options)
end