Sha256: d292579c0e6319c612c2bd07dfa096b68a45a35a026543adf61eb8631a263aa5

Contents?: true

Size: 856 Bytes

Versions: 10

Compression:

Stored size: 856 Bytes

Contents

module DeepSymbolizable

  def deep_symbolize(&block)
    method = self.class.to_s.downcase.to_sym
    syms = DeepSymbolizable::Symbolizers
    syms.respond_to?(method) ? syms.send(method, self, &block) : self
  end

  module Symbolizers
    extend self

    def hash(hash, &block)
      hash.inject({}) do |result, (key, value)|
        value = _recurse_(value, &block)
        key = yield key if block_given?
        sym_key = key.to_sym rescue key
        result[sym_key] = value
        result
      end
    end

    def array(ary, &block)
      ary.map { |v| _recurse_(v, &block) }
    end

    def _recurse_(value, &block)
      if value.is_a?(Enumerable) && !value.is_a?(String)
        value.extend DeepSymbolizable unless value.class.include?(DeepSymbolizable)
        value = value.deep_symbolize(&block)
      end
      value
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
devinstall-1.2.3 lib/devinstall/deep_symbolize.rb
devinstall-1.2.2 lib/devinstall/deep_symbolize.rb
devinstall-1.2.1 lib/devinstall/deep_symbolize.rb
devinstall-1.2.0 lib/devinstall/deep_symbolize.rb
devinstall-1.1.0 lib/devinstall/deep_symbolize.rb
devinstall-1.0.1 lib/devinstall/deep_symbolize.rb
devinstall-1.0.0 lib/devinstall/deep_symbolize.rb
devinstall-0.2.6 lib/devinstall/deep_symbolize.rb
devinstall-0.2.5 lib/devinstall/deep_symbolize.rb
devinstall-0.2.3 lib/devinstall/deep_symbolize.rb