Class: BERT::Encoder
- Inherits:
-
Object
- Object
- BERT::Encoder
- Defined in:
- lib/bert/encoder.rb
Class Method Summary collapse
-
.convert(item) ⇒ Object
Convert Ruby types into corresponding Erlectricity representation of BERT complex types.
-
.encode(ruby) ⇒ Object
Encode a Ruby object into a BERT.
Class Method Details
.convert(item) ⇒ Object
Convert Ruby types into corresponding Erlectricity representation of BERT complex 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 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bert/encoder.rb', line 17 def self.convert(item) case item when Hash pairs = Erl::List[] item.each_pair { |k, v| pairs << [convert(k), convert(v)] } [:bert, :dict, pairs] when Tuple item.map { |x| convert(x) } when Array Erl::List.new(item.map { |x| convert(x) }) when nil [:bert, :nil] when TrueClass [:bert, :true] when FalseClass [:bert, :false] when Time [:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec] when Regexp = Erl::List[] << :caseless if item. & Regexp::IGNORECASE > 0 << :extended if item. & Regexp::EXTENDED > 0 << :multiline if item. & Regexp::MULTILINE > 0 [:bert, :regex, item.source, ] else item end end |
.encode(ruby) ⇒ Object
Encode a Ruby object into a BERT.
+ruby+ is the Ruby object
Returns a BERT
7 8 9 10 |
# File 'lib/bert/encoder.rb', line 7 def self.encode(ruby) complex_ruby = convert(ruby) Erlectricity::Encoder.encode(complex_ruby) end |