Class: Rencdec
- Inherits:
-
Object
- Object
- Rencdec
- Defined in:
- lib/rencdec.rb
Instance Attribute Summary collapse
-
#current_data ⇒ Object
readonly
Returns the value of attribute current_data.
-
#current_encoding ⇒ Object
readonly
Returns the value of attribute current_encoding.
-
#current_radix ⇒ Object
readonly
Returns the value of attribute current_radix.
-
#target_data ⇒ Object
readonly
Returns the value of attribute target_data.
-
#target_encoding ⇒ Object
readonly
Returns the value of attribute target_encoding.
-
#target_radix ⇒ Object
readonly
Returns the value of attribute target_radix.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #decode ⇒ Object
- #encode ⇒ Object
-
#initialize(params) ⇒ Rencdec
constructor
A new instance of Rencdec.
- #verify ⇒ Object
Constructor Details
#initialize(params) ⇒ Rencdec
Returns a new instance of Rencdec.
5 6 7 8 9 10 |
# File 'lib/rencdec.rb', line 5 def initialize(params) @current_encoding, @target_encoding, @current_data = params[:current_encoding], params[:target_encoding], params[:current_data] @current_radix = Radix::Base.new current_encoding @target_radix = Radix::Base.new target_encoding end |
Instance Attribute Details
#current_data ⇒ Object (readonly)
Returns the value of attribute current_data.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def current_data @current_data end |
#current_encoding ⇒ Object (readonly)
Returns the value of attribute current_encoding.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def current_encoding @current_encoding end |
#current_radix ⇒ Object (readonly)
Returns the value of attribute current_radix.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def current_radix @current_radix end |
#target_data ⇒ Object (readonly)
Returns the value of attribute target_data.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def target_data @target_data end |
#target_encoding ⇒ Object (readonly)
Returns the value of attribute target_encoding.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def target_encoding @target_encoding end |
#target_radix ⇒ Object (readonly)
Returns the value of attribute target_radix.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def target_radix @target_radix end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
4 5 6 |
# File 'lib/rencdec.rb', line 4 def verbose @verbose end |
Instance Method Details
#decode ⇒ Object
16 17 18 19 20 |
# File 'lib/rencdec.rb', line 16 def decode tmp_target = target_data.map{|t| target_radix.values[t.to_s]} tmp_current = Radix.convert_base(tmp_target, target_radix.base, current_radix.base) @current_data ||= tmp_current.map{|t| tmp = current_radix.values.invert[t]; tmp[/^\d+$/] ? tmp.to_i : tmp.to_sym } end |
#encode ⇒ Object
11 12 13 14 15 |
# File 'lib/rencdec.rb', line 11 def encode tmp_current = current_data.map{|c| current_radix.values[c.to_s]} tmp_target = Radix.convert_base(tmp_current, current_radix.base, target_radix.base) @target_data ||= tmp_target.map{|t| tmp = target_radix.values.invert[t]; tmp[/^\d+$/] ? tmp.to_i : tmp.to_sym } end |
#verify ⇒ Object
21 22 23 |
# File 'lib/rencdec.rb', line 21 def verify encode == target_data && decode == current_data end |