Class: HashPipe::ArchivedAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/hashpipe/archived_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, instance, opts = {}) ⇒ ArchivedAttribute

Returns a new instance of ArchivedAttribute.



12
13
14
15
16
17
18
19
20
21
# File 'lib/hashpipe/archived_attribute.rb', line 12

def initialize(name, instance, opts = {})
  @name     = name
  @instance = instance
  @dirty    = false

  @_options = HashPipe::GlobalConfiguration.instance.to_hash.
    merge(opts)

  @backend = instantiate_backend_from(options)
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



10
11
12
# File 'lib/hashpipe/archived_attribute.rb', line 10

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/hashpipe/archived_attribute.rb', line 10

def name
  @name
end

Instance Method Details

#destroyObject



46
47
48
# File 'lib/hashpipe/archived_attribute.rb', line 46

def destroy
  @backend.destroy
end

#dirty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hashpipe/archived_attribute.rb', line 36

def dirty?
  @dirty
end

#instantiate_backend_from(options) ⇒ Object

Returns a backend object based on the options given (e.g., filesystem, s3).



52
53
54
55
# File 'lib/hashpipe/archived_attribute.rb', line 52

def instantiate_backend_from(options)
  "HashPipe::Backends::#{options[:storage].to_s.camelize}".
    constantize.new(self)
end

#optionsObject



57
58
59
# File 'lib/hashpipe/archived_attribute.rb', line 57

def options
  @_options
end

#saveObject

First saves this record to the back-end. If backend storage raises an error, we capture it and add it to the AR validation errors.



42
43
44
# File 'lib/hashpipe/archived_attribute.rb', line 42

def save
  @backend.save(@stashed_value) if self.dirty?
end

#valueObject



23
24
25
26
27
# File 'lib/hashpipe/archived_attribute.rb', line 23

def value
  val = defined?(@stashed_value) ? @stashed_value : @backend.load
  val = compress? ? Zlib::Inflate.inflate(val) : val
  val = marshal? ? Marshal.load(val) : val
end

#value=(other) ⇒ Object



29
30
31
32
33
34
# File 'lib/hashpipe/archived_attribute.rb', line 29

def value=(other)
  other = marshal? ? Marshal.dump(other) : other
  other = compress? && !other.nil? ? Zlib::Deflate.deflate(other) : other
  @stashed_value = other
  @dirty = true
end