Sha256: b6a2b0d4d895d57809ec0a66b5652992aa9f55a182020ad998f8f268d28f5236

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Protobuf
  module Rpc
    class Env < Hash
      # Creates an accessor that simply sets and reads a key in the hash:
      #
      #   class Config < Hash
      #     hash_accessor :app
      #   end
      #
      #   config = Config.new
      #   config.app = Foo
      #   config[:app] #=> Foo
      #
      #   config[:app] = Bar
      #   config.app #=> Bar
      #
      def self.hash_accessor(*names) #:nodoc:
        names.each do |name|
          name_str = name.to_s.freeze

          define_method name do
            self[name_str]
          end

          define_method "#{name}=" do |value|
            self[name_str] = value
          end

          define_method "#{name}?" do
            !self[name_str].nil?
          end
        end
      end

      # TODO: Add extra info about the environment (i.e. variables) and other
      # information that might be useful
      hash_accessor :client_host,
                    :encoded_request,
                    :encoded_response,
                    :log_signature,
                    :method_name,
                    :parent_env,
                    :request,
                    :request_type,
                    :response,
                    :response_type,
                    :rpc_method,
                    :rpc_service,
                    :service_name,
                    :worker_id

      def initialize(options = {})
        merge!(options)

        self['worker_id'] = ::Thread.current.object_id.to_s(16)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
protobuffy-4.0.1 lib/protobuf/rpc/env.rb
protobuffy-4.0.0 lib/protobuf/rpc/env.rb