Class: ObStore::Data

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/obstore/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#mutex, #with_mutex

Constructor Details

#initialize(data = nil, options = {}) ⇒ Data

Returns a new instance of Data.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/obstore/data.rb', line 19

def initialize(data=nil, options={})
  @expiry = options[:expiry] # in seconds
  @data = {}
  store_data_by_key :data, data
  @updated = Time.now
  if options[:metadata]
    options[:metadata].each do |key, value|
      store_data_by_key key, value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



81
82
83
84
85
86
87
88
89
# File 'lib/obstore/data.rb', line 81

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(.+)=$/
    store_data_by_key($1, *args)
  elsif meth.to_s =~ /^(.+)$/
    fetch_data_by_key($1)
  else
    super
  end
end

Instance Attribute Details

#dataObject

custom getter for retrieving data from the data hash



57
58
59
# File 'lib/obstore/data.rb', line 57

def data
  @data
end

#expiryObject

Returns the value of attribute expiry.



16
17
18
# File 'lib/obstore/data.rb', line 16

def expiry
  @expiry
end

#updatedObject (readonly)

Returns the value of attribute updated.



17
18
19
# File 'lib/obstore/data.rb', line 17

def updated
  @updated
end

Instance Method Details

#fetchObject

returns the object you had saved



32
33
34
# File 'lib/obstore/data.rb', line 32

def fetch
  fetch_data_by_key(:data)
end

#save(data) ⇒ Object

Saves the object



47
48
49
# File 'lib/obstore/data.rb', line 47

def save(data)
  self.data=data
end

#stale?Boolean

returns boolean value if data has expired

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/obstore/data.rb', line 37

def stale?
  if @expiry
    if ts < Time.now.to_i - @expiry
      return true
    end
  end
  return false
end

#tsObject

helper method to return the timestamp as an int



62
63
64
# File 'lib/obstore/data.rb', line 62

def ts
  with_mutex { @updated.to_i }
end