Class: RoadForest::ContentHandling::Engine::TypeHandlerList

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/content-handling/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ TypeHandlerList

Returns a new instance of TypeHandlerList.



10
11
12
13
14
15
16
# File 'lib/roadforest/content-handling/engine.rb', line 10

def initialize(prefix)
  @prefix = prefix
  @types = MediaTypeList.new
  @handlers = {}
  @type_map = []
  @symbol_lookup = {}
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



17
18
19
# File 'lib/roadforest/content-handling/engine.rb', line 17

def handlers
  @handlers
end

#type_mapObject (readonly)

Returns the value of attribute type_map.



17
18
19
# File 'lib/roadforest/content-handling/engine.rb', line 17

def type_map
  @type_map
end

#typesObject (readonly)

Returns the value of attribute types.



17
18
19
# File 'lib/roadforest/content-handling/engine.rb', line 17

def types
  @types
end

Instance Method Details

#add(handler) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/roadforest/content-handling/engine.rb', line 19

def add(handler)
  type = handler.type
  @types.add(type)
  @handlers[type] = handler
  symbol = handler_symbol(type)
  raise "Type collision: #{type} already in #{self.inspect}" if @symbol_lookup.has_key?(symbol)
  @type_map << [type.content_type_header, symbol]
  @symbol_lookup[symbol] = handler
end

#fetch(symbol, &block) ⇒ Object



33
34
35
# File 'lib/roadforest/content-handling/engine.rb', line 33

def fetch(symbol, &block)
  @symbol_lookup.fetch(symbol, &block)
end

#handler_for(type) ⇒ Object



41
42
43
44
45
46
# File 'lib/roadforest/content-handling/engine.rb', line 41

def handler_for(type)
  type = MediaType.parse(type)
  @handlers.fetch(type)
rescue KeyError
  raise UnrecognizedType, "No Content-Type handler for #{type}"
end

#handler_symbol(type) ⇒ Object



29
30
31
# File 'lib/roadforest/content-handling/engine.rb', line 29

def handler_symbol(type)
  "#{@prefix}_#{type.accept_header.gsub(/\W/, "_")}".to_sym
end

#resetObject



37
38
39
# File 'lib/roadforest/content-handling/engine.rb', line 37

def reset
  @handlers.clear
end