Sha256: 04f1aa9c17c9d787511bce3700ad7e71c61a14d58d843d2988f07bcbe498e1a3

Contents?: true

Size: 952 Bytes

Versions: 2

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

Dir[File.join(File.dirname(__FILE__), "../*.rb")].sort.each { |file| require file }

OpenStruct.class_eval do
  def to_hash
    each_pair.with_object({}) do |(key, value), hash|
      hash[key] = case value
                  when OpenStruct
                    value.to_hash
                  when Array
                    value.map!(&:to_hash)
                  else
                    value
                  end
    end
  end

  def set_nested_value(keys, value)
    keys = keys.split(" ") if keys.is_a?(String)
    create_nest(keys)
    *keys, final_key = keys
    to_set = keys.empty? ? self : dig(*keys)

    return unless to_set

    to_set[final_key] = value
  end

  def create_nest(keys)
    keys = keys.split(" ") if keys.is_a?(String)
    ostruct = self
    keys.each do |key|
      ostruct[key] = OpenStruct.new unless ostruct[key].is_a?(OpenStruct)
      ostruct = ostruct[key]
    end
    self
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mithraic_events-0.1.1 lib/plugin/plugin.rb
mithraic_events-0.1.0 lib/plugin/plugin.rb