Module: Modis::Persistence

Defined in:
lib/modis/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/modis/persistence.rb', line 3

def self.included(base)
  base.extend ClassMethods
  base.instance_eval do
    after__internal_create :track
    before__internal_destroy :untrack
  end
end

Instance Method Details

#destroyObject



92
93
94
95
96
97
98
99
100
# File 'lib/modis/persistence.rb', line 92

def destroy
  self.class.transaction do |redis|
    run_callbacks :destroy do
      run_callbacks :_internal_destroy do
        redis.del(key)
      end
    end
  end
end

#keyObject



74
75
76
# File 'lib/modis/persistence.rb', line 74

def key
  new_record? ? nil : self.class.key_for(id)
end

#new_record?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/modis/persistence.rb', line 78

def new_record?
  defined?(@new_record) ? @new_record : true
end

#persisted?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/modis/persistence.rb', line 70

def persisted?
  true
end

#reloadObject



102
103
104
105
106
# File 'lib/modis/persistence.rb', line 102

def reload
  new_attributes = Modis.with_connection { |redis| self.class.attributes_for(redis, id) }
  initialize(new_attributes)
  self
end

#save(args = {}) ⇒ Object



82
83
84
85
86
# File 'lib/modis/persistence.rb', line 82

def save(args = {})
  create_or_update(args)
rescue Modis::RecordInvalid
  false
end

#save!(args = {}) ⇒ Object



88
89
90
# File 'lib/modis/persistence.rb', line 88

def save!(args = {})
  create_or_update(args) || (raise RecordNotSaved)
end

#update_attribute(name, value) ⇒ Object



108
109
110
111
# File 'lib/modis/persistence.rb', line 108

def update_attribute(name, value)
  assign_attributes(name => value)
  save(validate: false)
end

#update_attributes(attrs) ⇒ Object



113
114
115
116
# File 'lib/modis/persistence.rb', line 113

def update_attributes(attrs)
  assign_attributes(attrs)
  save
end

#update_attributes!(attrs) ⇒ Object



118
119
120
121
# File 'lib/modis/persistence.rb', line 118

def update_attributes!(attrs)
  assign_attributes(attrs)
  save!
end