Sha256: a6f0faf14528f097808ab1a6d17b504b3f4c35a306b3e4b0df69e709d5d2a931

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Birdwatcher
  module Commands
    class Set < Birdwatcher::Command
      self.meta = {
        :description => "Set module option",
        :names       => %w(set),
        :usage       => "set OPTION VALUE"
      }

      TRUTHY_VALUES = %w(1 true yes on).freeze
      FALSY_VALUES  = %w(0 false no off).freeze

      def run
        if arguments.count < 2
          error("You must provide an option name and value")
          return false
        end

        if !current_module
          error("No module loaded")
          return false
        end

        option, value = arguments.first.upcase, arguments[1..-1].join(" ")
        if !current_module.meta[:options].keys.include?(option)
          error("Unknown option: #{option.bold}")
          return false
        end

        if current_module.meta[:options][option][:boolean]
          if truthy?(value)
            value = true
          elsif falsy?(value)
            value = false
          end
        end

        current_module.meta[:options][option][:value] = value
      end

      private

      def truthy?(value)
        TRUTHY_VALUES.include?(value.downcase)
      end

      def falsy?(value)
        FALSY_VALUES.include?(value.downcase)
      end

      def current_module
        console.current_module
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
birdwatcher-0.4.0 lib/birdwatcher/commands/set.rb
birdwatcher-0.3.1 lib/birdwatcher/commands/set.rb
birdwatcher-0.1.0 lib/birdwatcher/commands/set.rb