Module: Toy::Attributes

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



73
74
75
# File 'lib/toy/attributes.rb', line 73

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



77
78
79
# File 'lib/toy/attributes.rb', line 77

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

#attributesObject



49
50
51
# File 'lib/toy/attributes.rb', line 49

def attributes
  @attributes
end

#attributes=(attrs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/toy/attributes.rb', line 62

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

#idObject



45
46
47
# File 'lib/toy/attributes.rb', line 45

def id
  read_attribute(:id)
end

#initialize(attrs = {}) ⇒ Object



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

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

#persisted_attributesObject



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

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