Module: Ambry::ActiveModel::InstanceMethods

Defined in:
lib/ambry/active_model.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



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

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

#destroyObject



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

def destroy
  run_callbacks(:destroy) { delete }
end

#initialize(*args) ⇒ Object



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

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

#keysObject



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

def keys
  self.class.attribute_names
end

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !new_record?
end

#saveObject



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

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

#save!Object



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

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

#to_keyObject



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

def to_key
  [to_param] if persisted?
end

#to_modelObject



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

def to_model
  self
end

#to_paramObject



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

def to_param
  to_id if persisted?
end

#to_partial_pathObject



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

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

#update_attributes(*args) ⇒ Object



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

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