Module: RedisRedirect::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Redirect
Defined in:
lib/redis-redirect/persistence.rb

Overview

Tries to apply the same interface used by ActiveRecord::Persistence to redis, when it makes sense, for simple use with scaffolded controllers api.rubyonrails.org/classes/ActiveRecord/Persistence.html

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject Also known as: delete



39
40
41
42
43
44
# File 'lib/redis-redirect/persistence.rb', line 39

def destroy
  result = 1 == RedisRedirect.redis.del(source)
  @destroyed = true
  freeze
  result
end

#destroy!Object



47
48
49
# File 'lib/redis-redirect/persistence.rb', line 47

def destroy!
  destroy || raise(RecordNotDestroyed)
end

#destroyed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/redis-redirect/persistence.rb', line 20

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/redis-redirect/persistence.rb', line 16

def new_record?
  !RedisRedirect.redis.get(source)
end

#persisted?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/redis-redirect/persistence.rb', line 24

def persisted?
  !(new_record? || destroyed?)
end

#saveObject Also known as: update

pretty much the same as an update, actually, yea



29
30
31
# File 'lib/redis-redirect/persistence.rb', line 29

def save
  create_or_update
end

#save!Object Also known as: update!



34
35
36
# File 'lib/redis-redirect/persistence.rb', line 34

def save!
  create_or_update || raise(RecordNotSaved)
end

#update_attributes(attributes = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/redis-redirect/persistence.rb', line 51

def update_attributes(attributes = {})
  attributes = HashWithIndifferentAccess.new(attributes)
  source = attributes['source'] if attributes.has_key?('source')
  target = attributes['target'] if attributes.has_key?('target')
  save
end

#update_attributes!(attributes = {}) ⇒ Object



57
58
59
60
# File 'lib/redis-redirect/persistence.rb', line 57

def update_attributes!(attributes = {})
  update_attributes(attributes)
  save!
end