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.



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

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

#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



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ayl/message.rb', line 14

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)
  end
  
end

Instance Method Details

#evaluate(top_binding) ⇒ Object



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

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

#to_hashObject



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

def to_hash
  @message_hash ||= {
    :type => :ayl,
    :code => to_rrepr
  }
end

#to_rreprObject



28
29
30
# File 'lib/ayl/message.rb', line 28

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