Module: ModelSerializer::ClassMethods

Defined in:
lib/model_serializer.rb

Instance Method Summary collapse

Instance Method Details

#json(*options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/model_serializer.rb', line 21

def json(*options)
	options.unshift(:id) if !options.include?(:id) # Insert :id if not inserted
	# options << :db if !options.include?(:db) # Insert :db if not inserted
	self.json_options = options
  class_eval do
    def json(rooted=true, *args)
    	result = {}
    	
    	self.class.json_options.each do |key|
    		result[key] = eval("self.#{key}")
    	end

      args.each do |key|
        result[key] = eval("self.#{key}")
      end

    	if rooted
        wrapper = {}
        wrapper[self.class.name.underscore] = result
        wrapper
      else
        result
      end
    end
  end
end

#json_optionsObject



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

def json_options
  @json_options
end

#json_options=(v) ⇒ Object



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

def json_options=(v)
  @json_options = v
end