Sha256: 730c0c636d60b316007b08785097cca67a08546433eb6eec7ce8c61005d527da

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

require_relative 'options'

module Idonethis::UseCases
  class App
    class << self
      def apply(argv={}, adapters={})
        args = Options.parse(argv)
      
        command,*rest = argv

        command = "help" unless command

        log      = args[:verbose] == true ? adapters[:log] : ->(_){} 
        internet = adapters[:internet] 
        views    = adapters[:views]    || fail("You need to supply the :views adapter")
        settings = adapters[:settings] || fail("You need to supply the :settings adapters")
        
        args.merge!({ opts: rest, log: log, internet: internet, view: views[:list]})

        log.call "args: #{args}, command: #{command}, rest: #{rest}"
        
        use_case = choose command.to_sym, adapters, views, rest 

        unless use_case
          log.call "No command <#{command.to_sym}> found"
          return
        end

        use_case.call settings.credential, args
      end
      
      private 
      
      def choose(command, adapters, views, opts)
       use_cases = {
          list:   List.method(:apply),
          new:    New.method(:apply),
          config: Config.method(:apply),
          git:    ->(_, args) { Git.apply(args.merge(git: adapters[:git], view: ->(msg) { puts msg }, fs: adapters[:fs]))},
          help:   ->(credential, args) do 
            puts ""
            puts "Usage:\n\n"
            puts %Q{idonethis #{New.usage}}
          end
        }
        
        if command == :list && opts.include?("teams")
          return ->(credential, args) { 
            Teams.apply(credential, args.merge(view: views[:teams]))
          }          
        end
        
        use_case = use_cases[command]
      end
      
      def choose_log(args={})
        args[:verbose].true? ? ->(msg){puts "[LOG] #{msg}"} : ->(_){}
      end 
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
idonethis-cli-0.20.0 lib/idonethis/use_cases/app.rb
idonethis-cli-0.19.1 lib/idonethis/use_cases/app.rb
idonethis-cli-0.19.0 lib/idonethis/use_cases/app.rb