Sha256: 3220f6ba5925892cb263025a4dc66b7040241ebd3a543c042eb8e41f17bcb816

Contents?: true

Size: 1022 Bytes

Versions: 2

Compression:

Stored size: 1022 Bytes

Contents

module Ownlan
  class Application

    attr_reader :raw_options, :config

    def initialize(options)
      @raw_options = options

      @config = ::Ownlan.config.dup

      config_options = raw_options.reject{ |k, v| k.to_s.match('_given') || !v }
      set_options(config_options)
    end

    def call
      action = config.modes.find do |type, modes|
        modes.find { |mode| process(type, mode) }
      end

      show_menu unless action
    end

    private

    def show_menu
      Trollop.educate
    rescue ArgumentError
      raise ::Ownlan::MissingArgumentError, 'Missing or Invalid parameter.'
    end

    def set_options(config_options)
      config_options.each { |k, v| config.send("#{k}=", v) }
    end

    def process(type, mode)
      return unless good_args?(type, mode)
      "Ownlan::#{type.capitalize}::#{mode.capitalize}".constantize.new(config).process
    rescue ::NoMethodError
      show_menu
    end

    def good_args?(type, mode)
      raw_options[type] == mode.to_s
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ownlan-0.3.2 lib/ownlan/application.rb
ownlan-0.3.0 lib/ownlan/application.rb