Class: Rencdec

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dataObject (readonly)

Returns the value of attribute current_data.



4
5
6
# File 'lib/rencdec.rb', line 4

def current_data
  @current_data
end

#current_encodingObject (readonly)

Returns the value of attribute current_encoding.



4
5
6
# File 'lib/rencdec.rb', line 4

def current_encoding
  @current_encoding
end

#current_radixObject (readonly)

Returns the value of attribute current_radix.



4
5
6
# File 'lib/rencdec.rb', line 4

def current_radix
  @current_radix
end

#target_dataObject (readonly)

Returns the value of attribute target_data.



4
5
6
# File 'lib/rencdec.rb', line 4

def target_data
  @target_data
end

#target_encodingObject (readonly)

Returns the value of attribute target_encoding.



4
5
6
# File 'lib/rencdec.rb', line 4

def target_encoding
  @target_encoding
end

#target_radixObject (readonly)

Returns the value of attribute target_radix.



4
5
6
# File 'lib/rencdec.rb', line 4

def target_radix
  @target_radix
end

#verboseObject (readonly)

Returns the value of attribute verbose.



4
5
6
# File 'lib/rencdec.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#decodeObject



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

#encodeObject



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

#verifyObject



21
22
23
# File 'lib/rencdec.rb', line 21

def verify
	encode == target_data && decode == current_data
end