Class: VCDry::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vcdry/config.rb

Constant Summary collapse

NOT_DEFINED =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, **options) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vcdry/config.rb', line 12

def initialize(name, type = nil, **options)
  @name = name.to_sym
  @type = (type.nil? || type.respond_to?(:call)) ? type : VCDry::Types[type]
  @options = options

  # Translate default: nil to optional: true
  if @options.key?(:default) && @options[:default].nil?
    @options.delete(:default)
    @options[:optional] = true
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/vcdry/config.rb', line 10

def name
  @name
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/vcdry/config.rb', line 24

def array?
  !!@options[:array]
end

#defaultObject



46
47
48
49
50
# File 'lib/vcdry/config.rb', line 46

def default
  value = @options.fetch(:default, NOT_DEFINED)
  value = value.call if value.respond_to?(:call)
  value
end

#default?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/vcdry/config.rb', line 28

def default?
  @options.key?(:default)
end

#dupObject



32
33
34
# File 'lib/vcdry/config.rb', line 32

def dup
  self.class.new(@name, @type, **@options.dup)
end

#enum?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/vcdry/config.rb', line 36

def enum?
  @options.key?(:values)
end

#enum_valuesObject



40
41
42
43
44
# File 'lib/vcdry/config.rb', line 40

def enum_values
  @enum_values ||= Array(@options[:values]).map do |value|
    @type.nil? ? value : @type.call(value)
  end
end

#instance_variableObject



52
53
54
# File 'lib/vcdry/config.rb', line 52

def instance_variable
  "@#{name}"
end

#optional?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/vcdry/config.rb', line 56

def optional?
  default? || @options[:optional]
end

#required?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/vcdry/config.rb', line 60

def required?
  !optional?
end

#type_cast(value) ⇒ Object



64
65
66
# File 'lib/vcdry/config.rb', line 64

def type_cast(value)
  array? ? type_cast_array(value) : type_cast_value(value)
end