Class: Raptcha::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/raptcha.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV, env = ENV) ⇒ CLI

Returns a new instance of CLI.



403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/raptcha.rb', line 403

def initialize(argv = ARGV, env = ENV)
  @argv = argv.map{|arg| arg.dup}
  @env = env.to_hash.dup

  @argv, kvs = @argv.partition{|arg| arg !~ /[=:]/}

  @opts = {}
  kvs.each do |kv|
    k, v = kv.split(/[=:]/)
    @opts.update(k.strip => v.strip)
  end

  @mode = @argv.shift || 'help'
end

Class Method Details

.runObject



399
400
401
# File 'lib/raptcha.rb', line 399

def CLI.run
  new.run
end

Instance Method Details

#generateObject



439
440
441
442
# File 'lib/raptcha.rb', line 439

def generate
  what = @argv.shift
  send("generate_#{ what }")
end

#generate_controllerObject



450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/raptcha.rb', line 450

def generate_controller
  src = DATA.read.strip

  rails_root? do
    path = File.join 'app', 'controllers', 'raptcha_controller.rb'
    if test(?e, path)
      puts "exists #{ path }"
      exit 1
    end
    open(path, 'w'){|fd| fd.puts(src)}
    puts "#{ path }"
  end
end

#generate_libObject



464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/raptcha.rb', line 464

def generate_lib
  src = IO.read(__FILE__).strip

  rails_root? do
    path = File.join('lib', 'raptcha.rb')
    if test(?e, path)
      puts "exists #{ path }"
      exit 1
    end
    open(path, 'w'){|fd| fd.puts(src)}
    puts "#{ path }"
  end
end

#helpObject



422
423
424
425
# File 'lib/raptcha.rb', line 422

def help
  STDERR.puts(README)
  exit(42)
end

#imageObject



427
428
429
430
431
432
433
434
435
436
437
# File 'lib/raptcha.rb', line 427

def image
  io = @argv.shift || STDOUT
  opened = false
  unless io.respond_to?(:write)
    io = open(io, 'w')
    opened = true
  end
  io.write(Raptcha.image(@opts))
ensure
  io.close if opened
end

#rails_root?(&block) ⇒ Boolean

Returns:

  • (Boolean)


444
445
446
447
448
# File 'lib/raptcha.rb', line 444

def rails_root?(&block)
  boolean = test(?d, 'app') && test(?d, 'app/controllers')
  return boolean unless block
  boolean ? block.call() : abort('run this in a RAILS_ROOT')
end

#runObject



418
419
420
# File 'lib/raptcha.rb', line 418

def run
  respond_to?(@mode) ? send(@mode) : send(:help)
end