Class: MandrillQueue::Message::Recipient::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_queue/message/recipient/data.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



36
37
38
# File 'lib/mandrill_queue/message/recipient/data.rb', line 36

def initialize
	@_vars = []
end

Class Method Details

.inherited(base) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/mandrill_queue/message/recipient/data.rb', line 10

def self.inherited(base)
	base.class_eval do
		define_method(base.name.underscore.split('/').last) do
			@_vars
		end
	end
end

.var_class(klass = nil) ⇒ Object



18
19
20
21
# File 'lib/mandrill_queue/message/recipient/data.rb', line 18

def self.var_class(klass = nil)
	@_var_class = klass unless klass.nil?
	@_var_class
end

Instance Method Details

#[](index) ⇒ Object



77
78
79
# File 'lib/mandrill_queue/message/recipient/data.rb', line 77

def [](index)
	@_vars[index]
end

#add(*args, &block) ⇒ Object Also known as: dsl



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mandrill_queue/message/recipient/data.rb', line 44

def add(*args, &block)
	recipient = args.first
	if recipient.is_a?(String)
		ind = recipient_index[recipient]
		if ind.nil?
			@_vars << var_class.new(*args, &block)
			register_recipient(recipient, @_vars.count - 1)
		else
			self[ind].variables.dsl(&block)
		end
	elsif recipient.respond_to?(:each)
		add_objects(*args, &block)
	elsif !recipient.nil?
		raise MessageError, "Invalid recipient for #{name}"
	end
end

#clear_recipient_indexObject



32
33
34
# File 'lib/mandrill_queue/message/recipient/data.rb', line 32

def clear_recipient_index
	@_rcpt_index = {}
end

#countObject



81
82
83
# File 'lib/mandrill_queue/message/recipient/data.rb', line 81

def count
	@_vars.count
end

#firstObject



69
70
71
# File 'lib/mandrill_queue/message/recipient/data.rb', line 69

def first
	@_vars.first
end

#lastObject



73
74
75
# File 'lib/mandrill_queue/message/recipient/data.rb', line 73

def last
	@_vars.last
end

#recipient_indexObject



28
29
30
# File 'lib/mandrill_queue/message/recipient/data.rb', line 28

def recipient_index
	@_rcpt_index || clear_recipient_index
end

#register_recipient(email, index) ⇒ Object

Raises:



23
24
25
26
# File 'lib/mandrill_queue/message/recipient/data.rb', line 23

def register_recipient(email, index)
	raise RecipientDataError, "#{email} has already been registered in recipient index." if recipient_index.has_key?(email)
	recipient_index[email] = index
end

#set!(list) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/mandrill_queue/message/recipient/data.rb', line 91

def set!(list)
	@_vars = list.map do |obj|
		var_class.new.set!(obj.symbolize_keys)
	end

	self
end

#to_a(options = {}) ⇒ Object



63
64
65
66
67
# File 'lib/mandrill_queue/message/recipient/data.rb', line 63

def to_a(options = {})
	@_vars.map do |v|
		v.to_hash(options)
	end
end

#validate(errors) ⇒ Object



85
86
87
88
89
# File 'lib/mandrill_queue/message/recipient/data.rb', line 85

def validate(errors)
	@_vars.each do |v|
		v.validate(errors)
	end
end

#var_classObject



40
41
42
# File 'lib/mandrill_queue/message/recipient/data.rb', line 40

def var_class
	self.class.var_class
end