lib/qs/job.rb in qs-0.3.0 vs lib/qs/job.rb in qs-0.4.0

- old
+ new

@@ -1,28 +1,26 @@ +require 'qs/message' + module Qs - class Job + class Job < Message - def self.parse(payload) - created_at = Time.at(payload['created_at'].to_i) - self.new(payload['name'], payload['params'], created_at) - end + PAYLOAD_TYPE = 'job' - attr_reader :name, :params, :created_at + attr_reader :name, :created_at - def initialize(name, params, created_at = nil) - validate!(name, params) + def initialize(name, options = nil) + options ||= {} + options[:params] ||= {} + validate!(name, options[:params]) @name = name - @params = params - @created_at = created_at || Time.now + @created_at = options[:created_at] || Time.now + super(PAYLOAD_TYPE, options) end - def to_payload - { 'name' => self.name.to_s, - 'params' => StringifyParams.new(self.params), - 'created_at' => self.created_at.to_i - } + def route_name + self.name end def inspect reference = '0x0%x' % (self.object_id << 1) "#<#{self.class}:#{reference} " \ @@ -31,11 +29,14 @@ "@created_at=#{self.created_at.inspect}>" end def ==(other) if other.kind_of?(self.class) - self.to_payload == other.to_payload + self.payload_type == other.payload_type && + self.name == other.name && + self.params == other.params && + self.created_at == other.created_at else super end end @@ -45,26 +46,13 @@ problem = if name.to_s.empty? "The job doesn't have a name." elsif !params.kind_of?(::Hash) "The job's params are not valid." end - raise(BadJobError, problem) if problem + raise(InvalidError, problem) if problem end - module StringifyParams - def self.new(object) - case(object) - when Hash - object.inject({}){ |h, (k, v)| h.merge(k.to_s => self.new(v)) } - when Array - object.map{ |item| self.new(item) } - else - object - end - end - end + InvalidError = Class.new(ArgumentError) end - - BadJobError = Class.new(ArgumentError) end