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



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

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
119
# 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)


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

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



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

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

#save(args = {}) ⇒ Object



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

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

#save!(args = {}) ⇒ Object



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

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

#update_attribute(name, value) ⇒ Object



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

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

#update_attributes(attrs) ⇒ Object



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

def update_attributes(attrs)
  assign_attributes(attrs)
  save
end

#update_attributes!(attrs) ⇒ Object



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

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