Class: GovernorBackground::Resque::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/governor_background/resque/resource.rb

Overview

Provides a way for ActiveRecord objects to be serialized to Resque. ActiveRecord objects can’t be sent directly to Resque, as Resque serializes everything to JSON. Resource provides an intermediate step. Resources are serialized to a JSON representation of the resource type (as registered in Governor) and the ID. When resources are deserialized, they are automatically converted into the ActiveRecord objects they represent.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_resource) ⇒ Resource

Returns a new instance of Resource.



12
13
14
15
# File 'lib/governor_background/resque/resource.rb', line 12

def initialize(ar_resource)
  @resource_key = ar_resource.class.name.tableize
  @id = ar_resource.id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/governor_background/resque/resource.rb', line 11

def id
  @id
end

#resource_keyObject (readonly)

Returns the value of attribute resource_key.



11
12
13
# File 'lib/governor_background/resque/resource.rb', line 11

def resource_key
  @resource_key
end

Class Method Details

.json_create(o) ⇒ Object



21
22
23
# File 'lib/governor_background/resque/resource.rb', line 21

def self.json_create(o)
  Governor.resources[o['resource_key'].to_sym].to.find(o['id'])
end

Instance Method Details

#to_hashObject



17
18
19
# File 'lib/governor_background/resque/resource.rb', line 17

def to_hash
  self.instance_values.merge('json_class' => self.class.name)
end