Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: Allow private-scoped after_initialize hooks (#301)

`after_initialize` hook comes very handy with `Literal::Data`, when we
want to, for example freeze some of the complex properties passed in.
But making this method public seems a bit incorrect.

authored by

Alexey Zapparov and committed by
GitHub
0ee6e62a 39b44e31

+39 -3
+1 -1
lib/literal/properties/schema.rb
··· 62 62 end 63 63 64 64 def generate_after_initializer(buffer = +"") 65 - buffer << " after_initialize if respond_to?(:after_initialize)\n" 65 + buffer << " after_initialize if respond_to?(:after_initialize, true)\n" 66 66 end 67 67 68 68 def generate_to_h(buffer = +"")
+38 -2
test/properties.test.rb
··· 182 182 test "after initialize callback" do 183 183 callback_called = false 184 184 185 - example = Class.new do 185 + public_callback = Class.new do 186 186 extend Literal::Properties 187 187 188 188 prop :name, String ··· 192 192 end 193 193 end 194 194 195 - example.new(name: "John") 195 + public_callback.new(name: "John") 196 + 197 + assert callback_called 198 + 199 + callback_called = false 200 + 201 + protected_callback = Class.new do 202 + extend Literal::Properties 203 + 204 + prop :name, String 205 + 206 + define_method :after_initialize do 207 + callback_called = true 208 + end 209 + 210 + protected :after_initialize 211 + end 212 + 213 + protected_callback.new(name: "John") 214 + 215 + assert callback_called 216 + 217 + callback_called = false 218 + 219 + private_callback = Class.new do 220 + extend Literal::Properties 221 + 222 + prop :name, String 223 + 224 + define_method :after_initialize do 225 + callback_called = true 226 + end 227 + 228 + private :after_initialize 229 + end 230 + 231 + private_callback.new(name: "John") 196 232 197 233 assert callback_called 198 234