Sha256: 0db7db0688e72ed0d8285ce6b67d6a16f0872eb6e03210dbc74f6a6c6fee813e

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module HawatelPS
  module Linux
    class ProcInfo < ProcControl

      # Process instance with attributes of that process
      # @param proc_attrs [Hash] attributes of the process
      # @return [void]
      def initialize(proc_attrs)
        @proc_attrs = proc_attrs
        define_attributes(proc_attrs)
      end

      # Make attributes of public.
      # Access to process attribute from an object instance by index of array.
      # @example
      #   p = ProcInfo.new(proc_attrs)
      #   puts p['processid']
      def [](key)
        @proc_attrs[key.downcase]
      end

      # Calls the given block once for each element in self, passing that element as a parameter.
      # @param &block
      # @example print all attributes of process
      #   p = ProcInfo.new(proc_attrs)
      #   proc.each {|key, val| puts "#{key} - #{val}"}
      # @return An Enumerator is returned if no block is given.
      def each(&block)
        @proc_attrs.each(&block)
      end

      # @see ProcInfo#define_attributes
      def metaclasses
        class << self; self; end
      end

      private
      # Make attributes of public.
      # Access  to process attribute from an object instance by public method where names of attributes are methods.
      # @example
      #   p = ProcInfo.new(proc_attrs)
      #   puts p.processid
      def define_attributes(hash)
        hash.each_pair do |key, value|
          metaclasses.send(:attr_reader, key.to_sym)
          instance_variable_set("@#{key}", value)
        end
      end


    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hawatel_ps-0.1.2 lib/hawatel_ps/linux/proc_info.rb
hawatel_ps-0.1.1 lib/hawatel_ps/linux/proc_info.rb