Class: Restful::Serializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/restful/serializer.rb

Overview

New instances of Serializer handle the actual conversion of a model subject into a hash of resource attributes.

Configuration

There are four levels of configuration.

  1. The subject’s base_class configuration (if different than it’s own class configuration)

  2. The subject’s class configuration

  3. Any additional parameters passed into the initialization of the Serializer.

  4. Any configuration performed when the Restful::Configuration::Resource is yielded to a passed block in initialization.

One through three are successively deep_merged. Four allows for complete redefinition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, web_service, options = {}, &block) ⇒ Serializer

Returns a new instance of Serializer.

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/restful/serializer.rb', line 139

def initialize(subject, web_service, options = {}, &block)
  self.subject = subject
  self.web_service = web_service || Restful::Configuration::WebService.new('__stub__')
  raise(ArgumentError, "No web service configuration set.  Received: #{web_service.inspect})") unless web_service.kind_of?(Restful::Configuration::WebService)

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

  self.options = (options || {}).symbolize_keys
  if subject.kind_of?(Array)
    # preserve configure block to pass to array members 
    self.configure_block = block
  else
    _configure(&block)
  end

  self.url_factory = UrlFactory.new(:api_prefix => api_prefix, :default_url_options => default_url_options)
end

Instance Attribute Details

#base_klassObject

Returns the value of attribute base_klass.



132
133
134
# File 'lib/restful/serializer.rb', line 132

def base_klass
  @base_klass
end

#configure_blockObject

Returns the value of attribute configure_block.



132
133
134
# File 'lib/restful/serializer.rb', line 132

def configure_block
  @configure_block
end

#klassObject

Returns the value of attribute klass.



132
133
134
# File 'lib/restful/serializer.rb', line 132

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



132
133
134
# File 'lib/restful/serializer.rb', line 132

def options
  @options
end

#resource_configurationObject

Returns the value of attribute resource_configuration.



133
134
135
# File 'lib/restful/serializer.rb', line 133

def resource_configuration
  @resource_configuration
end

#subjectObject

Returns the value of attribute subject.



132
133
134
# File 'lib/restful/serializer.rb', line 132

def subject
  @subject
end

#url_factoryObject

Returns the value of attribute url_factory.



133
134
135
# File 'lib/restful/serializer.rb', line 133

def url_factory
  @url_factory
end

#web_serviceObject

Returns the value of attribute web_service.



133
134
135
# File 'lib/restful/serializer.rb', line 133

def web_service
  @web_service
end

Instance Method Details

#active_record_serialization_optionsObject



167
168
169
170
171
# File 'lib/restful/serializer.rb', line 167

def active_record_serialization_options
  ar_options = serialization.to_hash(:ignore_empty => true)
  ar_options.delete(:include) if shallow 
  return ar_options
end

#associationsObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/restful/serializer.rb', line 177

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

#hrefObject



193
194
195
196
197
198
199
200
# File 'lib/restful/serializer.rb', line 193

def href 
  unless @href
    @href = url_factory.create(:method => url_for, :args => subject.id) if url_for
    @href = url_factory.create(:resources => klass, :args => subject.id) unless @href
    @href = url_factory.create(:resources => base_klass, :args => subject.id) unless @href || base_klass == klass
  end
  return @href
end

#nameObject



173
174
175
# File 'lib/restful/serializer.rb', line 173

def name
  subject.send(name_method) if subject.respond_to?(name_method)
end

#serializeObject

Encode as a resource hash.



159
160
161
162
163
164
165
# File 'lib/restful/serializer.rb', line 159

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