1
1
require 'spec_helper'
2
2
3
- class TestUser < Struct . new ( :id , :name , :credit_card , :secret_token )
3
+ class TestUser < Struct . new ( :id , :name , :credit_card , :secret_token , :quote )
4
4
extend Hashifiable
5
5
hashify :id ,
6
6
:name ,
7
+ 'quote' ,
8
+ 'random' => Proc . new { [ 1 , 2 , 3 ] . sample } ,
7
9
:two_times_two => Proc . new { 2 * 2 } ,
8
10
:encrypted_token => Proc . new { secret_token + ' secret sauce' } ,
9
11
:lambdas_at_work => -> ( ) { 2 * 2 }
10
12
end
11
13
12
14
describe Hashifiable do
13
- let ( :user ) { TestUser . new ( 1 , 'pote' , '1123123241241' , 'i2j34i2j34302843' ) }
15
+ let ( :user ) { TestUser . new ( 1 , 'pote' , '1123123241241' , 'i2j34i2j34302843' , "It's bigger on the inside!" ) }
14
16
15
17
it 'should have a to_h method' do
16
18
user . should respond_to ( :to_h )
@@ -36,10 +38,18 @@ class TestUser < Struct.new(:id, :name, :credit_card, :secret_token)
36
38
user . to_h [ :id ] . should == 2
37
39
end
38
40
41
+ it 'should include strings as well as symbols on the hash representation' do
42
+ user . to_h [ 'quote' ] . should == "It's bigger on the inside!"
43
+ end
44
+
39
45
it 'should include procs in the hash representation' do
40
46
user . to_h . keys . should include ( :two_times_two )
41
47
end
42
48
49
+ it 'should allow procs to have strings as keys' do
50
+ user . to_h [ 'random' ] . class . should == Fixnum
51
+ end
52
+
43
53
it 'should return the output of the function and not a proc' do
44
54
user . to_h [ :two_times_two ] . should == 4
45
55
end
0 commit comments