312x Filetype PDF File size 2.94 MB Source: dl.icdst.org
Scala Cookbook: Bonus Chapters
Alvin Alexander
Scala Cookbook: Bonus Chapters
by Alvin Alexander
Copyright © 2013 Alvin Alexander. All rights reserved.
Printed in the United States of America.
Published by O°Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O°Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Courtney Nash Indexer: Ellen Troutman
Production Editor: Rachel Steely Cover Designer: Randy Comer
Copyeditor: Kim Cofer Interior Designer: David Futato
Proofreader: Linley Dolby Illustrator: Rebecca Demarest
August 2013: First Edition
Revision History for the First Edition:
2013-07-30: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449339616 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O°Reilly logo are registered trademarks of O°Reilly
Media, Inc. Scala Cookbook, the image of a long-beaked echidna, and related trade dress are trademarks of
O°Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O°Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-33961-6
[LSI]
Table of Contents
XML and XPath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1. Creating XML Literals 5
2. Mixing Scala and XML 8
3. Extracting Data from XML Nodes 10
4. Basic XPath Searching with \ and \\ 15
5. Deeper XML Parsing and Extracting Tag Attributes 19
6. Extracting Data from an Array of XML Elements 22
7. Using Match Expressions with XML 25
8. Serializing and Deserializing XML 30
9. Loading XML from a URL 35
10. Loading XML from a File 38
11. Saving XML to a File 40
12. Displaying XML in a Human-Readable Format (Pretty Printing) 42
Testing and Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1. Installing ScalaTest 45
2. Writing TDD Tests with ScalaTest 46
3. Writing a First BDD Test with ScalaTest 49
4. Adding Given/When/Then Behavior to BDD Tests 51
5. Adding More Tests and Test Suites 54
6. Printing Expected and Actual Values When a Test Fails 55
7. Marking a Test as Pending 56
8. Testing Expected Exceptions 57
9. Mark Your Tests with Tags So You Can Include or Exclude Them 58
10. Temporarily Disabling a Test 60
11. Using Mock Objects with ScalaTest 61
12. Java-Style Logging with SLF4J 64
13. Scala-Style Logging with Grizzled-SLF4J 65
iii
14. Using JUnit with Scala 67
The Play Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
1. Creating a ¾Hello, WorldÀ Project 72
2. Adding a Route, Model, and Controller Method to a Play Application 81
3. Using Multiple Template Wrappers 85
4. Creating Reusable Code Blocks in Templates 86
5. Calling Scala Functions from Templates 89
6. Creating a Widget and Including It in Pages 91
7. Using CoffeeScript and LESS 93
8. Creating a Simple Form 94
9. Validating a Form 102
10. Displaying and Validating Common Play Form Elements 111
11. Selecting from a Database with Anorm 118
12. Inserting Data into a Database with Anorm 124
13. Deleting Records in a Database Table with Anorm 129
14. Updating Records in a Database Table with Anorm 130
15. Testing Queries Outside of Play 131
16. Deploying a Play Framework Project 134
17. Handling 404 and 500 Errors 138
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
iv | Table of Contents
no reviews yet
Please Login to review.