Sha256: 2588cf96ee35d653cca6fb32ee3492ce8528a50ca17df6ec0b2c9823ad07c9df

Contents?: true

Size: 1.02 KB

Versions: 66

Compression:

Stored size: 1.02 KB

Contents

module Coco
  module Options
    class Option
      include Options::Item

      attr_accessor :default
      attr_reader :from, :name

      def initialize(name, required: false, value: :undefined, from: nil, **kwargs)
        @name = name.to_sym
        @required = required
        @value = value
        @from = Array(from)
        @private = kwargs.fetch(:private, false)
        @default = kwargs.fetch(:default, nil)
        @alias = kwargs.fetch(:alias, nil)
      end

      def validate_required!
        if required? && undefined?
          raise ArgumentError, "The `#{name}` option requires a value"
        end
      end

      def required?
        @required == true
      end

      def undefined?
        @value == :undefined
      end

      def value
        undefined? ? default : @value
      end

      def value=(val)
        if from.any? && !from.include?(val)
          raise ArgumentError, "`#{val}` is not a valid #{name} value.\n\tAvailable options: #{from}"
        end
        @value = val
      end
    end
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
coveragebook_components-0.16.0 lib/coco/options/option.rb
coveragebook_components-0.15.0 lib/coco/options/option.rb
coveragebook_components-0.14.0 lib/coco/options/option.rb
coveragebook_components-0.13.1 lib/coco/options/option.rb
coveragebook_components-0.13.0 lib/coco/options/option.rb
coveragebook_components-0.12.2 lib/coco/options/option.rb
coveragebook_components-0.12.1 lib/coco/options/option.rb
coveragebook_components-0.12.0 lib/coco/options/option.rb
coveragebook_components-0.11.0 lib/coco/options/option.rb
coveragebook_components-0.10.1.beta.2 lib/coco/options/option.rb
coveragebook_components-0.10.1.beta.1 lib/coco/options/option.rb
coveragebook_components-0.10.1.beta.0 lib/coco/options/option.rb
coveragebook_components-0.10.0 lib/coco/options/option.rb
coveragebook_components-0.9.1 lib/coco/options/option.rb
coveragebook_components-0.9.0 lib/coco/options/option.rb
coveragebook_components-0.8.9 lib/coco/options/option.rb
coveragebook_components-0.8.8 lib/coco/options/option.rb
coveragebook_components-0.8.7 lib/coco/options/option.rb
coveragebook_components-0.8.6 lib/coco/options/option.rb
coveragebook_components-0.8.5 lib/coco/options/option.rb