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, scope, backend, opts = {}) ⇒ ArchivedAttribute

Returns a new instance of ArchivedAttribute.



10
11
12
13
14
15
16
# File 'lib/hashpipe/archived_attribute.rb', line 10

def initialize(name, scope, backend, opts = {})
  @name    = name
  @scope   = scope
  @dirty   = false
  @options = HashPipe::GlobalConfiguration.instance.to_hash.merge(opts)
  @backend = backend
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



7
8
9
# File 'lib/hashpipe/archived_attribute.rb', line 7

def backend
  @backend
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/hashpipe/archived_attribute.rb', line 7

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



8
9
10
# File 'lib/hashpipe/archived_attribute.rb', line 8

def scope
  @scope
end

Instance Method Details

#destroyObject



40
41
42
# File 'lib/hashpipe/archived_attribute.rb', line 40

def destroy
  backend.delete(key)
end

#dirty?Boolean

Returns:

  • (Boolean)


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

def dirty?
  @dirty
end

#keyObject



48
49
50
# File 'lib/hashpipe/archived_attribute.rb', line 48

def key
  [scope, name].join('_')
end

#optionsObject



44
45
46
# File 'lib/hashpipe/archived_attribute.rb', line 44

def options
  @options
end

#saveObject



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

def save
  backend[key] = @stashed_value if self.dirty?
  @dirty = false
end

#valueObject



18
19
20
21
22
# File 'lib/hashpipe/archived_attribute.rb', line 18

def value
  val = defined?(@stashed_value) ? @stashed_value : backend[key]
  val = compress? && !val.nil? ? Zlib::Inflate.inflate(val) : val
  val = marshal? ? Marshal.load(val) : val
end

#value=(other) ⇒ Object



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

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