Module: Mapr

Defined in:
lib/mapr.rb,
lib/mapr/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

CONSTANT_PREFIX =
/\A\+/
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.dig(data, path) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/mapr.rb', line 18

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

.map(schema, data) ⇒ Object



11
12
13
14
15
16
# File 'lib/mapr.rb', line 11

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

.parse_path(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mapr.rb', line 27

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

.transform_path_token(path_token) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mapr.rb', line 41

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