class GrassGis::Module

Generate and execute GRASS commands

r = GrassGis::Module.new('r')
r.resamp.stats '-n', input: "map1@mapset1", output: "map2"

To execute a command without arguments, run must be invoked explicitly:

g = GrassGis::Module.new('g')
g.region.run

Public Class Methods

new(id, options = {}) click to toggle source
# File lib/grassgis/module.rb, line 14
def initialize(id, options = {})
  @id = id.to_s
  @parent = options[:parent]
  @context = options[:context]
end

Public Instance Methods

method_missing(method, *args) click to toggle source
# File lib/grassgis/module.rb, line 62
def method_missing(method, *args)
  m = Module.new(method, parent: self, context: @context)
  if args.size > 0
    m.run *args
  else
    m
  end
end
name() click to toggle source
# File lib/grassgis/module.rb, line 20
def name
  if @parent
    "#{@parent.name}.#{@id}"
  else
    @id
  end
end
run(*args) click to toggle source

Executes the command (with given arguments) returns a SysCmd object (with status, status_value, output, error_output methods)

# File lib/grassgis/module.rb, line 30
def run(*args)
  stdin = nil
  cmd = SysCmd.command name do
    args.each do |arg|
      case arg
      when Hash
        arg.each do |key, value|
          next if value.nil?
          case value
          when Array
            value = value*","
          when String
            if value.include?("\n")
              raise "Cannot pass multiple options through STDIN" if stdin
              stdin = Support.unindent(value)
              value = "-"
              input stdin
            end
          end
          option key.to_s, equal_value: value
        end
      else
        option arg
      end
    end
  end
  if @context
    @context.execute cmd
  end
  cmd
end