Sha256: aca54279f2a8e40639b41cb3910813b1e893cc2ed35bbfefea202c2d0c517c53

Contents?: true

Size: 913 Bytes

Versions: 2

Compression:

Stored size: 913 Bytes

Contents

# frozen_string_literal: true

class Solid::Adapters::Configurable::Options
  MapOption = ->(key) { key.end_with?("=") ? key[0..-2].to_sym : key }
  MapValue = ->(value) { value.is_a?(::Proc) ? value.call : value }

  def initialize(**options)
    @options = options
  end

  def to_h
    @options.dup
  end

  def key?(name)
    @options.key?(name)
  end

  def [](name)
    @options[name].then(&MapValue)
  end

  def fetch(name, &block)
    @options.fetch(name, &block).then(&MapValue)
  end

  def method_missing(method_name, value = nil, &block)
    return fetch(method_name) { super } if !method_name.end_with?("=") && !block

    option_name = MapOption[method_name]

    @options[option_name] = block || value
  end

  def respond_to_missing?(method_name, include_private = false)
    (method_name.end_with?("=") || key?(method_name)) || super
  end

  def freeze
    @options.freeze

    super
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solid-adapters-1.1.0 lib/solid/adapters/configurable/options.rb
solid-adapters-1.0.0 lib/solid/adapters/configurable/options.rb