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



5
6
7
8
9
10
11
12
13
# File 'lib/modis/persistence.rb', line 5

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

Instance Method Details

#destroyObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/modis/persistence.rb', line 134

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

#keyObject



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

def key
  return nil if new_record?
  self.class.sti_child? ? self.class.sti_base_key_for(id) : self.class.key_for(id)
end

#new_record?Boolean

Returns:

  • (Boolean)


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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  true
end

#reloadObject



149
150
151
152
153
# File 'lib/modis/persistence.rb', line 149

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

#save(args = {}) ⇒ Object



124
125
126
127
128
# File 'lib/modis/persistence.rb', line 124

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

#save!(args = {}) ⇒ Object



130
131
132
# File 'lib/modis/persistence.rb', line 130

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

#update_attribute(name, value) ⇒ Object



155
156
157
158
# File 'lib/modis/persistence.rb', line 155

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

#update_attributes(attrs) ⇒ Object



160
161
162
163
# File 'lib/modis/persistence.rb', line 160

def update_attributes(attrs)
  assign_attributes(attrs)
  save
end

#update_attributes!(attrs) ⇒ Object



165
166
167
168
# File 'lib/modis/persistence.rb', line 165

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