Sha256: 126b0fc5018a19ee2c29393f0c115cc3fef62928d08f231ee7b9318cc068bf04

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

require "mapr/version"
require "yaml"

module Mapr
  class Error < StandardError; end

  module_function

  CONSTANT_PREFIX = /\A\+/

  def map(schema, data)
    YAML.load(schema)
      .transform_values do |path|
        dig(data, path)
      end
  end

  def dig(data, path)
    case path
    when CONSTANT_PREFIX
      path.gsub(CONSTANT_PREFIX, "")
    else
      data.dig(*parse_path(path))
    end
  end

  def parse_path(path)
    case path
    when Array
      path
    when String
      path.split("/")
        .map(&method(:transform_path_token))
    when Integer
      [path]
    else
      raise Mapr::Error, "Path has to be a string or integer path was of type: '#{path.class}' (#{path})"
    end
  end

  def transform_path_token(path_token)
    if path_token =~ /\A\d+\Z/
      path_token.to_i
    else
      path_token
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mapr-0.1.0 lib/mapr.rb