Sha256: 09e09513436db50ae30837da38a66f123a954e37c81987812085fa835a241676

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

#
# The nested structure that takes nested hashes with indifferent access
#
class Dry::Initializer::Struct
  extend Dry::Initializer

  class << self
    undef_method :param

    def new(options)
      super(**Hash(options).each_with_object({}) { |(k, v), h| h[k.to_sym] = v })
    end
    alias call new
  end

  #
  # Represents event data as a nested hash with deeply stringified keys
  # @return [Hash<String, ...>]
  #
  def to_h
    self
      .class
      .dry_initializer
      .attributes(self)
      .each_with_object({}) { |(k, v), h| h[k.to_s] = __hashify(v) }
  end

  private

  def __hashify(value)
    case value
    when Hash
      value.each_with_object({}) { |(k, v), obj| obj[k.to_s] = __hashify(v) }
    when Array then value.map { |v| __hashify(v) }
    when Dry::Initializer::Struct then value.to_h
    else value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-3.0.4 lib/dry/initializer/struct.rb
dry-initializer-3.0.3 lib/dry/initializer/struct.rb
dry-initializer-3.0.2 lib/dry/initializer/struct.rb