Module: Pinspec::Tags

Defined in:
lib/pinspec/tags.rb

Constant Summary collapse

VALUED =
%w[int float str sym decimal date time bin ref seq relation cycle inf].freeze

Class Method Summary collapse

Class Method Details

.decimal(value) ⇒ Object



30
31
32
# File 'lib/pinspec/tags.rb', line 30

def decimal(value)
  { "t" => "decimal", "v" => value.to_s }
end

.decode(tagged) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pinspec/tags.rb', line 42

def decode(tagged)
  return tagged unless tagged?(tagged)

  case tagged["t"]
  when "nil"      then nil
  when "bool"     then tagged["v"]
  when "int"      then tagged["v"]
  when "float"    then tagged["v"]
  when "nan"      then Float::NAN
  when "inf"      then tagged["sign"].to_i.negative? ? -Float::INFINITY : Float::INFINITY
  when "sym"      then tagged["v"].to_sym
  when "str"      then tagged["v"]
  when "decimal"  then tagged["v"]
  when "date"     then tagged["v"]
  when "time"     then tagged["v"]
  when "bin"      then tagged["v"].unpack1("m0").force_encoding(tagged["enc"] || "ASCII-8BIT")
  when "array"    then tagged["v"].map { |element| decode(element) }
  when "hash"     then tagged["v"].to_h { |key, element| [decode(key), decode(element)] }
  when "ref"      then tagged["v"]
  else tagged["v"]
  end
end

.describe(tagged) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pinspec/tags.rb', line 65

def describe(tagged)
  return tagged.inspect unless tagged?(tagged)

  case tagged["t"]
  when "nil"   then "nil"
  when "ref"   then tagged["v"]
  when "sym"   then ":#{tagged['v']}"
  when "str"   then tagged["v"].inspect
  when "array" then "[#{tagged['v'].map { |element| describe(element) }.join(', ')}]"
  when "hash"  then "{#{tagged['v'].map { |k, v| "#{describe(k)}=>#{describe(v)}" }.join(', ')}}"
  when "nan"   then "NaN"
  when "inf"   then tagged["sign"].to_i.negative? ? "-Infinity" : "Infinity"
  else tagged["v"].to_s
  end
end

.encode(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pinspec/tags.rb', line 10

def encode(value)
  case value
  when nil        then { "t" => "nil" }
  when true       then { "t" => "bool", "v" => true }
  when false      then { "t" => "bool", "v" => false }
  when Integer    then { "t" => "int", "v" => value }
  when Float      then encode_float(value)
  when Symbol     then { "t" => "sym", "v" => value.to_s }
  when String     then encode_string(value)
  when Array      then { "t" => "array", "v" => value.map { |element| encode(element) } }
  when Hash       then encode_hash(value)
  when Date       then { "t" => "date", "v" => value.iso8601 }
  else { "t" => "str", "v" => value.to_s }
  end
end

.ref(name) ⇒ Object



26
27
28
# File 'lib/pinspec/tags.rb', line 26

def ref(name)
  { "t" => "ref", "v" => name.to_s }
end

.tagged?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pinspec/tags.rb', line 34

def tagged?(value)
  value.is_a?(Hash) && value.key?("t")
end

.type_of(value) ⇒ Object



38
39
40
# File 'lib/pinspec/tags.rb', line 38

def type_of(value)
  tagged?(value) ? value["t"] : nil
end