Class: Moc::Protocol::Symbol

Inherits:
Object
  • Object
show all
Extended by:
Type
Defined in:
lib/moc/protocol/symbol.rb

Direct Known Subclasses

Command, Event, State

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Type

read, unpack

Constructor Details

#initialize(value) ⇒ Symbol

Returns a new instance of Symbol.



24
25
26
27
28
29
30
# File 'lib/moc/protocol/symbol.rb', line 24

def initialize (value)
	@internal = value.respond_to?(:to_i) ? self.class.codes.key(value.to_i) : value.to_sym.upcase

	unless @internal && self.class.codes.member?(@internal)
		raise ArgumentError, "invalid symbol #{value} for #{self.class.name}"
	end
end

Class Method Details

.codesObject



20
21
22
# File 'lib/moc/protocol/symbol.rb', line 20

def self.codes
	raise 'no code specification'
end

.read(io) ⇒ Object



16
17
18
# File 'lib/moc/protocol/symbol.rb', line 16

def self.read (io)
	new(Integer.read(io))
end

Instance Method Details

#==(value) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/moc/protocol/symbol.rb', line 32

def == (value)
	return true if super

	return true if value.respond_to?(:to_i) && to_i == value.to_i

	return true if value.respond_to?(:to_sym) && to_sym == value.to_sym.upcase

	false
end

#inspectObject



54
55
56
# File 'lib/moc/protocol/symbol.rb', line 54

def inspect
	"#<#{self.class.name}: #{to_s}>"
end

#packObject



58
59
60
# File 'lib/moc/protocol/symbol.rb', line 58

def pack
	Integer.new(to_i).pack
end

#to_iObject



42
43
44
# File 'lib/moc/protocol/symbol.rb', line 42

def to_i
	self.class.codes[to_sym]
end

#to_sObject



50
51
52
# File 'lib/moc/protocol/symbol.rb', line 50

def to_s
	to_sym.to_s
end

#to_symObject



46
47
48
# File 'lib/moc/protocol/symbol.rb', line 46

def to_sym
	@internal
end