Module: PulseMeter::Mixins::Dumper::InstanceMethods

Defined in:
lib/pulse-meter/mixins/dumper.rb

Instance Method Summary collapse

Instance Method Details

#cleanup_dumpObject

Cleans up object dump in Redis



40
41
42
# File 'lib/pulse-meter/mixins/dumper.rb', line 40

def cleanup_dump
  redis.hdel(DUMP_REDIS_KEY, self.name)
end

#dump!(safe = true) ⇒ Object

Serializes object and saves it to Redis

Raises:

  • (DumpError)

    if dumping fails for any reason



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pulse-meter/mixins/dumper.rb', line 13

def dump!(safe = true)
  ensure_storability!
  serialized_obj = to_yaml
  if safe
    unless redis.hsetnx(DUMP_REDIS_KEY, name, serialized_obj)
      stored = self.class.restore(name)
      unless stored.class == self.class
        raise DumpConflictError, "Attempt to create sensor #{name} of class #{self.class} but it already has class #{stored.class}"
      end
    end
  else
    redis.hset(DUMP_REDIS_KEY, name, serialized_obj)
  end
rescue DumpError, RestoreError => exc
  raise exc
rescue StandardError => exc
  raise DumpError, "object cannot be dumped: #{exc}"
end

#ensure_storability!Object

Ensures that object is dumpable

Raises:



34
35
36
37
# File 'lib/pulse-meter/mixins/dumper.rb', line 34

def ensure_storability!
  raise DumpError, "#name attribute must be readable" unless self.respond_to?(:name)
  raise DumpError, "#redis attribute must be available" unless self.respond_to?(:redis) && self.redis
end