Class: Apiwork::Configuration::Option

Inherits:
Object
  • Object
show all
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.

Examples:

instance_eval style

option :pagination, type: :hash do
  option :strategy, type: :symbol, default: :offset
  option :default_size, type: :integer, default: 20
end

yield style

option :pagination, type: :hash do |option|
  option.option :strategy, type: :symbol, default: :offset
  option.option :default_size, type: :integer, default: 20
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)



29
30
31
# File 'lib/apiwork/configuration/option.rb', line 29

def children
  @children
end

#defaultObject (readonly)



29
30
31
# File 'lib/apiwork/configuration/option.rb', line 29

def default
  @default
end

#enumObject (readonly)



29
30
31
# File 'lib/apiwork/configuration/option.rb', line 29

def enum
  @enum
end

#nameObject (readonly)



29
30
31
# File 'lib/apiwork/configuration/option.rb', line 29

def name
  @name
end

#typeObject (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_defaultObject



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

Returns:

  • (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.

Examples:

instance_eval style

option :pagination, type: :hash do
  option :strategy, type: :symbol, default: :offset
  option :default_size, type: :integer, default: 20
end

yield style

option :pagination, type: :hash do |option|
  option.option :strategy, type: :symbol, default: :offset
  option.option :default_size, type: :integer, default: 20
end

Parameters:

  • name (Symbol)

    The option name.

  • default (Object, nil) (defaults to: nil)

    (nil) The default value.

  • enum (Array, nil) (defaults to: nil)

    (nil) The allowed values.

  • type (Symbol)
    :boolean, :hash, :integer, :string, :symbol

    The option type.

Yields:

  • block for nested options (type: :hash)

Yield Parameters:



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