Class: FIFO::Payload
- Inherits:
-
Object
- Object
- FIFO::Payload
- Defined in:
- lib/fifo/payload.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#queue ⇒ Object
Returns the value of attribute queue.
Instance Method Summary collapse
-
#failed ⇒ Object
Public.
-
#initialize(object, method, *args) ⇒ Payload
constructor
Public.
-
#process ⇒ Object
Public.
-
#retry ⇒ Object
Public.
-
#to_s ⇒ Object
Public.
Constructor Details
#initialize(object, method, *args) ⇒ Payload
Public. Create a payload with an object a method of that object and optional arguments for that method.
object - The Object the payload carries. method - The Symbol method name for the object. args - The optional array of arguments for the method.
Returns nothing. Raises ArgumentError if object or method aren’t present.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fifo/payload.rb', line 16 def initialize(object,method,*args) raise ArgumentError, "object and method required" unless object && method @attempts = 1 @object = object.is_a?(Class) ? object.to_s.split("::").last : object.to_yaml @method = method @arguments = args end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
5 6 7 |
# File 'lib/fifo/payload.rb', line 5 def attempts @attempts end |
#queue ⇒ Object
Returns the value of attribute queue.
4 5 6 |
# File 'lib/fifo/payload.rb', line 4 def queue @queue end |
Instance Method Details
#failed ⇒ Object
Public. Mark the instance as failed by increaseing the number of attempts to process it.
Returns the Integer number of attempts used.
51 52 53 |
# File 'lib/fifo/payload.rb', line 51 def failed @attempts += 1 end |
#process ⇒ Object
Public. Call the method on the carried object with the carried arguments. If the carried object is an ActiveRecord::Base object, reload it from the db to get a fresh state.
Returns the result of the method call on the object.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fifo/payload.rb', line 33 def process if is_object_yaml? object = YAML.load(@object) if defined?(ActiveRecord) && object.is_a?(ActiveRecord::Base) object = object.class.find object.id end else object = Object.const_get(@object) end object.send(@method.to_sym,*@arguments) end |
#retry ⇒ Object
Public. Add the payload back onto the queue.
57 58 59 60 |
# File 'lib/fifo/payload.rb', line 57 def retry failed queue.push self end |
#to_s ⇒ Object
Public. Override to_s for a loggable output.
Returns the String form of the Payload.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fifo/payload.rb', line 65 def to_s output = "" if is_object_yaml? object = YAML.load(@object) output << object.class.name else output << Object.const_get(@object).name end output << " :#{@method} #{@arguments.join("|")}" output end |