From 81d7009dc6a6cb6680a4b3e9585ad27c17ef278c Mon Sep 17 00:00:00 2001 From: Leif Gensert Date: Fri, 31 Mar 2017 16:20:05 +0200 Subject: [PATCH] completely get rid of activesupport use deep_merge gem for deep_merge and backport symbolize_keys --- lib/morfo.rb | 4 ++-- lib/morfo/builder.rb | 2 +- lib/morfo/tools.rb | 21 +++++++++++++++++++-- morfo.gemspec | 2 +- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/morfo.rb b/lib/morfo.rb index f6d20ff..e1cecc1 100644 --- a/lib/morfo.rb +++ b/lib/morfo.rb @@ -1,4 +1,4 @@ -require "active_support/core_ext/hash" +require 'deep_merge/rails_compat' require "morfo/version" require "morfo/tools" require "morfo/actions" @@ -20,7 +20,7 @@ def self.morf input, options = {} def self.morf_single input, options = {} output = {} mapping_actions.each do |field_path, action| - output.deep_merge!(store_value(action.execute(input), field_path, options)) + output.deeper_merge!(store_value(action.execute(input), field_path, options)) end output end diff --git a/lib/morfo/builder.rb b/lib/morfo/builder.rb index 42c8b90..e4ef994 100644 --- a/lib/morfo/builder.rb +++ b/lib/morfo/builder.rb @@ -7,7 +7,7 @@ def initialize(definitions) def build # WTF??? `definitions` is not accessible inside class # so this javascript technique is necesseray - tmp_definitions = definitions.map { |h| h.symbolize_keys } + tmp_definitions = definitions.map { |h| Morfo::Tools::ExtendedHash.symbolize_keys(h) } Class.new(Morfo::Base) do tmp_definitions.each do |definition| f = field(*definition[:field]) diff --git a/lib/morfo/tools.rb b/lib/morfo/tools.rb index 1a7da3d..a68c67d 100644 --- a/lib/morfo/tools.rb +++ b/lib/morfo/tools.rb @@ -1,5 +1,22 @@ module Morfo module Tools + module ExtendedHash + extend self + + def symbolize_keys(hash) + transform_keys(hash) { |key| key.to_sym rescue key } + end + + def transform_keys(hash) + return hash.enum_for(:transform_keys) { size } unless block_given? + result = {} + hash.each_key do |key| + result[yield(key)] = hash[key] + end + result + end + end + class FlattenHashKeys attr_reader :input_hash @@ -10,10 +27,10 @@ def initialize(input_hash) def flatten input_hash.inject({}) do |result_hash, (key, value)| inner_hash = false - if value.is_a?(Hash) + if value.is_a?(::Hash) inner_hash = true value.each do |inner_key, inner_value| - if inner_value.is_a?(Hash) + if inner_value.is_a?(::Hash) inner_hash = true end result_hash.merge!("#{key}.#{inner_key}".to_sym => inner_value) diff --git a/morfo.gemspec b/morfo.gemspec index 67a6c2d..2f7eba0 100644 --- a/morfo.gemspec +++ b/morfo.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.0.0' spec.add_runtime_dependency "json" - spec.add_runtime_dependency "activesupport", "< 5.0" + spec.add_runtime_dependency "deep_merge" spec.add_runtime_dependency "rubysl" if RUBY_ENGINE == "rbx" spec.add_development_dependency "rake"