Module: NinjaModel::Persistence::InstanceMethods

Defined in:
lib/ninja_model/persistence.rb

Instance Method Summary collapse

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
# File 'lib/ninja_model/persistence.rb', line 18

def create
  run_callbacks :create do
    if self.class.adapter.create(self)
      @persisted = true
    end
    @persisted
  end
end

#destroyObject



45
46
47
48
49
50
51
52
# File 'lib/ninja_model/persistence.rb', line 45

def destroy
  run_callbacks :destroy do
    if self.class.adapter.destroy(self)
      @destroyed = true
    end
    @destroyed
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ninja_model/persistence.rb', line 37

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ninja_model/persistence.rb', line 33

def new_record?
  !@persisted
end

#persisted?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ninja_model/persistence.rb', line 41

def persisted?
  @persisted && !destroyed?
end

#reloadObject



54
55
56
# File 'lib/ninja_model/persistence.rb', line 54

def reload
  self.class.adapter.reload(self)
end

#saveObject



10
11
12
13
14
15
16
# File 'lib/ninja_model/persistence.rb', line 10

def save(*)
  run_callbacks :save do
    result = new_record? ? create : update
    changed_attributes.clear if result
    result
  end
end

#updateObject



27
28
29
30
31
# File 'lib/ninja_model/persistence.rb', line 27

def update
  run_callbacks :update do
    self.class.adapter.update(self)
  end
end

#update_attributes(attributes) ⇒ Object



58
59
60
61
# File 'lib/ninja_model/persistence.rb', line 58

def update_attributes(attributes)
  self.attributes = attributes
  save
end