Class: Marconi::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/marconi/envelope.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Envelope

Returns a new instance of Envelope.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/marconi/envelope.rb', line 20

def initialize(options = {}, &block)
  if xml = options[:xml]
    hash = SmartXML.parse(xml)
    @topic = hash[:headers][:topic]
    hash[:payload].each do |hsh|
      messages << { :meta => hsh[:meta], :data => HashWithIndifferentAccess.new(hsh[:data]) }
    end
  else
    @timestamp = options[:timestamp] || Time.now
    @topic = options[:topic]
    block.call(self) if block_given?
  end
end

Instance Attribute Details

#topicObject

Returns the value of attribute topic.



9
10
11
# File 'lib/marconi/envelope.rb', line 9

def topic
  @topic
end

Class Method Details

.from_xml(xml) ⇒ Object



13
14
15
16
# File 'lib/marconi/envelope.rb', line 13

def from_xml(xml)
  return nil unless xml
  Envelope.new(:xml => xml)
end

Instance Method Details

#contentsObject



38
39
40
# File 'lib/marconi/envelope.rb', line 38

def contents
  { :headers => headers, :payload => messages }
end

#create(model) ⇒ Object



42
43
44
# File 'lib/marconi/envelope.rb', line 42

def create(model)
  add_message(model, :create)
end

#destroy(model) ⇒ Object



50
51
52
# File 'lib/marconi/envelope.rb', line 50

def destroy(model)
  add_message(model, :destroy)
end

#messagesObject



34
35
36
# File 'lib/marconi/envelope.rb', line 34

def messages
  @messages ||= []
end

#override(old_guid, model) ⇒ Object



54
55
56
# File 'lib/marconi/envelope.rb', line 54

def override(old_guid, model)
  add_message(model, :override, old_guid)
end

#to_sObject



58
59
60
# File 'lib/marconi/envelope.rb', line 58

def to_s
  contents.to_xml
end

#update(model) ⇒ Object



46
47
48
# File 'lib/marconi/envelope.rb', line 46

def update(model)
  add_message(model, :update)
end