Class: KingKonf::Variable
- Inherits:
-
Object
- Object
- KingKonf::Variable
- Defined in:
- lib/king_konf/variable.rb
Instance Attribute Summary collapse
-
#allowed_values ⇒ Object
readonly
Returns the value of attribute allowed_values.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validate_with ⇒ Object
readonly
Returns the value of attribute validate_with.
Instance Method Summary collapse
- #allowed?(value) ⇒ Boolean
- #cast(value) ⇒ Object
- #decode(value) ⇒ Object
-
#initialize(name:, type:, default: nil, description: "", required: false, allowed_values: nil, validate_with: ->(_) { true }, options: {}) ⇒ Variable
constructor
A new instance of Variable.
- #required? ⇒ Boolean
- #valid?(value) ⇒ Boolean
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 = @default = cast(default) unless default.nil? @validate_with = validate_with end |
Instance Attribute Details
#allowed_values ⇒ Object (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 |
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/king_konf/variable.rb', line 5 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/king_konf/variable.rb', line 5 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/king_konf/variable.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/king_konf/variable.rb', line 5 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/king_konf/variable.rb', line 5 def type @type end |
#validate_with ⇒ Object (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
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, **) 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, **) end |
#required? ⇒ Boolean
30 31 32 |
# File 'lib/king_konf/variable.rb', line 30 def required? @required end |
#valid?(value) ⇒ 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 |