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.8.4 lib/coco/options/option.rb
coveragebook_components-0.8.3 lib/coco/options/option.rb
coveragebook_components-0.8.2 lib/coco/options/option.rb
coveragebook_components-0.8.1 lib/coco/options/option.rb
coveragebook_components-0.8.0 lib/coco/options/option.rb
coveragebook_components-0.8.0.beta.3 lib/coco/options/option.rb
coveragebook_components-0.8.0.beta.2 lib/coco/options/option.rb
coveragebook_components-0.8.0.beta.1 lib/coco/options/option.rb
coveragebook_components-0.7.10 lib/coco/options/option.rb
coveragebook_components-0.7.9 lib/coco/options/option.rb
coveragebook_components-0.7.8 lib/coco/options/option.rb
coveragebook_components-0.7.7 lib/coco/options/option.rb
coveragebook_components-0.7.6 lib/coco/options/option.rb
coveragebook_components-0.7.5 lib/coco/options/option.rb
coveragebook_components-0.7.4 lib/coco/options/option.rb
coveragebook_components-0.7.3 lib/coco/options/option.rb
coveragebook_components-0.7.2 lib/coco/options/option.rb
coveragebook_components-0.7.1 lib/coco/options/option.rb
coveragebook_components-0.7.0 lib/coco/options/option.rb
coveragebook_components-0.6.5 lib/coco/options/option.rb