File: map.rb

package info (click to toggle)
ruby-glob 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 200 kB
  • sloc: ruby: 449; makefile: 7; sh: 4
file content (34 lines) | stat: -rw-r--r-- 597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

module Glob
  class Map
    def self.call(target)
      new(target).call
    end

    def initialize(target)
      @target = target
    end

    def call
      @target
        .map {|(key, value)| compute(value, escape(key)) }
        .flatten
        .sort
    end

    private def escape(key)
      key.to_s.gsub(".", "\\.")
    end

    private def compute(value, path)
      if value.is_a?(Hash)
        value.map do |key, other_value|
          compute(other_value, "#{path}.#{escape(key)}")
        end
      else
        path.to_s
      end
    end
  end
end