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



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

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



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

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)


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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  true
end

#reloadObject



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

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

#save(args = {}) ⇒ Object



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

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

#save!(args = {}) ⇒ Object



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

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

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



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

def update(attrs)
  assign_attributes(attrs)
  save
end

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



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

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

#update_attribute(name, value) ⇒ Object



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

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