Sha256: d3127413fb9d3c4696de153a27af286e54a753165c188b2050025da2d3fb8a2c

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

module Crystal
  module Processors    
    class RemoteCaller < Processor
      # TODO2 :remote isn't registered with crystal[
      inject :remote => :remote
      
      def initialize next_processor, result_variable = 'content'
        super(next_processor)
        @result_variable = result_variable
      end
      
      def call                
        return next_processor.call unless workspace.class?
                
        # prepare
        klass = workspace.class
        raise "The remote class #{klass} must be a Crystal::Remote!" unless klass.is? Crystal::Remote
        workspace.remote_object = klass.new                  
        method = workspace.method_name

        # call
        begin
          result = workspace.remote_object.run_callbacks :action, :method => method do
            workspace.remote_object.send method
          end
      
          ensure_correct_result! result
          workspace.remote_result = result                        

          next_processor.call
  
          # write JSON as a result if format is JSON and no one else filled it
          if workspace[@result_variable].blank?
            workspace[@result_variable] = workspace.remote_result.to_json
          end              
        rescue StandardError => e
          raise e if !workspace.params.format.json? or config.test?
      
          workspace[@result_variable] = {:error => e.message}.to_json
  
          logger.error e
          logger.info "\n"
        end
      end
      
      private
        def ensure_correct_result! result
          unless result.rson?
            raise "You can't use object of type '#{result.class}' as Remote's return value!"
          end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
crystal-0.0.13 lib/crystal/remote/processors/remote_caller.rb
crystal-0.0.12 lib/crystal/remote/processors/remote_caller.rb
crystal_ext-0.0.11 lib/crystal/remote/processors/remote_caller.rb