Skip to content

Commit

Permalink
Fix Hash methods define not only when got LoadError but also when met…
Browse files Browse the repository at this point in the history
…hod is undefined
  • Loading branch information
AkiraFukushima committed Jan 28, 2016
1 parent 189194d commit 8124814
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions lib/shoryuken/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
begin
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/deep_merge'
{}.stringify_keys
{}.symbolize_keys
{}.deep_symbolize_keys
rescue LoadError, NoMethodError
class Hash
def stringify_keys
keys.each do |key|
self[key.to_s] = delete(key)
module Shoryuken
module CoreExt
module StringifyKeys
def stringify_keys
keys.each do |key|
self[key.to_s] = delete(key)
end
self
end
self
end if !{}.respond_to?(:stringify_keys)
end

def symbolize_keys
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
module SymbolizeKeys
def symbolize_keys
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
end
self
end
self
end if !{}.respond_to?(:symbolize_keys)
end

def deep_symbolize_keys
keys.each do |key|
value = delete(key)
self[(key.to_sym rescue key) || key] = value
module DeepSymbolizeKeys
def deep_symbolize_keys
keys.each do |key|
value = delete(key)
self[(key.to_sym rescue key) || key] = value

value.deep_symbolize_keys if value.is_a? Hash
value.deep_symbolize_keys if value.is_a? Hash
end
self
end
self
end if !{}.respond_to?(:deep_symbolize_keys)
end
end
end

begin
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/deep_merge'
rescue LoadError
end

class Hash
include Shoryuken::CoreExt::StringifyKeys if !method_defined?(:stringify_keys)
include Shoryuken::CoreExt::SymbolizeKeys if !method_defined?(:symbolize_keys)
include Shoryuken::CoreExt::DeepSymbolizeKeys if !method_defined?(:deep_symbolize_keys)
end


begin
require 'active_support/core_ext/string/inflections'
rescue LoadError
Expand Down

0 comments on commit 8124814

Please sign in to comment.