Class: BERT::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/bert/decoder.rb

Class Method Summary collapse

Class Method Details

.convert(item) ⇒ Object

Convert Erlectricity representation of BERT complex types into corresponding Ruby types.

+item+ is the Ruby object to convert

Returns the converted Ruby object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bert/decoder.rb', line 17

def self.convert(item)
  case item
    when TrueClass, FalseClass
      item.to_s.to_sym
    when Erl::List
      item.map { |x| convert(x) }
    when Array
      if item[0] == :bert
        convert_bert(item)
      else
        Tuple.new(item.map { |x| convert(x) })
      end
    else
      item
  end
end

.convert_bert(item) ⇒ Object

Convert complex types.

+item+ is the complex type array

Returns the converted Ruby object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bert/decoder.rb', line 38

def self.convert_bert(item)
  case item[1]
    when :nil
      nil
    when :dict
      item[2].inject({}) do |acc, x|
        acc[convert(x[0])] = convert(x[1]); acc
      end
    when TrueClass
      true
    when FalseClass
      false
    when :time
      Time.at(item[2] * 1_000_000 + item[3], item[4])
    when :regex
      options = 0
      options |= Regexp::EXTENDED if item[3].include?(:extended)
      options |= Regexp::IGNORECASE if item[3].include?(:caseless)
      options |= Regexp::MULTILINE if item[3].include?(:multiline)
      Regexp.new(item[2], options)
    else
      nil
  end
end

.decode(bert) ⇒ Object

Decode a BERT into a Ruby object.

+bert+ is the BERT String

Returns a Ruby object



7
8
9
10
# File 'lib/bert/decoder.rb', line 7

def self.decode(bert)
  simple_ruby = Erlectricity::Decoder.decode(bert)
  convert(simple_ruby)
end