Class: Protocol::Media::Map

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

Overview

An immutable map from concrete media types to objects for range-based lookup.

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Map

Initialize a new media map with a given index.

The index is retained without copying or freezing it.



73
74
75
# File 'lib/protocol/media/map.rb', line 73

def initialize(index)
	@index = index
end

Class Method Details

.buildObject

Construct an immutable map using a builder.



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

def self.build
	builder = Builder.new
	
	if block_given?
		yield builder
	end
	
	return builder.build
end

.for(entries) ⇒ Object

Convert keyed entries into a media map.

Existing maps are returned unchanged, including their frozen state.



58
59
60
61
62
63
64
65
66
# File 'lib/protocol/media/map.rb', line 58

def self.for(entries)
	return entries if entries.instance_of?(self)
	
	return build do |builder|
		entries.each do |media_type, object|
			builder[media_type] = object
		end
	end
end

Instance Method Details

#[](media_range) ⇒ Object

Find the object matching a media type or range.



91
92
93
# File 'lib/protocol/media/map.rb', line 91

def [](media_range)
	return lookup(Range.for(media_range))
end

#for(media_ranges) ⇒ Object

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



99
100
101
102
103
104
105
106
107
# File 'lib/protocol/media/map.rb', line 99

def for(media_ranges)
	media_ranges.each do |media_range|
		if object = lookup(Range.for(media_range))
			return [object, media_range]
		end
	end
	
	return nil
end

#freezeObject

Freeze the media map and its index.



79
80
81
82
83
84
85
# File 'lib/protocol/media/map.rb', line 79

def freeze
	return self if frozen?
	
	@index.freeze
	
	return super
end