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

MushroomObserver / mushroom-observer / 13175285189

06 Feb 2025 09:03AM UTC coverage: 88.937% (+0.4%) from 88.554%
13175285189

push

github

nimmolo
Merge branch 'main' into query-scopes-folder

223 of 231 new or added lines in 29 files covered. (96.54%)

8 existing lines in 5 files now uncovered.

27390 of 30797 relevant lines covered (88.94%)

548.47 hits per line

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

97.96
/app/classes/query/modules/serialization.rb
1
# frozen_string_literal: true
2

3
# Turn a query into a string and vice versa.
4
module Query::Modules::Serialization
1✔
5
  def self.included(base)
1✔
6
    base.extend(ClassMethods)
1✔
7
  end
8

9
  def serialize
1✔
10
    hash = params.merge(model: model.to_s.to_sym)
3,084✔
11
    hash.keys.sort_by(&:to_s).map do |key|
3,084✔
12
      serialize_key_value(key, hash[key])
8,372✔
13
    end.join(";")
14
  end
15

16
  def serialize_key_value(key, val)
1✔
17
    "#{key}=#{serialize_value(val)}"
8,372✔
18
  end
19

20
  def serialize_value(val)
1✔
21
    case val
10,757✔
22
    when Array      then "@#{val.map { |v| serialize_value(v) }.join(",")}"
3,993✔
23
    when String     then "$#{serialize_string(val)}"
2,291✔
24
    when Symbol     then ":#{serialize_string(val.to_s)}"
3,205✔
25
    when Integer, Float then "##{val}"
2,494✔
26
    when TrueClass  then "1"
1,056✔
27
    when FalseClass then "0"
75✔
UNCOV
28
    when NilClass   then "-"
×
29
    end
30
  end
31

32
  def serialize_string(val)
1✔
33
    # The "n" forces the Regexp to be in ascii 8 bit encoding = binary.
34
    String.new(val).force_encoding("binary").
5,496✔
35
      gsub(%r{[,;:#%&=/?\x00-\x1f\x7f-\xff]}n) do |char|
36
      format("%%%02.2X", char.ord)
213✔
37
    end
38
  end
39

40
  # Class methods.
41
  module ClassMethods
1✔
42
    def deserialize(str)
1✔
43
      params = deserialize_params(str)
207✔
44
      model  = params[:model]
207✔
45
      params.delete(:model)
207✔
46
      Query.new(model, params)
207✔
47
    end
48

49
    def deserialize_params(str)
1✔
50
      params = {}
207✔
51
      str.split(";").each do |line|
207✔
52
        next if line !~ /^(\w+)=(.*)/
511✔
53

54
        key = Regexp.last_match(1)
511✔
55
        val = Regexp.last_match(2)
511✔
56
        params[key.to_sym] = deserialize_value(val)
511✔
57
      end
58
      params
207✔
59
    end
60

61
    def deserialize_value(val)
1✔
62
      val = val.sub(/^(.)/, "")
658✔
63
      case Regexp.last_match(1)
658✔
64
      when "@" then val.split(",").map { |v| deserialize_value(v) }
203✔
65
      when "$" then deserialize_string(val)
153✔
66
      when ":" then deserialize_string(val).to_sym
207✔
67
      when "#" then deserialize_number(val)
205✔
68
      when "1" then true
36✔
69
      when "0" then false
1✔
70
      when "-" then nil
71
      end
72
    end
73

74
    def deserialize_string(val)
1✔
75
      String.new(val).force_encoding("binary").gsub(/%(..)/) do |match|
360✔
76
        match[1..2].hex.chr("binary")
4✔
77
      end.force_encoding("UTF-8")
78
    end
79

80
    def deserialize_number(val)
1✔
81
      val.include?(".") ? val.to_f : val.to_i
205✔
82
    end
83
  end
84
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