Module: Lebowski::Runtime::ObjectEncoder
- Defined in:
- lib/lebowski/runtime/object_encoder.rb
Overview
Used to encode Ruby values into an encoded string that can be sent over to the browser to then be decoded and used.
TODO: This needs to be completely changed. Instead of using a custom encoding,
should instead use a standard encoding scheme, like JSON. Got too
experimental in this case.
Constant Summary collapse
- COMMA_CHAR =
','- COLON_CHAR =
':'- EQUAL_CHAR =
'='- LEFT_SQUARE_BRACKET_CHAR =
'['- RIGHT_SQUARE_BRACKET_CHAR =
']'- ENCODED_COMMA =
"[cma]"- ENCODED_COLON =
"[cln]"- ENCODED_EQUAL =
"[eql]"- ENCODED_LEFT_SQUARE_BRACKET =
"[lsb]"- ENCODED_RIGHT_SQUARE_BRACKET =
"[rsb]"
Class Method Summary collapse
- .encode(value) ⇒ Object
-
.encode_array(array) ⇒ Object
Encodes a given array into a string representation that can be sent over to the remote browser via Selenium.
-
.encode_hash(hash) ⇒ Object
Encodes a given hash into a string representation that can be sent over to the remote browser via Selenium.
-
.encode_string(string) ⇒ Object
Encodes special characters in a string.
- .encode_value(value) ⇒ Object
Class Method Details
.encode(value) ⇒ Object
31 32 33 34 35 |
# File 'lib/lebowski/runtime/object_encoder.rb', line 31 def self.encode(value) return encode_hash(value) if value.kind_of? Hash return encode_array(value) if value.kind_of? Array return nil end |
.encode_array(array) ⇒ Object
Encodes a given array into a string representation that can be sent over to the remote browser via Selenium
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lebowski/runtime/object_encoder.rb', line 67 def self.encode_array(array) str = "" counter = 1 array.each do |value| str << encode_value(value) str << "," if counter < array.length counter = counter.next end return str end |
.encode_hash(hash) ⇒ Object
Encodes a given hash into a string representation that can be sent over to the remote browser via Selenium. Examples of encoding:
{ 'foo' => "bar"} # => foo=bar
{ :foo => "bar"} # => foo=bar
{ 'foo' => "cat", => "dog"} # => foo=cat,bar=dog
{ 'foo' => /bar/ } # => foo=regexp:bar
{ 'foo' => /bar/i } # => foo=regexpi:bar
{ 'foo.bar' => "cat" } # => foo.bar=cat
{ 'foo' => "Acme, Inc."} # => foo=Acme[comma] Inc.
{ 'foo' => 100 } # => foo=int:100
{ 'foo' => true } # => foo=bool:true
{ 'foo' => false } # => foo=bool:false
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lebowski/runtime/object_encoder.rb', line 52 def self.encode_hash(hash) str = "" counter = 1 hash.each do |key, value| str << key.to_s << "=" << encode_value(value) str << "," if counter < hash.length counter = counter.next end return str end |
.encode_string(string) ⇒ Object
Encodes special characters in a string. Characters encoded are:
, : = [ ]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/lebowski/runtime/object_encoder.rb', line 107 def self.encode_string(string) new_string = string.gsub(/[,=:\[\]]/) do |c| case c when COMMA_CHAR ENCODED_COMMA when COLON_CHAR ENCODED_COLON when EQUAL_CHAR ENCODED_EQUAL when LEFT_SQUARE_BRACKET_CHAR ENCODED_LEFT_SQUARE_BRACKET when RIGHT_SQUARE_BRACKET_CHAR ENCODED_RIGHT_SQUARE_BRACKET end end return new_string end |
.encode_value(value) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lebowski/runtime/object_encoder.rb', line 78 def self.encode_value(value) str = "" if value.kind_of? Regexp str << "regexp" str << "i" if (value. & Regexp::IGNORECASE) == Regexp::IGNORECASE str << ":" << encode_string(value.source) elsif value.kind_of? Integer str << "int:" << value.to_s elsif value.kind_of? TrueClass or value.kind_of? FalseClass str << "bool:" << value.to_s elsif value.kind_of? Hash str << "hash:" << encode_string(encode_hash(value)) elsif value.kind_of? Array str << "array:" << encode_string(encode_array(value)) elsif value.nil? str << "null:null" elsif value == :undefined str << "undefined:undefined" else str << encode_string(value.to_s) end return str end |