Class: Ixtlan::Babel::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/babel/serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_or_models) ⇒ Serializer

Returns a new instance of Serializer.



37
38
39
# File 'lib/ixtlan/babel/serializer.rb', line 37

def initialize(model_or_models)
  @model_or_models = model_or_models
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



45
46
47
# File 'lib/ixtlan/babel/serializer.rb', line 45

def method_missing(method, *args, &block)
  @model_or_models.send(method, *args, &block)
end

Instance Method Details

#add_custom_serializers(map) ⇒ Object



49
50
51
# File 'lib/ixtlan/babel/serializer.rb', line 49

def add_custom_serializers(map)
  filter.add_custom_serializers(map)
end

#idObject



29
30
31
32
33
34
35
# File 'lib/ixtlan/babel/serializer.rb', line 29

def id
  if @model_or_models.is_a? Array
    super
  else
    @model_or_models.id
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ixtlan/babel/serializer.rb', line 41

def respond_to?(method)
  @model_or_models.respond_to?(method)
end

#to_hash(options = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/ixtlan/babel/serializer.rb', line 95

def to_hash(options = nil)
  setup_filter( options )
  if collection?
    @model_or_models.collect do |m|
      filter_model( m )
    end
  else
    filter_model( @model_or_models )
  end
end

#to_json(options = nil) ⇒ Object



106
107
108
# File 'lib/ixtlan/babel/serializer.rb', line 106

def to_json(options = nil)
  to_hash(options).to_json
end

#to_xml(options = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ixtlan/babel/serializer.rb', line 126

def to_xml(options = nil)
  setup_filter

  result = to_hash

  root = config.root

  if root && result.is_a?(Array) && root.respond_to?(:pluralize)
    root = root.pluralize
  end
  result.to_xml :root => root
end

#to_yaml(options = nil) ⇒ Object



139
140
141
# File 'lib/ixtlan/babel/serializer.rb', line 139

def to_yaml(options = nil)
  to_hash(options).to_yaml
end

#use(context_or_options) ⇒ Object



90
91
92
93
# File 'lib/ixtlan/babel/serializer.rb', line 90

def use( context_or_options )
  @context_or_options = context_or_options
  self
end