Class: Protocol::Media::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/media/map.rb

Overview

Maps media types and ranges to objects using type/subtype compatibility.

Instance Method Summary collapse

Constructor Details

#initializeMap

Initialize an empty media map.



13
14
15
# File 'lib/protocol/media/map.rb', line 13

def initialize
	@entries = {}
end

Instance Method Details

#[](range) ⇒ Object

Find the object matching a media type or range.

Exact type/subtype registrations take priority, followed by the first compatible registration.



33
34
35
36
37
# File 'lib/protocol/media/map.rb', line 33

def [](range)
	if entry = lookup(Range.for(range))
		entry.last
	end
end

#[]=(range, object) ⇒ Object

Associate a media type or range with an object.



21
22
23
24
25
# File 'lib/protocol/media/map.rb', line 21

def []=(range, object)
	range = Range.for(range)
	
	@entries[name(range)] = [range, object]
end

#for(ranges) ⇒ Object

Find the first object matching an ordered sequence of media ranges.



43
44
45
46
47
48
49
50
51
# File 'lib/protocol/media/map.rb', line 43

def for(ranges)
	ranges.each do |range|
		if entry = lookup(Range.for(range))
			return [entry.last, range]
		end
	end
	
	return nil
end

#freezeObject

Freeze the map and its internal entries.



56
57
58
59
60
61
62
63
# File 'lib/protocol/media/map.rb', line 56

def freeze
	unless frozen?
		@entries.each_value(&:freeze)
		@entries.freeze
	end
	
	super
end