Sha256: 93f8dcf6176fead10a321ec571b119c8c91c431b85fa44d5ef0d1db76d89c5fe

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
require 'yaml'

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

      SEPARATOR = '.'.freeze

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

      private

      def locate(path)
        result = nil
        contents.each do |_, contents|
          result = parts(path).reduce(contents) do |r, part|
            r && 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
          unless args.key?(key.to_sym)
            raise MissedArgument, "Missed argument #{key} for '#{string}'"
          end

          "#{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.expand_path(File.join('..', 'texts', 'base.yml'), __FILE__)]
      end
    end
  end
end

Version data entries

13 entries across 12 versions & 2 rubygems

Version Path
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/byebug-9.0.6/lib/byebug/printers/base.rb
tdiary-5.0.5 vendor/bundle/gems/byebug-9.0.6/lib/byebug/printers/base.rb
tdiary-5.0.4 vendor/bundle/gems/byebug-9.0.6/lib/byebug/printers/base.rb
byebug-9.0.6 lib/byebug/printers/base.rb
byebug-9.0.5 lib/byebug/printers/base.rb
byebug-9.0.4 lib/byebug/printers/base.rb
byebug-9.0.3 lib/byebug/printers/base.rb
byebug-9.0.2 lib/byebug/printers/base.rb
byebug-9.0.1 lib/byebug/printers/base.rb
byebug-9.0.0 lib/byebug/printers/base.rb
byebug-8.2.5 lib/byebug/printers/base.rb
byebug-8.2.4 lib/byebug/printers/base.rb
byebug-8.2.3 lib/byebug/printers/base.rb