Class: Apiwork::Configuration::Option
- Inherits:
-
Object
- Object
- Apiwork::Configuration::Option
- Includes:
- Validatable
- Defined in:
- lib/apiwork/configuration/option.rb
Overview
Block context for nested configuration options.
Used inside ‘option :name, type: :hash do … end` blocks in Adapter::Base and Export::Base subclasses.
Instance Attribute Summary collapse
- #children ⇒ Object readonly
- #default ⇒ Object readonly
- #enum ⇒ Object readonly
- #name ⇒ Object readonly
- #type ⇒ Object readonly
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #effective_default ⇒ Object
-
#initialize(name, type, children: nil, default: nil, enum: nil, &block) ⇒ Option
constructor
A new instance of Option.
- #nested? ⇒ Boolean
-
#option(name, default: nil, enum: nil, type:) {|option| ... } ⇒ void
Defines a nested option.
- #validate!(value) ⇒ Object
Constructor Details
#initialize(name, type, children: nil, default: nil, enum: nil, &block) ⇒ Option
Returns a new instance of Option.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/apiwork/configuration/option.rb', line 35 def initialize(name, type, children: nil, default: nil, enum: nil, &block) @name = name @type = type @default = default @enum = enum @children = children || {} return unless block && type == :hash block.arity.positive? ? yield(self) : instance_eval(&block) end |
Instance Attribute Details
#children ⇒ Object (readonly)
29 30 31 |
# File 'lib/apiwork/configuration/option.rb', line 29 def children @children end |
#default ⇒ Object (readonly)
29 30 31 |
# File 'lib/apiwork/configuration/option.rb', line 29 def default @default end |
#enum ⇒ Object (readonly)
29 30 31 |
# File 'lib/apiwork/configuration/option.rb', line 29 def enum @enum end |
#name ⇒ Object (readonly)
29 30 31 |
# File 'lib/apiwork/configuration/option.rb', line 29 def name @name end |
#type ⇒ Object (readonly)
29 30 31 |
# File 'lib/apiwork/configuration/option.rb', line 29 def type @type end |
Instance Method Details
#cast(value) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/apiwork/configuration/option.rb', line 100 def cast(value) return nil if value.nil? return value unless value.is_a?(String) case type when :symbol then value.to_sym when :string then value when :integer then value.to_i when :boolean then %w[true 1 yes].include?(value.downcase) when :hash then value end end |
#effective_default ⇒ Object
81 82 83 84 85 |
# File 'lib/apiwork/configuration/option.rb', line 81 def effective_default return default unless nested? children.transform_values(&:default) end |
#nested? ⇒ Boolean
77 78 79 |
# File 'lib/apiwork/configuration/option.rb', line 77 def nested? children.any? end |
#option(name, default: nil, enum: nil, type:) {|option| ... } ⇒ void
This method returns an undefined value.
Defines a nested option.
73 74 75 |
# File 'lib/apiwork/configuration/option.rb', line 73 def option(name, default: nil, enum: nil, type:, &block) @children[name] = Option.new(name, type, default:, enum:, &block) end |
#validate!(value) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/apiwork/configuration/option.rb', line 87 def validate!(value) return if value.nil? if nested? raise ConfigurationError, "#{name} must be a Hash" unless value.is_a?(Hash) validate_children!(value) else validate_type!(value) validate_enum!(value) if enum end end |