Class: Restful::Serializer

Inherits:
Object
  • Object
show all
Includes:
ActionController::UrlWriter, UrlForHelpers
Defined in:
lib/restful/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlForHelpers

#get_url_for

Constructor Details

#initialize(subject, *args) ⇒ Serializer

Returns a new instance of Serializer.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/restful/serializer.rb', line 41

def initialize(subject, *args)
  self.subject = subject

  self.base_klass = subject.class.base_class.name.demodulize.underscore if subject.class.respond_to?(:base_class)
  self.klass = subject.class.name.demodulize.underscore

  passed_options = (args.pop || {}).symbolize_keys
  if subject.kind_of?(Array)
    # preserve options as is to be passed to array members
    self.options = passed_options
  else
    deeply_merge = passed_options.delete(:deep_merge)
    base_options = Restful.model_configuration_for(base_klass) || {}
    class_options = Restful.model_configuration_for(klass) || {}
    self.options = (klass == base_klass || class_options[:no_inherited_options]) ? 
      class_options : 
      base_options.merge(class_options)
    self.options.merge!(passed_options)
    self.options.deep_merge(deeply_merge) if deeply_merge
  end

  self.shallow = options[:shallow]
end

Instance Attribute Details

#base_klassObject

Returns the value of attribute base_klass.



39
40
41
# File 'lib/restful/serializer.rb', line 39

def base_klass
  @base_klass
end

#klassObject

Returns the value of attribute klass.



39
40
41
# File 'lib/restful/serializer.rb', line 39

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



39
40
41
# File 'lib/restful/serializer.rb', line 39

def options
  @options
end

#shallowObject

Returns the value of attribute shallow.



39
40
41
# File 'lib/restful/serializer.rb', line 39

def shallow
  @shallow
end

#subjectObject

Returns the value of attribute subject.



39
40
41
# File 'lib/restful/serializer.rb', line 39

def subject
  @subject
end

Instance Method Details

#active_record_serialization_optionsObject



73
74
75
76
77
# File 'lib/restful/serializer.rb', line 73

def active_record_serialization_options
  ar_options = (options[:serialization] || {}).clone
  ar_options.delete(:include) if shallow 
  return ar_options
end

#associationsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/restful/serializer.rb', line 84

def associations
  unless @associations
    @associations = case options[:associations]
      when Array,Hash
        options[:associations].map do |name,assoc|
          Association.new(subject, klass, (assoc.nil? ? name : assoc), name)
        end
      when nil
        []
      else
        [Association.new(subject, klass, options[:associations])]
    end
  end
  return @associations
end

#hrefObject



100
101
102
103
104
105
106
107
# File 'lib/restful/serializer.rb', line 100

def href 
  unless @href
    @href = get_url_for(:method => options[:url_for], :args => subject.id) if options.include?(:url_for)
    @href = get_url_for(:resources => klass, :args => subject.id) unless @href
    @href = get_url_for(:resources => base_klass, :args => subject.id) unless @href || base_klass == klass
  end
  return @href
end

#nameObject



79
80
81
82
# File 'lib/restful/serializer.rb', line 79

def name
  name_method = options[:name] || :name
  subject.send(name_method) if subject.respond_to?(name_method)
end

#serializeObject



65
66
67
68
69
70
71
# File 'lib/restful/serializer.rb', line 65

def serialize
  case 
    when subject.respond_to?(:attribute_names) then _serialize_active_record
    when subject.kind_of?(Array) then _serialize_array
    else ActiveSupport::JSON.decode(subject.to_json) # just capture the hash of the object structure
  end
end