Sha256: 6ba98f9dea56fc5a056a03b3e604d1fbd8e52f7f64ffd1a18aa6ceaff882d0c3

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'active_remote/serializers/json'

module ActiveRemote
  module Serialization
    def self.included(klass)
      klass.class_eval do
        include ::ActiveRemote::Serializers::JSON
      end
    end

    # Examine the given response and add any errors to our internal errors
    # list. If no response is given, use the last response.
    #
    def add_errors_from_response(response=self.last_response)
      return unless response.respond_to?(:errors)

      response.errors.each do |error|
        if error.respond_to?(:message)
          errors.add(error.field, error.message)
        elsif error.respond_to?(:messages)
          error.messages.each do |message|
            errors.add(error.field, message)
          end
        end
      end
    end

    # Examine the last response and serialize any records returned into Active
    # Remote objects.
    #
    def serialize_records
      return nil unless last_response.respond_to?(:records)

      last_response.records.map do |remote_record|
        record = self.class.allocate
        record.instantiate(remote_record.to_hash)
        record
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_remote-1.8.1 lib/active_remote/serialization.rb
active_remote-1.8.0 lib/active_remote/serialization.rb
active_remote-1.8.0.rc1 lib/active_remote/serialization.rb
active_remote-1.7.1 lib/active_remote/serialization.rb