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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/modis/persistence.rb', line 137

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

#keyObject



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

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)


123
124
125
# File 'lib/modis/persistence.rb', line 123

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  true
end

#reloadObject



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

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

#save(args = {}) ⇒ Object



127
128
129
130
131
# File 'lib/modis/persistence.rb', line 127

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

#save!(args = {}) ⇒ Object



133
134
135
# File 'lib/modis/persistence.rb', line 133

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

#update(attrs) ⇒ Object Also known as: update_attributes



163
164
165
166
# File 'lib/modis/persistence.rb', line 163

def update(attrs)
  assign_attributes(attrs)
  save
end

#update!(attrs) ⇒ Object Also known as: update_attributes!



171
172
173
174
# File 'lib/modis/persistence.rb', line 171

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

#update_attribute(name, value) ⇒ Object



158
159
160
161
# File 'lib/modis/persistence.rb', line 158

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