Skip to content

Commit d2bcdef

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

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/blacklight/utils.rb

Lines changed: 22 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?
@@ -11,11 +13,31 @@ def self.protected_attributes_enabled?
1113
end
1214
end
1315

16+
class HashWithIndifferentAccessWithSymbolKeys < ActiveSupport::HashWithIndifferentAccess
17+
protected
18+
19+
def convert_key(key)
20+
if key.is_a?(Symbol)
21+
key
22+
elsif key.respond_to? :to_sym
23+
key.to_sym
24+
else
25+
key
26+
end
27+
end
28+
end
29+
1430
##
1531
# An OpenStruct that responds to common Hash methods
1632
class OpenStructWithHashAccess < OpenStruct
1733
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
1834

35+
def initialize(*args)
36+
super
37+
38+
@table = HashWithIndifferentAccessWithSymbolKeys.new(@table)
39+
end
40+
1941
##
2042
# Expose the internal hash
2143
# @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)