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
10
11
# File 'lib/modis/persistence.rb', line 3

def self.included(base)
  base.extend ClassMethods
  base.instance_eval do
    class << self
      attr_reader :sti_child
      alias_method :sti_child?, :sti_child
    end
  end
end

Instance Method Details

#destroyObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/modis/persistence.rb', line 124

def destroy
  self.class.transaction do |redis|
    run_callbacks :destroy do
      redis.pipelined do
        remove_from_indexes(redis)
        redis.srem(self.class.key_for(:all), id)
        redis.del(key)
      end
    end
  end
end

#keyObject



106
107
108
# File 'lib/modis/persistence.rb', line 106

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

#new_record?Boolean

Returns:

  • (Boolean)


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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  true
end

#reloadObject



136
137
138
139
140
# File 'lib/modis/persistence.rb', line 136

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

#save(args = {}) ⇒ Object



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

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

#save!(args = {}) ⇒ Object



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

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

#update_attribute(name, value) ⇒ Object



142
143
144
145
# File 'lib/modis/persistence.rb', line 142

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

#update_attributes(attrs) ⇒ Object



147
148
149
150
# File 'lib/modis/persistence.rb', line 147

def update_attributes(attrs)
  assign_attributes(attrs)
  save
end

#update_attributes!(attrs) ⇒ Object



152
153
154
155
# File 'lib/modis/persistence.rb', line 152

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