Class: UltraSettings::Field
- Inherits:
-
Object
- Object
- UltraSettings::Field
- Defined in:
- lib/ultra_settings/field.rb
Overview
Definition for a field on a configuration.
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#default_if ⇒ Object
readonly
Returns the value of attribute default_if.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#env_var ⇒ Object
readonly
Returns the value of attribute env_var.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#runtime_setting ⇒ Object
readonly
Returns the value of attribute runtime_setting.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#yaml_key ⇒ Object
readonly
Returns the value of attribute yaml_key.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
Coerce the passed in value to the type of the field.
-
#initialize(name:, type: :string, description: nil, default: nil, default_if: nil, env_var: nil, runtime_setting: nil, yaml_key: nil, static: false, secret: false) ⇒ Field
constructor
A new instance of Field.
-
#secret? ⇒ Boolean
Returns true if the field is marked as having a secret value.
-
#source(env: nil, settings: nil, yaml_config: nil) ⇒ Symbol?
Get the source for the field from the passed in state.
-
#static? ⇒ Boolean
Returns true if the field is static.
-
#value(env: nil, settings: nil, yaml_config: nil) ⇒ Object
Get the value for the field from the passed in state.
Constructor Details
#initialize(name:, type: :string, description: nil, default: nil, default_if: nil, env_var: nil, runtime_setting: nil, yaml_key: nil, static: false, secret: false) ⇒ Field
Returns a new instance of Field.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ultra_settings/field.rb', line 25 def initialize( name:, type: :string, description: nil, default: nil, default_if: nil, env_var: nil, runtime_setting: nil, yaml_key: nil, static: false, secret: false ) @name = name.to_s.freeze @type = type.to_sym @description = description&.to_s&.freeze @default = Coerce.coerce_value(default, @type).freeze @default_if = default_if @env_var = env_var&.to_s&.freeze @runtime_setting = runtime_setting&.to_s&.freeze @yaml_key = yaml_key&.to_s&.freeze @static = !!static @secret = (secret.respond_to?(:call) ? secret : !!secret) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
9 10 11 |
# File 'lib/ultra_settings/field.rb', line 9 def default @default end |
#default_if ⇒ Object (readonly)
Returns the value of attribute default_if.
10 11 12 |
# File 'lib/ultra_settings/field.rb', line 10 def default_if @default_if end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
8 9 10 |
# File 'lib/ultra_settings/field.rb', line 8 def description @description end |
#env_var ⇒ Object (readonly)
Returns the value of attribute env_var.
11 12 13 |
# File 'lib/ultra_settings/field.rb', line 11 def env_var @env_var end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ultra_settings/field.rb', line 6 def name @name end |
#runtime_setting ⇒ Object (readonly)
Returns the value of attribute runtime_setting.
12 13 14 |
# File 'lib/ultra_settings/field.rb', line 12 def runtime_setting @runtime_setting end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/ultra_settings/field.rb', line 7 def type @type end |
#yaml_key ⇒ Object (readonly)
Returns the value of attribute yaml_key.
13 14 15 |
# File 'lib/ultra_settings/field.rb', line 13 def yaml_key @yaml_key end |
Instance Method Details
#coerce(value) ⇒ Object
Coerce the passed in value to the type of the field.
72 73 74 |
# File 'lib/ultra_settings/field.rb', line 72 def coerce(value) Coerce.coerce_value(value, @type) end |
#secret? ⇒ Boolean
Returns true if the field is marked as having a secret value.
86 87 88 89 90 91 92 |
# File 'lib/ultra_settings/field.rb', line 86 def secret? if @secret.respond_to?(:call) !!@secret.call else @secret end end |
#source(env: nil, settings: nil, yaml_config: nil) ⇒ Symbol?
Get the source for the field from the passed in state.
64 65 66 |
# File 'lib/ultra_settings/field.rb', line 64 def source(env: nil, settings: nil, yaml_config: nil) fetch_value_and_source(env: env, settings: settings, yaml_config: yaml_config).last end |
#static? ⇒ Boolean
Returns true if the field is static.
79 80 81 |
# File 'lib/ultra_settings/field.rb', line 79 def static? @static end |
#value(env: nil, settings: nil, yaml_config: nil) ⇒ Object
Get the value for the field from the passed in state.
54 55 56 |
# File 'lib/ultra_settings/field.rb', line 54 def value(env: nil, settings: nil, yaml_config: nil) fetch_value_and_source(env: env, settings: settings, yaml_config: yaml_config).first end |