class InstaCLI::CLI

Instant CLI - just add Objects!

Public Class Methods

new(**mapping) click to toggle source
# File lib/instacli/cli.rb, line 18
def initialize(**mapping)
  @mapping = mapping
  self
end

Public Instance Methods

execute(*args) click to toggle source
# File lib/instacli/cli.rb, line 67
def execute(*args)
  return STDERR.puts help(*args[1..-1]) if %w(--help -h).include?(args.first)

  o, m, *rest = *args

  begin
    STDOUT.puts method(o, m).call(*rest)
  rescue *rescues
    STDERR.puts help(*args)
  end
end
help(*) click to toggle source
# File lib/instacli/cli.rb, line 63
def help(*)
  ''
end
method(o, m) click to toggle source
# File lib/instacli/cli.rb, line 51
def method(o, m)
  raise ArgumentError unless method? o, m
  object(o).method(m)
end
method?(o, m) click to toggle source
# File lib/instacli/cli.rb, line 46
def method?(o, m)
  methods(o).include? m.to_sym
end
methods(o) click to toggle source
# File lib/instacli/cli.rb, line 39
def methods(o)
  (object(o).methods - Object.methods).reject do |m|
    m.to_s.start_with? '__contracts_ruby_original_'
  end
end
object(o) click to toggle source
# File lib/instacli/cli.rb, line 34
def object(o)
  @mapping.fetch(o.to_sym) { Object.new }
end
object?(o) click to toggle source
# File lib/instacli/cli.rb, line 29
def object?(o)
  object.include? o.to_sym
end
objects() click to toggle source
# File lib/instacli/cli.rb, line 24
def objects
  @mapping.keys
end
parameters(o, m) click to toggle source
# File lib/instacli/cli.rb, line 57
def parameters(o, m)
  method(o, m)
    .parameters
    .map { |p| p.first == :req ? "<#{p.last}>" : "[#{p.last}]" }
end
rescues() click to toggle source
# File lib/instacli/cli.rb, line 13
def rescues
  [ArgumentError]
end