<commit_before>=begin rdoc

= RUNIT.RB

*Author*::      <NAME> <<EMAIL>>
*Since*::       2013-04-23
*Copyright*::   (c) 2013 Tamara Temple Web Development
*License*::     MIT
  
== Description

A simple class to wrap Open3 to execute a command and return the result, stdout and stderr

== Synopsis

    require 'RunIt'

    runner = RunIt.new "/bin/ls"
    runner.run
    puts runner.output

=end

require 'open3'
require 'ostruct'

class RunIt

  # Version 
  VERSION = "1.0.0"

  # cmd:: set/get the command to execute
  attr_accessor :cmd

  # input:: set/get the input for the command
  attr_accessor :input

  # output:: get the output from the command
  attr_reader :output

  # error:: get the error messages from the command
  attr_reader :error

  # result:: the result of the command as a Process::Status object
  attr_reader :result


  # Class function to tell if mocking is turned on
  def self.mock?
    @@mock
  end

  # Class method to turn on mocking
  def self.mock!
    @@mock = true
  end

  # Class method to turn off mocking
  def self.unmock!
    @@mock = false
  end
  
  # Initialize the RunIt object
  #
  # cmd:: (required) [String] - the full command line to execute
  # input:: (optional) [String] - input to the command, if any
  def initialize(cmd, input=nil)
    self.cmd = cmd
    self.input = input
    self.result = nil
    @@mock = false
  end

  # Execute the specified command
  #
  # If mocking is on, only return the command as the output, and set success to true
  def run

    if @@mock

      self.result = OpenStruct.new(:success? => true, :exitstatus => 0)
      self.output = "Command entered: #{self.cmd}"
      self.error  = ''
      return true

    else

      self.output = ''
      self.error  = ''
      self.result = nil

      begin

        Open3.popen3(self.cmd) do |stdin, stdout, stderr, wait|
          stdin.puts self.input unless self.input.nil?
          stdin.close
          until stdout.eof?
            self.output <<  stdout.gets
          end
          until stderr.eof?
            self.error <<  stderr.gets
          end
          self.result = wait.value
        end

      rescue Exception => e

        self.result = OpenStruct.new(:success? => false, :exitstatus => -1)
        self.error << "#{cmd} raised an error: #{e.class}:#{e}"

      end

      self.result.success?

    end

  end

  # Whether the command completed successfully
  def success?
    self.result.success?
  end

  # The exit status of the command
  def exitstatus
    self.result.exitstatus
  end

  # Instance method to tell if mocking is turned on
  def mock?
    @@mock
  end
<commit_msg>resolve internal conflict between attr_reader and use of self<commit_after>=begin rdoc

= RUNIT.RB

*Author*::      <NAME> <<EMAIL>>
*Since*::       2013-04-23
*Copyright*::   (c) 2013 Tamara Temple Web Development
*License*::     MIT
  
== Description

A simple class to wrap Open3 to execute a command and return the result, stdout and stderr

== Synopsis

    require 'RunIt'

    runner = RunIt.new "/bin/ls"
    runner.run
    puts runner.output

=end

require 'open3'
require 'ostruct'

class RunIt

  # Version 
  VERSION = "1.0.0"

  # cmd:: set/get the command to execute
  attr_accessor :cmd

  # input:: set/get the input for the command
  attr_accessor :input

  # output:: get the output from the command
  attr_reader :output

  # error:: get the error messages from the command
  attr_reader :error

  # result:: the result of the command as a Process::Status object
  attr_reader :result


  # Class function to tell if mocking is turned on
  def self.mock?
    @@mock
  end

  # Class method to turn on mocking
  def self.mock!
    @@mock = true
  end

  # Class method to turn off mocking
  def self.unmock!
    @@mock = false
  end
  
  # Initialize the RunIt object
  #
  # cmd:: (required) [String] - the full command line to execute
  # input:: (optional) [String] - input to the command, if any
  def initialize(cmd, input=nil)
    self.cmd = cmd
    self.input = input
    @result = nil
    @@mock = false
  end

  # Execute the specified command
  #
  # If mocking is on, only return the command as the output, and set success to true
  def run

    if @@mock

      @result = OpenStruct.new(:success? => true, :exitstatus => 0)
      @output = "Command entered: #{self.cmd}"
      @error  = ''
      return true

    else

      @output = ''
      @error  = ''
      @result = nil

      begin

        Open3.popen3(self.cmd) do |stdin, stdout, stderr, wait|
          stdin.puts self.input unless self.input.nil?
          stdin.close
          until stdout.eof?
            @output <<  stdout.gets
          end
          until stderr.eof?
            @error <<  stderr.gets
          end
          @result = wait.value
        end

      rescue Exception => e

        @result = OpenStruct.new(:success? => false, :exitstatus => -1)
        @error << "#{cmd} raised an error: #{e.class}:#{e}"

      end

      self.result.success?

    end

  end

  # Whether the command completed successfully
  def success?
    self.result.success?
  end

  # The exit status of the command
  def exitstatus
    self.result.exitstatus
  end

  # Instance method to tell if mocking is turned on
  def mock?
    @@mock
  end

{'fraction_non_alphanumeric': 0.09549071618037135, 'fraction_numerical': 0.007957559681697613, 'mean_word_length': 2.31803896920176, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4171899', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1606, 'n_words': 582}