Class: Ayl::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ayl/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, selector, opts, *args) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
# File 'lib/ayl/message.rb', line 8

def initialize(object, selector, opts, *args)
  @object = object
  @selector = selector
  @options = opts
  @arguments = args
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



5
6
7
# File 'lib/ayl/message.rb', line 5

def arguments
  @arguments
end

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/ayl/message.rb', line 6

def code
  @code
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/ayl/message.rb', line 5

def object
  @object
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/ayl/message.rb', line 5

def options
  @options
end

#selectorObject

Returns the value of attribute selector.



5
6
7
# File 'lib/ayl/message.rb', line 5

def selector
  @selector
end

Class Method Details

.from_hash(message_hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ayl/message.rb', line 15

def self.from_hash(message_hash)
  raise Ayl::UnrecoverableMessageException, "parameter must be a hash" unless message_hash.is_a?(Hash)
  raise Ayl::UnrecoverableMessageException, "not a valid message hash" if message_hash['type'] != 'ayl' || message_hash['code'].nil?
  raise Ayl::UnrecoverableMessageException, "No code provided in job: #{job.body}" if message_hash['code'].nil?

  code = message_hash['code']

  Message.new(nil, nil, MessageOptions.new).tap do | m |
    m.send(:message_hash=, message_hash)
    m.send(:code=, code)
    m.options.failed_job_handler = 
      message_hash['failed_job_handler'] if message_hash['failed_job_handler']
    m.options.failed_job_count = 
      message_hash['failed_job_count'] if message_hash['failed_job_handler'] == 'decay' && message_hash['failed_job_count']
    m.options.failed_job_delay = 
      message_hash['failed_job_delay'] if message_hash['failed_job_handler'] == 'decay' && message_hash['failed_job_delay']
  end
  
end

Instance Method Details

#evaluate(top_binding) ⇒ Object



56
57
58
59
# File 'lib/ayl/message.rb', line 56

def evaluate(top_binding)
  code_to_eval = to_rrepr
  eval(code_to_eval, top_binding, code_to_eval, 1)
end

#new_message_hashObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ayl/message.rb', line 43

def new_message_hash
  {
    :type => :ayl,
    :failed_job_handler => options.failed_job_handler,
    :code => to_rrepr
  }.tap do | h |
    if options.failed_job_handler == 'decay'
      h[:failed_job_count] = options.failed_job_count
      h[:failed_job_delay] = options.failed_job_delay
    end
  end
end

#to_hashObject



39
40
41
# File 'lib/ayl/message.rb', line 39

def to_hash
  @message_hash ||= new_message_hash
end

#to_rreprObject



35
36
37
# File 'lib/ayl/message.rb', line 35

def to_rrepr
  @code ||= %Q{#{@object.to_rrepr}.#{@selector}(#{@arguments.to_rrepr[1...-1]})}
end