Module: Redis::Actions::Saving

Defined in:
lib/redis/actions/saving.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/redis/actions/saving.rb', line 2

def self.included(base)
  base.class_eval do
    define_model_callbacks :save
    class_inheritable_array :within_save_blocks
    self.within_save_blocks ||= []
    extend Redis::Actions::Saving::ClassMethods
  end
end

Instance Method Details

#define_idObject



37
38
39
40
41
# File 'lib/redis/actions/saving.rb', line 37

def define_id
  # model_name.to_s is required because model_name is actually an ActiveModel::Name
  # and that can't be serialized by Rubinius. (Worked fine with all other rubies, though...)
  self.id = File.join(model_name.to_s, connection.incr("__uniq__").to_s)
end

#saveObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis/actions/saving.rb', line 11

def save
  if valid?
    define_id if id.nil?
    run_callbacks(:save) do
      transaction do
        within_save_blocks.each do |block_or_method|
          if block_or_method.kind_of?(String) || block_or_method.kind_of?(Symbol)
            send block_or_method
          else
            block_or_method.call self
          end
        end
        connection.set(id, serializer.dump(attributes))
      end
    end
    set_unchanged!
    true
  else
    false
  end
end

#save!Object



33
34
35
# File 'lib/redis/actions/saving.rb', line 33

def save!
  raise "Record was not saved: #{errors.full_messages}" unless save
end