Class: KingKonf::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/king_konf/variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, default: nil, description: "", required: false, allowed_values: nil, validate_with: ->(_) { true }, options: {}) ⇒ Variable

Returns a new instance of Variable.



7
8
9
10
11
12
13
14
15
# File 'lib/king_konf/variable.rb', line 7

def initialize(name:, type:, default: nil, description: "", required: false, allowed_values: nil, validate_with: ->(_) { true }, options: {})
  @name, @type = name, type
  @description = description
  @required = required
  @allowed_values = allowed_values
  @options = options
  @default = cast(default) unless default.nil?
  @validate_with = validate_with
end

Instance Attribute Details

#allowed_valuesObject (readonly)

Returns the value of attribute allowed_values.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def allowed_values
  @allowed_values
end

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def type
  @type
end

#validate_withObject (readonly)

Returns the value of attribute validate_with.



5
6
7
# File 'lib/king_konf/variable.rb', line 5

def validate_with
  @validate_with
end

Instance Method Details

#allowed?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/king_konf/variable.rb', line 42

def allowed?(value)
  allowed_values.nil? || allowed_values.include?(cast(value))
rescue ConfigError
  false
end

#cast(value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/king_konf/variable.rb', line 17

def cast(value)
  case @type
  when :boolean then [true, false].include?(value) ? value : Decoder.boolean(value, **options)
  when :integer then Integer(value)
  when :float then Float(value)
  when :duration then value.is_a?(Integer) ? value : Decoder.duration(value)
  when :symbol then value.to_sym
  else value
  end
rescue ArgumentError, NoMethodError
  raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, expected #{type}"
end

#decode(value) ⇒ Object



48
49
50
# File 'lib/king_konf/variable.rb', line 48

def decode(value)
  Decoder.public_send(@type, value, **options)
end

#required?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/king_konf/variable.rb', line 30

def required?
  @required
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/king_konf/variable.rb', line 34

def valid?(value)
  cast_value = cast(value)
rescue ConfigError
  false
else
  !!validate_with.call(cast_value)
end