Module: Dynamoid::Document::ClassMethods

Defined in:
lib/dynamoid/document.rb

Instance Method Summary collapse

Instance Method Details

#attr_readonly(*read_only_attributes) ⇒ Object



35
36
37
# File 'lib/dynamoid/document.rb', line 35

def attr_readonly(*read_only_attributes)
  self.read_only_attributes.concat read_only_attributes.map(&:to_s)
end

#build(attrs = {}) ⇒ Dynamoid::Document

Initialize a new object.

Parameters:

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

    Attributes with which to create the object.

Returns:

Since:

  • 0.2.0



104
105
106
# File 'lib/dynamoid/document.rb', line 104

def build(attrs = {})
  attrs[:type] ? attrs[:type].constantize.new(attrs) : new(attrs)
end

#countObject

Returns the number of items for this class.

Since:

  • 0.6.1



63
64
65
# File 'lib/dynamoid/document.rb', line 63

def count
  Dynamoid.adapter.count(table_name)
end

#create(attrs = {}) ⇒ Dynamoid::Document

Initialize a new object and immediately save it to the database.

Parameters:

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

    Attributes with which to create the object.

Returns:

Since:

  • 0.2.0



74
75
76
77
78
79
80
# File 'lib/dynamoid/document.rb', line 74

def create(attrs = {})
  if attrs.is_a?(Array)
    attrs.map { |attr| create(attr) }
  else
    build(attrs).tap(&:save)
  end
end

#create!(attrs = {}) ⇒ Dynamoid::Document

Initialize a new object and immediately save it to the database. Raise an exception if persistence failed.

Parameters:

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

    Attributes with which to create the object.

Returns:

Since:

  • 0.2.0



89
90
91
92
93
94
95
# File 'lib/dynamoid/document.rb', line 89

def create!(attrs = {})
  if attrs.is_a?(Array)
    attrs.map { |attr| create!(attr) }
  else
    build(attrs).tap(&:save!)
  end
end

#deep_subclassesObject



122
123
124
# File 'lib/dynamoid/document.rb', line 122

def deep_subclasses
  subclasses + subclasses.map(&:deep_subclasses).flatten
end

#exists?(id_or_conditions = {}) ⇒ Boolean

Does this object exist?

Parameters:

  • id_or_conditions (Mixed) (defaults to: {})

    the id of the object or a hash with the options to filter from.

Returns:

  • (Boolean)

    true/false

Since:

  • 0.2.0



115
116
117
118
119
120
# File 'lib/dynamoid/document.rb', line 115

def exists?(id_or_conditions = {})
  case id_or_conditions
    when Hash then where(id_or_conditions).first.present?
    else !! find_by_id(id_or_conditions)
  end
end

#hash_keyObject

Returns the id field for this class.

Since:

  • 0.4.0



56
57
58
# File 'lib/dynamoid/document.rb', line 56

def hash_key
  options[:key] || :id
end

#read_capacityObject

Returns the read_capacity for this table.

Since:

  • 0.4.0



42
43
44
# File 'lib/dynamoid/document.rb', line 42

def read_capacity
  options[:read_capacity] || Dynamoid::Config.read_capacity
end

#table(options = {}) ⇒ Object

Set up table options, including naming it whatever you want, setting the id key, and manually overriding read and write capacity.

Parameters:

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

    options to pass for this table

Options Hash (options):

  • :name (Symbol)

    the name for the table; this still gets namespaced

  • :id (Symbol)

    id column for the table

  • :read_capacity (Integer)

    set the read capacity for the table; does not work on existing tables

  • :write_capacity (Integer)

    set the write capacity for the table; does not work on existing tables

Since:

  • 0.4.0



30
31
32
33
# File 'lib/dynamoid/document.rb', line 30

def table(options = {})
  self.options = options
  super if defined? super
end

#write_capacityObject

Returns the write_capacity for this table.

Since:

  • 0.4.0



49
50
51
# File 'lib/dynamoid/document.rb', line 49

def write_capacity
  options[:write_capacity] || Dynamoid::Config.write_capacity
end