Skip to content

Commit 5732fa3

Browse files
committed
[MODEL] Added a HashWrapper class to wrap Hash structures instead of raw Hashie::Mash
Since Hashie now prints out a warning whenever a key which clashes with a built-in method is added, such as `sort`, the logs are flooded. Hashie has a `disable_warnings` method in hashie/hashie@b2f94a4 to supress the logging, so a simple sub-class has been added to allow calling it cleanly, and to serve as a possible extension point in the future. Many thanks to @michaelherold for the pointers to a similar implementation in the Omniauth gem and adding quickly the infrastructure in the Hashie gem! Closes #666
1 parent 6987856 commit 5732fa3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Elasticsearch
2+
module Model
3+
4+
# Subclass of `Hashie::Mash` to wrap Hash-like structures
5+
# (responses from Elasticsearch, search definitions, etc)
6+
#
7+
# The primary goal of the subclass is to disable the
8+
# warning being printed by Hashie for re-defined
9+
# methods, such as `sort`.
10+
#
11+
class HashWrapper < ::Hashie::Mash
12+
disable_warnings if respond_to?(:disable_warnings)
13+
end
14+
end
15+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'test_helper'
2+
3+
require 'hashie/version'
4+
5+
class Elasticsearch::Model::HashWrapperTest < Test::Unit::TestCase
6+
context "The HashWrapper class" do
7+
should "not print the warning for re-defined methods" do
8+
Hashie.logger.expects(:warn).never
9+
10+
subject = Elasticsearch::Model::HashWrapper.new(:foo => 'bar', :sort => true)
11+
end if Hashie::VERSION >= '3.5.3'
12+
end
13+
end

0 commit comments

Comments
 (0)