Created
December 21, 2011 19:22
-
-
Save lenny/1507301 to your computer and use it in GitHub Desktop.
ruby block interface for anonymous class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ex. Java invocation | |
public void save() { | |
txTemplate.execute(new TxCallback() { | |
public Object execute(TransactionHolder tx) throws Exception { | |
//do stuff | |
return something; | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ex. ruby invocation | |
Transaction.execute { something } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction | |
class CallbackWrapper | |
include Java::org.aps.eop.hibernate.springtx.TxCallback | |
def initialize(&blk) | |
@blk = blk | |
end | |
def execute(tx) | |
@blk.call | |
end | |
end | |
def self.execute(&blk) | |
txTemplate = SpringAppContext.get_bean(bean_name) | |
txTemplate.execute(CallbackWrapper.new(&blk)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface TxCallback { | |
public Object execute(TransactionHolder txHolder) throws Exception; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TxTemplate { | |
.... | |
public Object execute(TxCallback callback) throws Exception { | |
return callback.execute(txUtil.getTransaction()); | |
} | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment