Class: Armada::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Observing, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations, AttributeMethods, DatabaseMethods, FinderMethods, RelationMethods, Validations
Defined in:
lib/armada/model.rb

Instance Method Summary collapse

Methods included from AttributeMethods

#attributes=, #method_missing, #read_attribute, #respond_to?, #write_attribute

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



19
20
21
22
23
# File 'lib/armada/model.rb', line 19

def initialize(attributes = {})
  @attributes = {}.with_indifferent_access
  @new_record = true
  self.attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Armada::AttributeMethods

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/armada/model.rb', line 49

def ==(other)
  klass = self.class
  case other
    when klass then klass.collection_name == other.class.collection_name && self.attributes == other.attributes
    else false
  end
end

#destroyObject



29
30
31
# File 'lib/armada/model.rb', line 29

def destroy
  @destroyed = (persisted? && relation.delete == 1)
end

#destroyed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/armada/model.rb', line 33

def destroyed?
  @destroyed || false
end

#new_record?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/armada/model.rb', line 25

def new_record?
  @new_record || false
end

#persisted?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/armada/model.rb', line 37

def persisted?
  !(new_record? || destroyed?)
end

#saveObject



41
42
43
# File 'lib/armada/model.rb', line 41

def save
  create_or_update
end

#save!Object



45
46
47
# File 'lib/armada/model.rb', line 45

def save!
  save || raise(Armada::RecordNotSaved)
end