Sha256: c0ef2e099405f17d4b9deaf46ba64a686d8ea048f01b1b3285f269f671c55bd6

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module ActiveRecord
  module Acts
    module ShellscriptExecutable
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def acts_as_shellscript_executable(options = {})
          @@configuration = { script: :script, stdout: nil }
          @@configuration.update(options) if options.is_a?(Hash)

          class_eval <<-EOV
            include ::ActiveRecord::Acts::ShellscriptExecutable::InstanceMethods

          EOV
        end

        def configuration
          @@configuration
        end
      end

      module InstanceMethods
        def execute!
          script = configuration[:script]
          stdout = configuration[:stdout]
          if configuration[:fork]
            fork { execute(script, stdout) }
            # for test
            Process.wait if ENV['test']
          else
            execute(script, stdout)
          end
        end

        private
        def configuration
          self.class.configuration
        end

        def execute(script, stdout)
          retval = %x(#{send script})
          if stdout && respond_to?("#{stdout}=".to_sym)
            send("#{stdout}=".to_sym, retval)
          end
          save!
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_shellscript_executable-0.0.0 lib/acts_as_shellscript_executable/active_record/acts/shellscript_executable.rb