File: base.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download | duplicates (3)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

require "yaml"

module Byebug
  module Printers
    #
    # Base printer
    #
    class Base
      class MissedPath < StandardError; end
      class MissedArgument < StandardError; end

      SEPARATOR = "."

      def type
        self.class.name.split("::").last.downcase
      end

      private

      def locate(path)
        result = nil
        contents.each_value do |contents|
          result = parts(path).reduce(contents) do |r, part|
            r&.key?(part) ? r[part] : nil
          end
          break if result
        end
        raise MissedPath, "Can't find part path '#{path}'" unless result

        result
      end

      def translate(string, args = {})
        # they may contain #{} string interpolation
        string.gsub(/\|\w+$/, "").gsub(/([^#]?){([^}]*)}/) do
          key = Regexp.last_match[2].to_s
          raise MissedArgument, "Missed argument #{key} for '#{string}'" unless args.key?(key.to_sym)

          "#{Regexp.last_match[1]}#{args[key.to_sym]}"
        end
      end

      def parts(path)
        path.split(SEPARATOR)
      end

      def contents
        @contents ||= contents_files.each_with_object({}) do |filename, hash|
          hash[filename] = YAML.load_file(filename) || {}
        end
      end

      def array_of_args(collection, &_block)
        collection_with_index = collection.each.with_index
        collection_with_index.each_with_object([]) do |(item, index), array|
          args = yield item, index
          array << args if args
        end
      end

      def contents_files
        [File.join(__dir__, "texts", "base.yml")]
      end
    end
  end
end