Module: Aws::Record::Attributes

Included in:
Aws::Record
Defined in:
lib/aws-record/record/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(sub_class) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/aws-record/record/attributes.rb', line 6

def self.included(sub_class)
  sub_class.extend(ClassMethods)
  model_attributes = ModelAttributes.new(self)
  sub_class.instance_variable_set('@attributes', model_attributes)
  sub_class.instance_variable_set('@keys', KeyAttributes.new(model_attributes))
  inherit_attributes(sub_class) if Aws::Record.extends_record?(sub_class)
end

Instance Method Details

#initialize(attr_values = {}) ⇒ Aws::Record

Base initialization method for a new item. Optionally, allows you to provide initial attribute values for the model. You do not need to provide all, or even any, attributes at item creation time.

Inheritance Support

Child models will inherit the attributes and keys defined in the parent model. Child models can override attribute keys if defined in their own model.

See examples below to see the feature in action.

Examples:

Usage Example

class MyModel
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
  string_attr  :body
end

item = MyModel.new(id: 1, name: "Quick Create")

Child model inheriting from Parent model

class Animal
  include Aws::Record
  string_attr :name,   hash_key: true
  integer_attr :age,   default_value: 1
end

class Cat < Animal
  include Aws::Record
  integer_attr :num_of_wiskers
end

cat = Cat.find(name: 'Foo')
cat.age    # => 1
cat.num_of_wiskers = 200

Child model overrides the hash key

class Animal
  include Aws::Record
  string_attr :name,   hash_key: true
  integer_attr :age,   range_key: true
end

class Dog < Animal
  include Aws::Record
  integer_attr :id, hash_key: true
end

Dog.keys # => {:hash=>:id, :range=>:age}

Parameters:

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

    Attribute symbol/value pairs for any initial attribute values you wish to set.

Returns:



63
64
65
66
67
68
69
70
71
# File 'lib/aws-record/record/attributes.rb', line 63

def initialize(attr_values = {})
  opts = {
    track_mutations: self.class.mutation_tracking_enabled?
  }
  @data = ItemData.new(self.class.attributes, opts)
  attr_values.each do |attr_name, attr_value|
    send("#{attr_name}=", attr_value)
  end
end

#to_hHash

Returns a hash representation of the attribute data.

Returns:

  • (Hash)

    Map of attribute names to raw values.



76
77
78
# File 'lib/aws-record/record/attributes.rb', line 76

def to_h
  @data.hash_copy
end