Sha256: 113f1c2b83fb1669ccdc73e707df59ad8c89a8c72b60aca930f48b1a8b0b4de8

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require "date"
require "time"
require "active_support/core_ext/hash/keys"
require "core_ext/hash"

class HashConverter
  SEPARATOR = "."
  KEY_REGEX = /\{([^}]*)\}/

  Types = [String, Integer, Float, Time, Date, DateTime]

  def self.convert(hash = {}, &block)
    @hash = hash.namespace_flatten(SEPARATOR)
    @converted = {}
    @path = []

    instance_eval(&block)

    @converted.namespace_unflatten.recursive_symbolize_keys!
  end

  private

    def self.path(path, &block)
      @path.push(path)
      instance_eval(&block)
      @path.pop
    end

    def self.map(input, output, type_or_symbol = nil, &block)
      value = if input =~ KEY_REGEX
        input.gsub(KEY_REGEX) { |var| self.get(var.gsub(/\{|\}/,"")) }
      else
        self.get(input)
      end

      value = if type_or_symbol.is_a?(Symbol)
        value.send(type_or_symbol)
      elsif type_or_symbol.is_a?(Class)
        typecast(value, type_or_symbol)
      else
        value
      end

      if block_given?
        value = yield value
      end

      @converted[output.to_s] = value
    end

    def self.typecast(value, type)
      return value if value.nil?

      if Types.include?(type)
        case type.to_s
        when "String"
          value.to_s
        when "Integer"
          value.to_i
        when "Float"
          value.to_f
        when "Time"
          Time.parse(value)
        when "Date"
          Date.parse(value)
        when "DateTime"
          DateTime.parse(value)
        else
          value
        end
      end
    end

    def self.namespaced_key(key)
      (@path + [key]).join(SEPARATOR).to_s
    end

    def self.get(*keys)
      values = []
      keys.each do |key|
        key = namespaced_key(key)
        values <<  @hash[key]
      end
      values.length == 1 ? values.first : values
    end

    def self.set(key, value)
      @converted[key] = value
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_converter-0.0.2 lib/hash_converter.rb