Sha256: ff3695fd04f0ac3c73773ad4020045146101a3ab0ac76d461bf6b2641bb6600f

Contents?: true

Size: 1.23 KB

Versions: 43

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "yaml"

YAML.add_builtin_type("omap") do |type, val|
  ActiveSupport::OrderedHash[val.map { |v| v.to_a.first }]
end

module ActiveSupport
  # DEPRECATED: +ActiveSupport::OrderedHash+ implements a hash that preserves
  # insertion order.
  #
  #   oh = ActiveSupport::OrderedHash.new
  #   oh[:a] = 1
  #   oh[:b] = 2
  #   oh.keys # => [:a, :b], this order is guaranteed
  #
  # Also, maps the +omap+ feature for YAML files
  # (See https://yaml.org/type/omap.html) to support ordered items
  # when loading from YAML.
  #
  # +ActiveSupport::OrderedHash+ is namespaced to prevent conflicts
  # with other implementations.
  class OrderedHash < ::Hash # :nodoc:
    def to_yaml_type
      "!tag:yaml.org,2002:omap"
    end

    def encode_with(coder)
      coder.represent_seq "!omap", map { |k, v| { k => v } }
    end

    def select(*args, &block)
      dup.tap { |hash| hash.select!(*args, &block) }
    end

    def reject(*args, &block)
      dup.tap { |hash| hash.reject!(*args, &block) }
    end

    def nested_under_indifferent_access
      self
    end

    # Returns true to make sure that this hash is extractable via <tt>Array#extract_options!</tt>
    def extractable_options?
      true
    end
  end
end

Version data entries

43 entries across 43 versions & 7 rubygems

Version Path
omg-activesupport-8.0.0.alpha2 lib/active_support/ordered_hash.rb
omg-activesupport-8.0.0.alpha1 lib/active_support/ordered_hash.rb
activesupport-7.1.4 lib/active_support/ordered_hash.rb
activesupport-7.2.1 lib/active_support/ordered_hash.rb
activesupport-7.2.0 lib/active_support/ordered_hash.rb
activesupport-7.2.0.rc1 lib/active_support/ordered_hash.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/ordered_hash.rb
activesupport-7.2.0.beta3 lib/active_support/ordered_hash.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/ordered_hash.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/ordered_hash.rb
activesupport-7.2.0.beta2 lib/active_support/ordered_hash.rb
activesupport-7.1.3.4 lib/active_support/ordered_hash.rb
activesupport-7.2.0.beta1 lib/active_support/ordered_hash.rb
activesupport-7.1.3.2 lib/active_support/ordered_hash.rb
activesupport-7.1.3.1 lib/active_support/ordered_hash.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/ordered_hash.rb
activesupport-7.1.3 lib/active_support/ordered_hash.rb
activesupport-7.1.2 lib/active_support/ordered_hash.rb
activesupport-7.1.1 lib/active_support/ordered_hash.rb
activesupport-7.1.0 lib/active_support/ordered_hash.rb