Sha256: 0b8d1c4df7522e0b4909786696caf3a234b0241cf021a77e32d0929750f35b2b

Contents?: true

Size: 1.42 KB

Versions: 29

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
require 'ostruct'
module Blacklight
  ##
  # An OpenStruct that responds to common Hash methods
  class OpenStructWithHashAccess < OpenStruct
    delegate :keys, :each, :map, :has_key?, :key?, :include?, :empty?, :length, :delete, :delete_if, :keep_if, :clear, :reject!, :select!, :replace, :fetch, :to_json, :as_json, :any?, to: :to_h

    ##
    # Expose the internal hash
    # @return [Hash]
    def to_h
      @table
    end

    def select *args, &block
      self.class.new to_h.select(*args, &block)
    end

    def sort_by *args, &block
      self.class.new Hash[to_h.sort_by(*args, &block)]
    end

    def sort_by! *args, &block
      replace Hash[to_h.sort_by(*args, &block)]
      self
    end

    ##
    # Merge the values of this OpenStruct with another OpenStruct or Hash
    # @param [Hash,#to_h] other_hash
    # @return [OpenStructWithHashAccess] a new instance of an OpenStructWithHashAccess
    def merge other_hash
      self.class.new to_h.merge((other_hash if other_hash.is_a? Hash) || other_hash.to_h)
    end

    ##
    # Merge the values of another OpenStruct or Hash into this object
    # @param [Hash,#to_h] other_hash
    # @return [OpenStructWithHashAccess] a new instance of an OpenStructWithHashAccess
    def merge! other_hash
      @table.merge!((other_hash if other_hash.is_a? Hash) || other_hash.to_h)
    end

    def deep_dup
      self.class.new @table.deep_dup
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
blacklight-7.15.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.15.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.14.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.14.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.13.2 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.13.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.13.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.12.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.12.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.11.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.10.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.9.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.8.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.8.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.7.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.6.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.5.1 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.5.0 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.4.2 lib/blacklight/open_struct_with_hash_access.rb
blacklight-7.4.1 lib/blacklight/open_struct_with_hash_access.rb