add class to wrap sensitive values
This commit is contained in:
		
							
								
								
									
										39
									
								
								lib/sensitive.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								lib/sensitive.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
class Sensitive
 | 
			
		||||
  alias_method :eql?, :==
 | 
			
		||||
  alias_method :equal?, :==
 | 
			
		||||
 | 
			
		||||
  def initialize(v, ch: "*", head: 2, tail: 2)
 | 
			
		||||
    @v = v
 | 
			
		||||
    @ch = ch
 | 
			
		||||
    @head = head
 | 
			
		||||
    @tail = tail
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def mask(v)
 | 
			
		||||
    "".concat(v[0, @head], @ch * (v.length - (@head + @tail)), v[-@tail, @tail])
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def unwrap
 | 
			
		||||
    @v
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def length
 | 
			
		||||
    @v.length
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def to_s
 | 
			
		||||
    mask @v
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def inspect
 | 
			
		||||
    "#<#{self.class.name} @v=#{wrap}>"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def hash
 | 
			
		||||
    @v.hash
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def ==(other)
 | 
			
		||||
    other.is_a?(Sensitive) && other.hash == hash
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										26
									
								
								spec/sensitive_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								spec/sensitive_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
require "minitest/autorun"
 | 
			
		||||
 | 
			
		||||
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
 | 
			
		||||
 | 
			
		||||
require "sensitive"
 | 
			
		||||
 | 
			
		||||
ALPHABET = ('a' .. 'z').reduce(:concat)
 | 
			
		||||
 | 
			
		||||
describe "Sensitive" do
 | 
			
		||||
  before do
 | 
			
		||||
    @s = Sensitive.new ALPHABET
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "test initialize" do
 | 
			
		||||
    _(@s.to_s).must_equal "ab" + "*" * 22 + "yz"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "test initialize" do
 | 
			
		||||
    _(@s.unwrap).must_equal ALPHABET
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "test using different mask character" do
 | 
			
		||||
    s = Sensitive.new ALPHABET, ch: "x" 
 | 
			
		||||
    _(s.to_s).must_equal "x"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										26
									
								
								test/test_sensitive.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								test/test_sensitive.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
require "minitest/autorun"
 | 
			
		||||
 | 
			
		||||
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
 | 
			
		||||
 | 
			
		||||
require "sensitive"
 | 
			
		||||
 | 
			
		||||
ALPHABET = ("a".."z").reduce(:concat)
 | 
			
		||||
 | 
			
		||||
class TestSensitive < Minitest::Test
 | 
			
		||||
  def setup
 | 
			
		||||
    @s = Sensitive.new ALPHABET
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_initialize
 | 
			
		||||
    assert_equal @s.to_s, "ab" + "*" * 22 + "yz"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_unwrap
 | 
			
		||||
    assert_equal @s.unwrap, ALPHABET
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_using_a_different_mask_character
 | 
			
		||||
    s = Sensitive.new ALPHABET, ch: "x"
 | 
			
		||||
    assert_equal s.to_s, "ab" + "x" * 22 + "yz"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user