Module: Kangaroo::Model::Persistence

Included in:
Base
Defined in:
lib/kangaroo/model/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject

Destroy this record

Returns:

  • self



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

def destroy
  return true if destroyed? || new_record?
  _run_destroy_callbacks do
    remote.unlink [id], :context => context
  end

  self
end

#destroyed?boolean

Check if this record has been destroyed

Returns:

  • (boolean)

    true/false



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

def destroyed?
  @destroyed
end

#new_record?boolean

Check if this record hasnt been persisted yet

Returns:

  • (boolean)

    true/false



27
28
29
# File 'lib/kangaroo/model/persistence.rb', line 27

def new_record?
  @new_record
end

#persisted?boolean

Check if this record has been persisted yet

Returns:

  • (boolean)

    true/false



34
35
36
# File 'lib/kangaroo/model/persistence.rb', line 34

def persisted?
  !@new_record
end

#reload(fields = self.class.attribute_names) ⇒ Object

Reload this record, or just a subset of fields

Parameters:

  • fields (Array) (defaults to: self.class.attribute_names)

    to reload

Returns:

  • self



80
81
82
83
84
85
86
87
# File 'lib/kangaroo/model/persistence.rb', line 80

def reload fields = self.class.attribute_names
  @attributes = remote.read([id], fields, context).first.except(:id).stringify_keys
  fields.each do |field|
    @changed_attributes.delete field.to_s
  end

  self
end

#save(options = {}) ⇒ boolean

Save this record

Parameters:

  • options (Hash) (defaults to: {})

    unused

Returns:

  • (boolean)

    true/false



61
62
63
64
65
# File 'lib/kangaroo/model/persistence.rb', line 61

def save options = {}
  _run_save_callbacks do
    create_or_update
  end
end

#save!(options = {}) ⇒ boolean

Save this record or raise an error

Parameters:

  • options (Hash) (defaults to: {})

    unused

Returns:

  • (boolean)

    true



71
72
73
74
# File 'lib/kangaroo/model/persistence.rb', line 71

def save! options = {}
  save options ||
  raise(RecordSavingFailed)
end