Module: Ambry::ActiveModel::InstanceMethods

Defined in:
lib/ambry/active_model.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



76
77
78
79
80
# File 'lib/ambry/active_model.rb', line 76

def attributes
  hash = to_hash
  hash.keys.each {|k| hash[k.to_s] = hash.delete(k)}
  hash
end

#destroyObject



122
123
124
# File 'lib/ambry/active_model.rb', line 122

def destroy
  run_callbacks(:destroy) { delete }
end

#initialize(*args) ⇒ Object



71
72
73
74
# File 'lib/ambry/active_model.rb', line 71

def initialize(*args)
  @new_record = true
  super
end

#keysObject



82
83
84
# File 'lib/ambry/active_model.rb', line 82

def keys
  self.class.attribute_names
end

#new_record?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ambry/active_model.rb', line 90

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ambry/active_model.rb', line 94

def persisted?
  !new_record?
end

#saveObject



98
99
100
101
102
103
104
# File 'lib/ambry/active_model.rb', line 98

def save
  return false unless valid?
  run_callbacks(:save) do
    @new_record = false
    super
  end
end

#save!Object



106
107
108
109
110
111
112
# File 'lib/ambry/active_model.rb', line 106

def save!
  if !valid?
    raise Ambry::AmbryError, errors.to_a.join(", ")
  else
    save
  end
end

#to_keyObject



118
119
120
# File 'lib/ambry/active_model.rb', line 118

def to_key
  [to_param] if persisted?
end

#to_modelObject



86
87
88
# File 'lib/ambry/active_model.rb', line 86

def to_model
  self
end

#to_paramObject



114
115
116
# File 'lib/ambry/active_model.rb', line 114

def to_param
  to_id if persisted?
end

#to_partial_pathObject



130
131
132
# File 'lib/ambry/active_model.rb', line 130

def to_partial_path
  "#{self.class.name.pluralize.underscore}/#{self.class.name.underscore}"
end

#update_attributes(*args) ⇒ Object



126
127
128
# File 'lib/ambry/active_model.rb', line 126

def update_attributes(*args)
  run_callbacks(:save) { update(*args) }
end