Class: Coco::Options::Option

Inherits:
Object
  • Object
show all
Includes:
Item
Defined in:
lib/coco/options/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Item

#alias, #html_alias, #private?, #public?

Constructor Details

#initialize(name, required: false, value: :undefined, from: nil, **kwargs) ⇒ Option

Returns a new instance of Option.



9
10
11
12
13
14
15
16
17
# File 'lib/coco/options/option.rb', line 9

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

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/coco/options/option.rb', line 6

def default
  @default
end

#fromObject (readonly)

Returns the value of attribute from.



7
8
9
# File 'lib/coco/options/option.rb', line 7

def from
  @from
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/coco/options/option.rb', line 7

def name
  @name
end

Instance Method Details

#required?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/coco/options/option.rb', line 25

def required?
  @required == true
end

#undefined?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/coco/options/option.rb', line 29

def undefined?
  @value == :undefined
end

#validate_required!Object



19
20
21
22
23
# File 'lib/coco/options/option.rb', line 19

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

#valueObject



33
34
35
# File 'lib/coco/options/option.rb', line 33

def value
  undefined? ? default : @value
end

#value=(val) ⇒ Object



37
38
39
40
41
42
# File 'lib/coco/options/option.rb', line 37

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