Class: Leadlight::TypeMap

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

Defined Under Namespace

Classes: DefaultType, Mapping

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TypeMap

Returns a new instance of TypeMap.



13
14
15
16
17
18
19
# File 'lib/leadlight/type_map.rb', line 13

def initialize(options={})
  options.each do |key, value|
    send key, value
  end
  @types = []
  add_default_types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



11
12
13
# File 'lib/leadlight/type_map.rb', line 11

def types
  @types
end

Instance Method Details

#add(enctype_pattern, object_pattern, type) ⇒ Object



21
22
23
# File 'lib/leadlight/type_map.rb', line 21

def add(enctype_pattern, object_pattern, type)
  types.unshift(Mapping.new(enctype_pattern, object_pattern, type))
end

#to_entity_body(object, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/leadlight/type_map.rb', line 25

def to_entity_body(object, options={})
  types.detect(handle_unknown_object_type(object)) {|t|
    t.match_for_object?(object) 
  }.encode(object, options)
end

#to_native(content_type, entity_body, options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/leadlight/type_map.rb', line 31

def to_native(content_type, entity_body, options={})
  types.detect(handle_unknown_enctype(content_type)) {|t| 
    t.match_for_enctype?(content_type) 
  }.decode(content_type, entity_body, options)
end