Class: Teamspeak::Base

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

Direct Known Subclasses

Channel, Client, Group, Server, Token

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}) ⇒ Base

Returns a new instance of Base


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/teamspeak.rb', line 60

def initialize(attr = {})
	@parent = attr.delete(:parent)

	# We set API functions based on the classname, because they're pretty straight forward mostly
	shortname = self.class.to_s.split('::').last.downcase
	@api = {
		:list => "#{shortname}list",
		:add => "#{shortname}add",
		:delete => "#{shortname}delete"
	}

	@attributes = attr
	@attributes.replace(self.class.defaults.merge(@attributes)) if self.class.defaults # reverse_merge!

	puts self.class.defaults.inspect
	puts @attributes.inspect

	attr.each {|attribute, value|
		value.gsub!('\s', ' ') if value && value.is_a?(String) && !value.empty?
		instance_variable_set("@#{attribute}", value.to_i.to_s == value ? value.to_i : value)

		self.class.class_eval <<-EOF
			def #{attribute}
				instance_variable_get("@#{attribute}")
			end

			def #{attribute}=(v)
				instance_variable_set("@#{attribute}", v)
			end
		EOF
	}
end

Class Attribute Details

.defaultsObject

Returns the value of attribute defaults


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

def defaults
  @defaults
end

Instance Attribute Details

#apiObject

Returns the value of attribute api


57
58
59
# File 'lib/teamspeak.rb', line 57

def api
  @api
end

#attributesObject

Returns the value of attribute attributes


57
58
59
# File 'lib/teamspeak.rb', line 57

def attributes
  @attributes
end

#parentObject

Returns the value of attribute parent


57
58
59
# File 'lib/teamspeak.rb', line 57

def parent
  @parent
end

#serverObject

Returns the value of attribute server


57
58
59
# File 'lib/teamspeak.rb', line 57

def server
  @server
end

Class Method Details

.create(attr = {}) ⇒ Object


107
108
109
110
# File 'lib/teamspeak.rb', line 107

def create(attr = {})
	object = new(attr)
	return object if object.save
end

Instance Method Details

#cmd(command, *args) ⇒ Object


102
103
104
# File 'lib/teamspeak.rb', line 102

def cmd(command, *args)
	parent.cmd(command, args[0])
end

#saveObject


97
98
99
100
# File 'lib/teamspeak.rb', line 97

def save
	ret = cmd(api[:add], attributes)
	update_attributes(ret) if ret.is_a?(Hash)
end

#update_attributes(attr = {}) ⇒ Object


93
94
95
# File 'lib/teamspeak.rb', line 93

def update_attributes(attr = {})
	@attributes.replace(@attributes.merge(attr))
end