Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
191 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
= | ||
* Birthday! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rubygems' | ||
require 'erlectricity' | ||
|
||
require 'bert/encoder' | ||
require 'bert/decoder' | ||
|
||
module BERT | ||
def self.encode(ruby) | ||
Encoder.encode(ruby) | ||
end | ||
|
||
def self.decode(bert) | ||
Decoder.decode(bert) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module BERT | ||
class Decoder | ||
# Decode a BERT into a Ruby object. | ||
# +bert+ is the BERT String | ||
# | ||
# Returns a Ruby object | ||
def self.decode(bert) | ||
simple_ruby = Erlectricity::Decoder.decode(bert) | ||
convert(simple_ruby) | ||
end | ||
|
||
# Convert Erlectricity representation of BERT complex types into | ||
# corresponding Ruby types. | ||
# +item+ is the Ruby object to convert | ||
# | ||
# Returns the converted Ruby object | ||
def self.convert(item) | ||
case item | ||
when [:nil, :nil] | ||
nil | ||
when Array | ||
if item.first == :dict | ||
item[1..-1].inject({}) do |acc, x| | ||
acc[convert(x[0])] = convert(x[1]); acc | ||
end | ||
else | ||
item.map { |x| convert(x) } | ||
end | ||
else | ||
item | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module BERT | ||
class Encoder | ||
# Encode a Ruby object into a BERT. | ||
# +ruby+ is the Ruby object | ||
# | ||
# Returns a BERT | ||
def self.encode(ruby) | ||
complex_ruby = convert(ruby) | ||
Erlectricity::Encoder.encode(complex_ruby) | ||
end | ||
|
||
# Convert Ruby types into corresponding Erlectricity representation | ||
# of BERT complex types. | ||
# +item+ is the Ruby object to convert | ||
# | ||
# Returns the converted Ruby object | ||
def self.convert(item) | ||
case item | ||
when Hash | ||
a = [:dict] | ||
item.each_pair { |k, v| a << [convert(k), convert(v)] } | ||
a | ||
when Array | ||
item.map { |x| convert(x) } | ||
when nil | ||
[:nil, :nil] | ||
else | ||
item | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
require 'test_helper' | ||
|
||
class BertTest < Test::Unit::TestCase | ||
should "probably rename this file and start testing for real" do | ||
flunk "hey buddy, you should probably rename this file and start testing for real" | ||
context "BERT" do | ||
should "encode" do | ||
before = [:user, {:name => 'TPW', :nick => 'mojombo'}] | ||
after = "\203h\002d\000\004userh\003d\000\004dicth\002d\000\004namem\000\000\000\003TPWh\002d\000\004nickm\000\000\000\amojombo" | ||
assert_equal after, BERT.encode(before) | ||
end | ||
|
||
should "decode" do | ||
before = "\203h\002d\000\004userh\003d\000\004dicth\002d\000\004namem\000\000\000\003TPWh\002d\000\004nickm\000\000\000\amojombo" | ||
after = [:user, {:name => 'TPW', :nick => 'mojombo'}] | ||
assert_equal after, BERT.decode(before) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'test_helper' | ||
|
||
class DecoderTest < Test::Unit::TestCase | ||
context "BERT Decoder complex type converter" do | ||
should "convert nil" do | ||
before = [:nil, :nil] | ||
after = nil | ||
assert_equal after, BERT::Decoder.convert(before) | ||
end | ||
|
||
should "convert nested nil" do | ||
before = [[:nil, :nil], [[:nil, :nil]]] | ||
after = [nil, [nil]] | ||
assert_equal after, BERT::Decoder.convert(before) | ||
end | ||
|
||
should "convert hashes" do | ||
before = [:dict, [:foo, 'bar']] | ||
after = {:foo => 'bar'} | ||
assert_equal after, BERT::Decoder.convert(before) | ||
end | ||
|
||
should "convert nested hashes" do | ||
before = [:dict, [:foo, [:dict, [:baz, 'bar']]]] | ||
after = {:foo => {:baz => 'bar'}} | ||
assert_equal after, BERT::Decoder.convert(before) | ||
end | ||
|
||
should "leave other stuff alone" do | ||
before = [1, 2.0, [:foo, 'bar']] | ||
assert_equal before, BERT::Decoder.convert(before) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'test_helper' | ||
|
||
class EncoderTest < Test::Unit::TestCase | ||
context "BERT Encoder complex type converter" do | ||
should "convert nil" do | ||
assert_equal [:nil, :nil], BERT::Encoder.convert(nil) | ||
end | ||
|
||
should "convert nested nil" do | ||
before = [nil, [nil]] | ||
after = [[:nil, :nil], [[:nil, :nil]]] | ||
assert_equal after, BERT::Encoder.convert(before) | ||
end | ||
|
||
should "convert hashes" do | ||
before = {:foo => 'bar'} | ||
after = [:dict, [:foo, 'bar']] | ||
assert_equal after, BERT::Encoder.convert(before) | ||
end | ||
|
||
should "convert nested hashes" do | ||
before = {:foo => {:baz => 'bar'}} | ||
after = [:dict, [:foo, [:dict, [:baz, 'bar']]]] | ||
assert_equal after, BERT::Encoder.convert(before) | ||
end | ||
|
||
should "leave other stuff alone" do | ||
before = [1, 2.0, [:foo, 'bar']] | ||
assert_equal before, BERT::Encoder.convert(before) | ||
end | ||
end | ||
end |