27
27
end
28
28
29
29
it 'makes sure that :format_with as a proc cannot be used with a block' do
30
- expect { subject . expose :name , format_with : proc { } { } } . to raise_error ArgumentError
30
+ expect { subject . expose :name , format_with : proc { } { } } . to raise_error ArgumentError
31
31
end
32
32
end
33
33
51
51
module EntitySpec
52
52
class SomeObject1
53
53
attr_accessor :prop1
54
-
54
+
55
55
def initialize
56
56
@prop1 = "value1"
57
57
end
58
58
end
59
-
59
+
60
60
class BogusEntity < Grape ::Entity
61
61
expose :prop1
62
62
end
63
63
end
64
64
65
- subject . expose ( :bogus , using : EntitySpec ::BogusEntity ) { self . object . prop1 = "MODIFIED 2" ; self . object }
66
-
65
+ subject . expose ( :bogus , using : EntitySpec ::BogusEntity ) do
66
+ object . prop1 = "MODIFIED 2"
67
+ object
68
+ end
69
+
67
70
object = EntitySpec ::SomeObject1 . new
68
71
value = subject . represent ( object ) . send ( :value_for , :bogus )
69
72
value . should be_instance_of EntitySpec ::BogusEntity
70
-
73
+
71
74
prop1 = value . send ( :value_for , :prop1 )
72
75
prop1 . should == "MODIFIED 2"
73
76
end
@@ -131,21 +134,21 @@ class BogusEntity < Grape::Entity
131
134
model = { birthday : Time . gm ( 2012 , 2 , 27 ) }
132
135
subject . new ( double ( model ) ) . as_json [ :birthday ] . should == '02/27/2012'
133
136
end
134
-
137
+
135
138
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
136
139
object = Hash . new
137
-
138
- subject . expose ( :size , format_with : lambda { |value | self . object . class . to_s } )
140
+
141
+ subject . expose ( :size , format_with : lambda { |value | self . object . class . to_s } )
139
142
subject . represent ( object ) . send ( :value_for , :size ) . should == object . class . to_s
140
143
end
141
-
144
+
142
145
it 'formats an exposure with a :format_with symbol that returns a value from the entity instance' do
143
146
subject . format_with :size_formatter do |date |
144
147
self . object . class . to_s
145
148
end
146
149
147
150
object = Hash . new
148
-
151
+
149
152
subject . expose ( :size , format_with : :size_formatter )
150
153
subject . represent ( object ) . send ( :value_for , :size ) . should == object . class . to_s
151
154
end
@@ -512,7 +515,6 @@ class TestEntity < Grape::Entity
512
515
513
516
context '#serializable_hash' do
514
517
module EntitySpec
515
-
516
518
class EmbeddedExample
517
519
def serializable_hash ( opts = { } )
518
520
{ abc : 'def' }
@@ -697,7 +699,7 @@ class FriendEntity < Grape::Entity
697
699
rep . first . serializable_hash [ :email ] . should be_nil
698
700
rep . last . serializable_hash [ :email ] . should be_nil
699
701
700
- rep = subject . send ( :value_for , :friends , { user_type : :admin } )
702
+ rep = subject . send ( :value_for , :friends , user_type : :admin )
701
703
rep . should be_kind_of Array
702
704
rep . reject { |r | r . is_a? ( EntitySpec ::FriendEntity ) } . should be_empty
703
705
rep . first . serializable_hash [ :email ] . should ==
'[email protected] '
@@ -717,7 +719,7 @@ class FriendEntity < Grape::Entity
717
719
expose :friends , using : EntitySpec ::FriendEntity
718
720
end
719
721
720
- rep = subject . send ( :value_for , :friends , { collection : false } )
722
+ rep = subject . send ( :value_for , :friends , collection : false )
721
723
rep . should be_kind_of Array
722
724
rep . reject { |r | r . is_a? ( EntitySpec ::FriendEntity ) } . should be_empty
723
725
rep . first . serializable_hash [ :email ] . should ==
'[email protected] '
@@ -818,18 +820,18 @@ def name
818
820
exposure_options = { if : :condition1 }
819
821
820
822
subject . send ( :conditions_met? , exposure_options , { } ) . should be_false
821
- subject . send ( :conditions_met? , exposure_options , { condition1 : true } ) . should be_true
822
- subject . send ( :conditions_met? , exposure_options , { condition1 : false } ) . should be_false
823
- subject . send ( :conditions_met? , exposure_options , { condition1 : nil } ) . should be_false
823
+ subject . send ( :conditions_met? , exposure_options , condition1 : true ) . should be_true
824
+ subject . send ( :conditions_met? , exposure_options , condition1 : false ) . should be_false
825
+ subject . send ( :conditions_met? , exposure_options , condition1 : nil ) . should be_false
824
826
end
825
827
826
828
it 'looks for absence/falsiness if a symbol is passed' do
827
829
exposure_options = { unless : :condition1 }
828
830
829
831
subject . send ( :conditions_met? , exposure_options , { } ) . should be_true
830
- subject . send ( :conditions_met? , exposure_options , { condition1 : true } ) . should be_false
831
- subject . send ( :conditions_met? , exposure_options , { condition1 : false } ) . should be_true
832
- subject . send ( :conditions_met? , exposure_options , { condition1 : nil } ) . should be_true
832
+ subject . send ( :conditions_met? , exposure_options , condition1 : true ) . should be_false
833
+ subject . send ( :conditions_met? , exposure_options , condition1 : false ) . should be_true
834
+ subject . send ( :conditions_met? , exposure_options , condition1 : nil ) . should be_true
833
835
end
834
836
835
837
it 'only passes through proc :if exposure if it returns truthy value' do
0 commit comments