Class: Restful::Serializer
- Inherits:
-
Object
- Object
- Restful::Serializer
- 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.
-
The subject’s base_class configuration (if different than it’s own class configuration)
-
The subject’s class configuration
-
Any additional parameters passed into the initialization of the Serializer.
-
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
-
#base_klass ⇒ Object
Returns the value of attribute base_klass.
-
#configure_block ⇒ Object
Returns the value of attribute configure_block.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource_configuration ⇒ Object
Returns the value of attribute resource_configuration.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#url_factory ⇒ Object
Returns the value of attribute url_factory.
-
#web_service ⇒ Object
Returns the value of attribute web_service.
Instance Method Summary collapse
- #active_record_serialization_options ⇒ Object
- #associations ⇒ Object
- #href ⇒ Object
-
#initialize(subject, web_service, options = {}, &block) ⇒ Serializer
constructor
A new instance of Serializer.
- #name ⇒ Object
-
#serialize ⇒ Object
Encode as a resource hash.
Constructor Details
#initialize(subject, web_service, options = {}, &block) ⇒ Serializer
Returns a new instance of Serializer.
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, = {}, &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. = ( || {}).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 => ) end |
Instance Attribute Details
#base_klass ⇒ Object
Returns the value of attribute base_klass.
132 133 134 |
# File 'lib/restful/serializer.rb', line 132 def base_klass @base_klass end |
#configure_block ⇒ Object
Returns the value of attribute configure_block.
132 133 134 |
# File 'lib/restful/serializer.rb', line 132 def configure_block @configure_block end |
#klass ⇒ Object
Returns the value of attribute klass.
132 133 134 |
# File 'lib/restful/serializer.rb', line 132 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
132 133 134 |
# File 'lib/restful/serializer.rb', line 132 def @options end |
#resource_configuration ⇒ Object
Returns the value of attribute resource_configuration.
133 134 135 |
# File 'lib/restful/serializer.rb', line 133 def resource_configuration @resource_configuration end |
#subject ⇒ Object
Returns the value of attribute subject.
132 133 134 |
# File 'lib/restful/serializer.rb', line 132 def subject @subject end |
#url_factory ⇒ Object
Returns the value of attribute url_factory.
133 134 135 |
# File 'lib/restful/serializer.rb', line 133 def url_factory @url_factory end |
#web_service ⇒ Object
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_options ⇒ Object
167 168 169 170 171 |
# File 'lib/restful/serializer.rb', line 167 def = serialization.to_hash(:ignore_empty => true) .delete(:include) if shallow return end |
#associations ⇒ Object
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 |
#href ⇒ Object
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 |
#name ⇒ Object
173 174 175 |
# File 'lib/restful/serializer.rb', line 173 def name subject.send(name_method) if subject.respond_to?(name_method) end |
#serialize ⇒ Object
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 |