Class: MandrillQueue::Variables::Internal

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_queue/variables.rb

Instance Method Summary collapse

Constructor Details

#initialize(values = nil, &block) ⇒ Internal

Returns a new instance of Internal.



22
23
24
25
26
# File 'lib/mandrill_queue/variables.rb', line 22

def initialize(values = nil, &block)
	@_variables = values if values.is_a?(Hash)
	@_variables ||= {}
	dsl(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mandrill_queue/variables.rb', line 83

def method_missing(method, *args, &block)
	if method.to_s.end_with?('=')
		method = method.to_s.chomp('=').to_sym
	end

	if args.count > 0
		@_variables[method] = args.first
		self
	else
		raise VariableNotSetError, "#{method} has not been set." \
			unless @_variables.has_key?(method)
		@_variables[method]
	end
end

Instance Method Details

#[](key) ⇒ Object



55
56
57
# File 'lib/mandrill_queue/variables.rb', line 55

def [](key)
	@_variables[key]
end

#[]=(key, value) ⇒ Object



59
60
61
# File 'lib/mandrill_queue/variables.rb', line 59

def []=(key, value)
	@_variables[key.to_sym] = value
end

#merge!(hash) ⇒ Object



79
80
81
# File 'lib/mandrill_queue/variables.rb', line 79

def merge!(hash)
	@_variables.merge!(hash)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/mandrill_queue/variables.rb', line 30

def respond_to?(method)
	super || @_variables.has_key?(method)
end

#set!(hash, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mandrill_queue/variables.rb', line 63

def set!(hash, options = {})
	case hash
	when Hash
       @_variables = hash.to_hash.symbolize_keys
	when Array
		options[:name_key] ||= :name
		options[:content_key] ||= :content
		@_variables = {}
		hash.dup.each do |obj|
			obj.symbolize_keys!
			@_variables[obj[options[:name_key]].to_sym] = obj[options[:content_key]]
		end
	end
	self
end

#to_hash(options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/mandrill_queue/variables.rb', line 34

def to_hash(options = {})
	if options.has_key?(:include_nils) && options[:include_nils]
		@_variables
	else
		@_variables.reject { |k, v| v.nil? }
	end
end

#to_key_value_array(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mandrill_queue/variables.rb', line 42

def to_key_value_array(options = {})
	options[:name_key] ||= :name
	options[:content_key] ||= :content

	result = []
	@_variables.each do |k, v|
		if !v.nil? || options[:include_nils]
			result.push({options[:name_key] => k.to_s, options[:content_key] => v})
		end
	end unless @_variables.nil?
	result
end