diff --git a/lib/sensitive.rb b/lib/sensitive.rb new file mode 100644 index 0000000..7219426 --- /dev/null +++ b/lib/sensitive.rb @@ -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 diff --git a/spec/sensitive_spec.rb b/spec/sensitive_spec.rb new file mode 100644 index 0000000..a5b91c2 --- /dev/null +++ b/spec/sensitive_spec.rb @@ -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 diff --git a/test/test_sensitive.rb b/test/test_sensitive.rb new file mode 100644 index 0000000..6175295 --- /dev/null +++ b/test/test_sensitive.rb @@ -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