Skip to content

Commit 04ab542

Browse files
committed
Blacklight::OpenStructWithHashAccess should expose a HashWithIndifferentAccess
1 parent 061ac75 commit 04ab542

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/blacklight/utils.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22
require 'ostruct'
3+
require 'active_support/hash_with_indifferent_access'
4+
35
module Blacklight
46
module Utils
57
def self.needs_attr_accessible?
@@ -10,12 +12,27 @@ def self.protected_attributes_enabled?
1012
defined?(ActiveModel::MassAssignmentSecurity)
1113
end
1214
end
15+
16+
class HashWithIndifferentAccessWithSymbolKeys < ActiveSupport::HashWithIndifferentAccess
17+
18+
protected
19+
20+
def convert_key(key)
21+
key.is_a?(Symbol) ? key : key.to_sym
22+
end
23+
end
1324

1425
##
1526
# An OpenStruct that responds to common Hash methods
1627
class OpenStructWithHashAccess < OpenStruct
1728
delegate :keys, :each, :map, :has_key?, :key?, :include?, :empty?, :length, :delete, :delete_if, :keep_if, :clear, :reject!, :select!, :replace, :fetch, :to_json, :as_json, to: :to_h
1829

30+
def initialize(*args)
31+
super
32+
33+
@table = HashWithIndifferentAccessWithSymbolKeys.new(@table)
34+
end
35+
1936
##
2037
# Expose the internal hash
2138
# @return [Hash]

spec/lib/blacklight/utils_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@
3838
end
3939

4040
it "exposes the internal hash table" do
41-
expect(@h.to_h).to be_a_kind_of(Hash)
41+
expect(@h.to_h).to be_a_kind_of(HashWithIndifferentAccess)
4242
expect(@h.to_h[:a]).to eq 1
4343
end
4444

4545
it "exposes keys" do
4646
expect(@h.keys).to include(:a, :b)
4747
end
48-
4948
end
5049

5150
describe "#key?" do

0 commit comments

Comments
 (0)