Module: Toy::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
Dirty
Defined in:
lib/toy/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



63
64
65
# File 'lib/toy/attributes.rb', line 63

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



67
68
69
# File 'lib/toy/attributes.rb', line 67

def []=(key, value)
  write_attribute(key, value)
end

#attributesObject



39
40
41
# File 'lib/toy/attributes.rb', line 39

def attributes
  @attributes
end

#attributes=(attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/toy/attributes.rb', line 52

def attributes=(attrs, *)
  return if attrs.nil?
  attrs.each do |key, value|
    if attribute_method?(key)
      write_attribute(key, value)
    elsif respond_to?("#{key}=")
      send("#{key}=", value)
    end
  end
end

#idObject



35
36
37
# File 'lib/toy/attributes.rb', line 35

def id
  read_attribute(:id)
end

#initialize(attrs = {}) ⇒ Object



29
30
31
32
33
# File 'lib/toy/attributes.rb', line 29

def initialize(attrs={})
  initialize_attributes_with_defaults
  self.attributes = attrs
  write_attribute :id, self.class.next_key(self) unless id?
end

#persisted_attributesObject



43
44
45
46
47
48
49
50
# File 'lib/toy/attributes.rb', line 43

def persisted_attributes
  {}.tap do |attrs|
    self.class.attributes.each do |name, attribute|
      next if attribute.virtual?
      attrs[attribute.persisted_name] = attribute.to_store(read_attribute(attribute.name))
    end
  end
end