Class: Serialize

Inherits:
Object
  • Object
show all
Defined in:
lib/serialize.rb,
lib/serialize/xml.rb,
lib/serialize/json.rb,
lib/serialize/format.rb,
lib/serialize/version.rb,
lib/serialize/generator.rb

Defined Under Namespace

Classes: Format, Generator, Json, Xml

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Serialize

Returns a new instance of Serialize.



7
8
9
10
11
# File 'lib/serialize.rb', line 7

def initialize(object, options={})
  key     = options[:as] || :default
  @object = object
  @block  = self.class.structures[key]
end

Class Method Details

.register(content_type, klass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/serialize.rb', line 31

def self.register(content_type, klass)
  @@serializers ||= {}
  @@serializers[content_type] = klass
  subtype = content_type.split("/").last

  define_method "to_#{subtype}" do |*args|
    @@serializers[content_type].new(@object, @block).render(*args)
  end

  send :alias_method, "as_#{subtype}", "to_#{subtype}"
end

.structure(key = nil, &block) ⇒ Object



22
23
24
25
# File 'lib/serialize.rb', line 22

def self.structure(key=nil, &block)
  key ||= :default
  structures[key] = block
end

.structuresObject



27
28
29
# File 'lib/serialize.rb', line 27

def self.structures
  @structures ||= {}
end

Instance Method Details

#responsesObject



13
14
15
# File 'lib/serialize.rb', line 13

def responses
  @@serializers.keys
end

#to(content_type) ⇒ Object



17
18
19
20
# File 'lib/serialize.rb', line 17

def to(content_type)
  subtype = content_type.split("/").last
  send("to_#{subtype}")
end