Module: ShadyDB::Persistence

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml
Included in:
Document
Defined in:
lib/shadydb/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shadydb/persistence.rb', line 8

def self.included(base)
  base.class_eval do
    define_model_callbacks :save, :create, :update, :destroy, :persist, :restore
    
    before_persist :_set_raw
    before_restore :_get_raw
    before_create :_ensure_data_directory_exists
    
    extend ClassMethods
  end
end

Instance Method Details

#destroyObject



42
43
44
45
46
47
48
49
50
# File 'lib/shadydb/persistence.rb', line 42

def destroy
  return false unless persisted?
  begin
    File.delete(path)
    @destroyed = true
  rescue
    false
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/shadydb/persistence.rb', line 60

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/shadydb/persistence.rb', line 56

def new_record?
  @new_record
end

#pathObject



64
65
66
# File 'lib/shadydb/persistence.rb', line 64

def path
  File.join(self.class.data_directory, id)
end

#persisted?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/shadydb/persistence.rb', line 52

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

#saveObject



31
32
33
34
35
# File 'lib/shadydb/persistence.rb', line 31

def save
  _run_save_callbacks do
    create_or_update
  end
end

#update_attributes(attribs) ⇒ Object



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

def update_attributes(attribs)
  @attributes.merge!(attribs.stringify_keys)
  save
end