Class: Teamspeak::Association

Inherits:
Array
  • Object
show all
Defined in:
lib/teamspeak.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, options = {}) ⇒ Association

Returns a new instance of Association.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/teamspeak.rb', line 13

def initialize(array, options = {})
	# I'm not making any elaborate tries to be fancy here now, so we just assume the association
	# is given an array of only one type of object, of which we'll take the first to inject finder methods.
	
	# object = array.first
	# object.attributes.each {|k,v|
	# 	self.class.class_eval <<-EOF
	# 	def find_by_#{k.to_s.downcase}
	# 		self.find{|e| e.respond_to?(:attributes) && e.attributes[:#{k}] && e.attributes[:#{k}] == value}
	# 	end
	# 	EOF
	# }

	if options[:parent]
		case options[:parent].class
			when Teamspeak::Server
				array.each{|i| i.server = options[:parent]; i.parent = options[:parent]}
			else
				array.each{|i| i.parent = options[:parent]}
		end
		@parent = options[:parent]
	end

	if options[:class]
		@obj_class = options[:class]
	end
	
	array_initialize(array)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object


43
44
45
46
47
48
# File 'lib/teamspeak.rb', line 43

def method_missing(method, *args)
	if find_attr = method.to_s.scan(/^find_by_(.*)/)
		find_attr = find_attr.to_s.to_sym
		return self.find{|e| e.attributes[find_attr] == args.first.to_s} if self.first.respond_to?(:attributes) && self.first.attributes[find_attr]
	end
end

Instance Attribute Details

#obj_classObject

Returns the value of attribute obj_class


10
11
12
# File 'lib/teamspeak.rb', line 10

def obj_class
  @obj_class
end

#parentObject

Returns the value of attribute parent


10
11
12
# File 'lib/teamspeak.rb', line 10

def parent
  @parent
end

Instance Method Details

#array_initializeObject


12
# File 'lib/teamspeak.rb', line 12

alias_method :array_initialize, :initialize

#create(attr = {}) ⇒ Object


50
51
52
53
# File 'lib/teamspeak.rb', line 50

def create(attr = {})
	attr[:parent] ||= parent
	@obj_class.create(attr)
end