• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jnbt / java-properties / 4254512104

pending completion
4254512104

Pull #21

github

GitHub
Merge bd6a0ab8d into c409d01fa
Pull Request #21: Test on Ruby 3.1 and 3.2

334 of 339 relevant lines covered (98.53%)

134.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/java-properties/generating/generator.rb
1
module JavaProperties
7✔
2
  module Generating
7✔
3
    # This module allows generating the content of a properties file
4
    # base on a {Properties} object (or any other hash like structure)
5
    # 
6
    # @example
7
    #    Generator.generate({:item => "something ה"}) => "item=something \u05d4"
8
    #
9
    module Generator
7✔
10
      # Character used for key-value separation
11
      # @return [String]
12
      KEY_VALUE_SEPARATOR = '='
7✔
13

14
      # Default options
15
      # @return [Hash]
16
      DEFAULT_OPTIONS = {
6✔
17
        :skip_encode_unicode       => false,
1✔
18
        :skip_encode_separators    => false,
19
        :skip_encode_special_chars => false
20
      }.freeze
21

22
      # Generates a properties file content based on a hash
23
      # @param properties [Properties] or simple hash
24
      # @param options    [Hash]
25
      # @option options skip_encode_unicode [Boolean] Skip unicode encoding
26
      # @option options skip_encode_separators [Boolean] Skip seperators encoding
27
      # @option options skip_encode_special_chars [Boolean] Skip special char encoding
28
      # @return [String]
29
      def self.generate(properties, options = {})
7✔
30
        options = DEFAULT_OPTIONS.merge(options)
42✔
31
        lines = []
42✔
32
        properties.each do |key, value|
42✔
33
          lines << build_line(key, value, options)
350✔
34
        end
35
        lines.join("\n")
42✔
36
      end
37

38
      private
7✔
39

40
      def self.build_line(key, value, options)
7✔
41
        encoded_key   = Encoding.encode!(key.to_s.dup,   *encoding_skips(false, options))
350✔
42
        encoded_value = Encoding.encode!(value.to_s.dup, *encoding_skips(true,  options))
350✔
43

44
        encoded_key + KEY_VALUE_SEPARATOR + encoded_value
350✔
45
      end
46

47
      def self.encoding_skips(is_value, options)
7✔
48
        skips = []
700✔
49
        skips << Encoding::SKIP_SEPARATORS    if is_value || options[:skip_encode_separators]
700✔
50
        skips << Encoding::SKIP_UNICODE       if options[:skip_encode_unicode]
700✔
51
        skips << Encoding::SKIP_SPECIAL_CHARS if options[:skip_encode_special_chars]
700✔
52
        skips
700✔
53
      end
54
    end
55
  end
56
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc