Author: bryanduxbury
Date: Tue Sep 21 16:30:58 2010
New Revision: 999487

URL: http://svn.apache.org/viewvc?rev=999487&view=rev
Log:
THRIFT-909. rb: allow block argument to struct constructor

This patch allows the user to pass a block to generated structs' constructors. 
The block will receive and instance of the new object.

Patch: Michael Stockton

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/spec/struct_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb?rev=999487&r1=999486&r2=999487&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Tue Sep 21 16:30:58 2010
@@ -21,7 +21,7 @@ require 'set'
 
 module Thrift
   module Struct
-    def initialize(d={})
+    def initialize(d={}, &block)
       # get a copy of the default values to work on, removing defaults in 
favor of arguments
       fields_with_defaults = fields_with_default_values.dup
       
@@ -50,6 +50,8 @@ module Thrift
           instance_variable_set("@#{name}", (default_value.dup rescue 
default_value))
         end
       end
+      
+      yield self if block_given?
     end
 
     def fields_with_default_values

Modified: incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/struct_spec.rb?rev=999487&r1=999486&r2=999487&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Tue Sep 21 16:30:58 2010
@@ -31,13 +31,22 @@ class ThriftStructSpec < Spec::ExampleGr
     end
 
     it "should initialize all fields to defaults" do
-      struct = Foo.new
-      struct.simple.should == 53
-      struct.words.should == "words"
-      struct.hello.should == Hello.new(:greeting => 'hello, world!')
-      struct.ints.should == [1, 2, 2, 3]
-      struct.complex.should be_nil
-      struct.shorts.should == Set.new([5, 17, 239])
+      validate_default_arguments(Foo.new)
+    end
+
+    it "should initialize all fields to defaults and accept a block argument" 
do
+      Foo.new do |f|
+        validate_default_arguments(f)
+      end
+    end
+
+    def validate_default_arguments(object)
+      object.simple.should == 53
+      object.words.should == "words"
+      object.hello.should == Hello.new(:greeting => 'hello, world!')
+      object.ints.should == [1, 2, 2, 3]
+      object.complex.should be_nil
+      object.shorts.should == Set.new([5, 17, 239])
     end
 
     it "should not share default values between instances" do


Reply via email to