<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kschidam</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kschidam"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Kschidam"/>
	<updated>2026-08-14T23:08:13Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch6_6b_SK&amp;diff=41026</id>
		<title>CSC/ECE 517 Fall 2010/ch6 6b SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch6_6b_SK&amp;diff=41026"/>
		<updated>2010-11-17T04:42:45Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ==&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in C++''' =&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] the support for assertions is pretty limited. Assertion is mainly used for error detection during debugging. If an assertion fails ,the program would stop at that point and indicate which assertion caused the failure. &lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
In C++ assertions are defined in the header file [http://en.wikipedia.org/wiki/Assert.h Assert.h].&lt;br /&gt;
&lt;br /&gt;
* assert (expression);&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
       assert(isAdmin == true);&lt;br /&gt;
&lt;br /&gt;
Similar to other languages , assertions can be turned off in C++ if the NDEBUG macro is defined before the inclusion of the header Assert.h .&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in C++''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing C++ code are API Sanity Autotest, C++test, Cantata++, CppUnit and CppTest. To see the complete list of xUnit and other testing frameworks supported by C++ please refer to [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [8];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you to refactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. As seen above assertions are natively supported in some languages like Ruby, whereas in languages like C++ and python it is merely used as a mechanism to track errors when debugging.&lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the adhoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40765</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40765"/>
		<updated>2010-11-16T06:32:58Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40760</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40760"/>
		<updated>2010-11-16T06:16:12Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ==&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you to refactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40759</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40759"/>
		<updated>2010-11-16T06:14:07Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ==&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40740</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40740"/>
		<updated>2010-11-16T04:45:16Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ==&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40739</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40739"/>
		<updated>2010-11-16T04:44:25Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ==&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ==&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40738</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40738"/>
		<updated>2010-11-16T04:43:38Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Java''' ===&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing Java code are JUnit , SpryTest , Jtest , TestNG and JExample. Other testing frameworks supported by Java can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Ruby''' ===&lt;br /&gt;
&lt;br /&gt;
The only xUnit framework that support testing Ruby code is Test::Unit. Other testing frameworks that support Ruby but that do not fall under the xUnit umbrella are RSpec , Shoulda , microtest and Bacon.&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
== '''xUnit Frameworks that support testing in Python''' ===&lt;br /&gt;
&lt;br /&gt;
Some of the xUnit frameworks that support testing python code are PyUnit, Nose, py.test and TwistedTrial. Other testing frameworks supported by python can be looked up at [9]&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks] Testing frameworks for programming languages&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/XUnit] xUnit&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40732</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40732"/>
		<updated>2010-11-16T04:34:39Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
== Simple Assert ==&lt;br /&gt;
&lt;br /&gt;
The following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
* assert condition&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
== Extended Assert ==&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
* assert Expression[, Arguments]&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40723</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40723"/>
		<updated>2010-11-16T04:18:40Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
For example, the following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
'''Syntax: assert condition'''&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
'''Syntax: assert Expression[, Arguments]'''&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40722</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40722"/>
		<updated>2010-11-16T04:18:06Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in python''' =&lt;br /&gt;
In [http://www.python.org/ Python] when the [http://docs.python.org/reference/simple_stmts.html#the-assert-statement assert] statement is encountered, the expression following the assert keyword is evaluated and the AssertionError exception is raised if the expression evaluates to false.&lt;br /&gt;
&lt;br /&gt;
For example, the following code would raise the [http://docs.python.org/library/exceptions.html#exceptions.AssertionError AssertionError] exception.&lt;br /&gt;
&lt;br /&gt;
'''Syntax: assert condition'''&lt;br /&gt;
&lt;br /&gt;
Example [1]:&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       assert 1 == 2&lt;br /&gt;
&lt;br /&gt;
In addition to evaluating an expression , arguments can be passed to the assertion so that for example, the right message can be displayed based on the outcome of the assertion.&lt;br /&gt;
&lt;br /&gt;
'''Syntax: assert Expression[, Arguments]'''&lt;br /&gt;
&lt;br /&gt;
Example [2];&lt;br /&gt;
       #!/usr/bin/python&lt;br /&gt;
       def IsSenior(age):&lt;br /&gt;
          assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
          return (age &amp;gt;= 65)&lt;br /&gt;
       print IsSenior(15)&lt;br /&gt;
       print IsSenior(70)&lt;br /&gt;
       print IsSenior(-1)&lt;br /&gt;
&lt;br /&gt;
Example [2] Output;&lt;br /&gt;
       false&lt;br /&gt;
       true&lt;br /&gt;
       Traceback (most recent call last):&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;br /&gt;
           print IsSenior(-5)&lt;br /&gt;
         File &amp;quot;test.py&amp;quot;, line 4, in IsSenior&lt;br /&gt;
           assert (age &amp;gt;= 1),&amp;quot;Age cannot be zero or less than zero !&amp;quot;&lt;br /&gt;
       AssertionError: Age cannot be zero or less than zero !&lt;br /&gt;
&lt;br /&gt;
One common used of assertions in python is for type checking. Please refer the article [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki3_1_sa#Python Assertion in O-O languages] for more details.&lt;br /&gt;
&lt;br /&gt;
One of the key feature of assertions in Python is that if python is started with -O option, then the assertions would not be evaluated. The scenario where this feature would apply is developers can write a lot of assertions for debugging and when the code is to be executed in production , if it is started with -O option , then all the assertions written by the developers for debugging would not be evaluated.&lt;br /&gt;
&lt;br /&gt;
In general assertions are not the best means to test for failure cases. Instead of using an assertion exceptions would be the right way to handle scenarios like wrong user input or system/environment failures.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/reference/simple_stmts.html] Python simple statements documentation&lt;br /&gt;
&lt;br /&gt;
[http://www.tutorialspoint.com/python/assertions_in_python.htm] Assertions in Python&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40709</id>
		<title>Zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Zz&amp;diff=40709"/>
		<updated>2010-11-16T03:38:20Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Introduction''' =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In computer language an [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a construct that immediately terminates the execution of a program if a certain expression or a condition is evaluated to false (assertion failure). It is mainly used for code [http://en.wikipedia.org/wiki/Debugging debugging] . Programmers use assertions to check for potential errors or bugs in the application being developed. The main feature of assertions is to verify the validity of the assumptions made by chunk of code during execution. A good example to illustrate this is the use of [http://en.wikipedia.org/wiki/Dynamic_memory_allocation dynamic memory allocation] in C++, wherein we can use an assertion to check a [http://en.wikipedia.org/wiki/Pointer_(computing) pointer] and ensure that it is not null before using this pointer. If this check in not made, a reference may occur later that would cause an error. Assertions play a vital role in developing reliable object-oriented software. An early advocate of using assertions in programming was Alan Turing [5]. Assertions serve to make explicit the assumptions on which programmers rely when they write software elements that they believe are correct. Assertion-based Object-Oriented techniques produce reliable software and enable software components to be reused safely. In languages such as Eiffel, assertions form part of the design process, and in others, such as C and Java, they are used only to check assumptions at runtime. Various object oriented programming languages support assertions. &lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in java''' =&lt;br /&gt;
&lt;br /&gt;
When implementing and debugging a class in java programming language, it is a good practice to specify conditions that should be true at a particular stage in a method. These conditions, called assertions, guarantee a program’s validity by catching potential bugs and identifying possible logic errors during development. For example, if you write a module that calculates the temperature of an element, you might assert that the calculated temperature is not less than 0 degree Kelvin. The syntax for assert statements are as follows [1]:&lt;br /&gt;
&lt;br /&gt;
          assert Expression1;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a [http://en.wikipedia.org/wiki/Boolean_expression Boolean expression]. If Expression1 is evaluated to be false, then the system throws an AssertionError. This syntax for assert will not give a detail error message. Therefore second form of assert syntax can be used [1] as given below&lt;br /&gt;
         assert Expression1: Expression2;&lt;br /&gt;
&lt;br /&gt;
Expression1 is a Boolean expression. Expression2 is an expression that has a value. This version of the assert statement provides detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message. This form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.  Below is an example code that demonstrates the functionality of assert statement. This code checks with assert that the value entered is an even number only [2]. &lt;br /&gt;
&lt;br /&gt;
   import java.util.Scanner;&lt;br /&gt;
   &lt;br /&gt;
  	public class AssertTest&lt;br /&gt;
   {&lt;br /&gt;
       public static void main( String args[] )&lt;br /&gt;
      {&lt;br /&gt;
           Scanner input = new Scanner( System.in );&lt;br /&gt;
          &lt;br /&gt;
          System.out.print( &amp;quot;Enter an even number:  &amp;quot; );&lt;br /&gt;
         int number = input.nextInt();&lt;br /&gt;
          &lt;br /&gt;
        // assert that the number is even&lt;br /&gt;
         assert ((number % 2 == 0)) : &amp;quot;Not an even number: &amp;quot; + number;&lt;br /&gt;
 &lt;br /&gt;
       System.out.printf( &amp;quot;You entered an even number %d\n&amp;quot;, number );&lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
       Enter an even number:  10&lt;br /&gt;
       You entered an even number 10&lt;br /&gt;
       &lt;br /&gt;
       Enter an even number:  25&lt;br /&gt;
       Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Not an even number:  25&lt;br /&gt;
       at AssertTest.main(AssertTest.java:15)&lt;br /&gt;
&lt;br /&gt;
The above code prompts the user to enter an even number, then this number is read from command prompt.  The assert statement then determines whether the user entered an even or odd number. If the user entered an odd number (as in second case of output), then the program throws an error. Otherwise, the program proceeds normally. Any line that executes after the assert statement can safely assume that number is not odd&lt;br /&gt;
&lt;br /&gt;
One obvious question that may arise is when exceptions can do the [http://en.wikipedia.org/wiki/Exception_handling error handling] why we need another level of checking. Java exceptions are primarily used to handle unusual conditions arising during program execution.  Assertions are not to replace exceptions but to augment them. Assertions are used to specify conditions that a programmer assumes are true. When programming, if a programmer can swear that the value being passed into a particular method is positive no matter what a calling client passes, it can be documented using an assertion to state it. Exceptions handle abnormal conditions arising in the course of the program; however they do not guarantee smooth or correct execution of the program. Assertions help state scenarios that ensure the program is running smoothly. Assertions can be efficient tools to ensure correct execution of a program. They improve the confidence about the program.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Assertions''' ==&lt;br /&gt;
* Preconditions - These are assertions about a program’s state when a method is invoked. Precondition refers to the parameters passed to a method in a program. Precondition asserts check the validity of parameters passed before they get used in the body of the method. &lt;br /&gt;
* Postconditions - These are assertions about a program’s state after a method finishes execution. Postcondition  should be evaluated before the exit point in a method. Postcondition asserts can be used to check for the validity of the return values in a method that has multiple return statements.&lt;br /&gt;
&lt;br /&gt;
One situation where use of assertions is helpful in Java programming language is : Internal Invariants [1]. Assertions can be used within programs to make sure the program behaves in a predetermined manner and will throw an error when violated. For instance, an assertion can be placed in the code below to declare that age will never be negative.&lt;br /&gt;
&lt;br /&gt;
           if (age &amp;gt; 0)&lt;br /&gt;
           {&lt;br /&gt;
              age = age + 1;&lt;br /&gt;
           } &lt;br /&gt;
           else&lt;br /&gt;
           {&lt;br /&gt;
       		assert age &amp;gt;0:&amp;quot;Age cannot be negative&amp;quot;&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== '''Enabling and Disabling Assertions'''==&lt;br /&gt;
&lt;br /&gt;
At runtime assertions are disabled by default as they reduce performance. To enable assertions at [http://en.wikipedia.org/wiki/Run_time_(computing) runtime], use the -ea command-line option. To disable assertions, use –da command line option. To execute a code with assertions enabled use&lt;br /&gt;
         java -ea AssertTest&lt;br /&gt;
&lt;br /&gt;
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class.&lt;br /&gt;
&lt;br /&gt;
= '''Support for Assertions in Ruby''' =&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is a process where individual parts of [http://en.wikipedia.org/wiki/Source_code source code] are isolated and tested separately to determine if they are bug free. The idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby language] supports a module called Test::Unit::Assertions in test/unit/assertions.rb. Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is includedin Test::Unit::TestCase. &lt;br /&gt;
&lt;br /&gt;
=='''Public class assert methods'''==&lt;br /&gt;
&lt;br /&gt;
*assert( boolean, [msg] )- This ensures that the object/expression is true&lt;br /&gt;
  assert [10, 20].include?(50)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_block(message=&amp;quot;assert_block failed.&amp;quot;) {|| ...} - If the block yields to true , then the assert passes&lt;br /&gt;
Example [3]:&lt;br /&gt;
       def assert_block(message=&amp;quot;assert_block failed.&amp;quot;) # :yields: &lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          if (! yield)&lt;br /&gt;
            raise AssertionFailedError.new(message.to_s)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* assert_match( regexp, string, [msg] )- Ensures that a string matches the regular expression&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_match(pattern, string, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        _wrap_assertion do&lt;br /&gt;
          pattern = case(pattern)&lt;br /&gt;
            when String&lt;br /&gt;
              Regexp.new(Regexp.escape(pattern))&lt;br /&gt;
            else&lt;br /&gt;
              pattern&lt;br /&gt;
          end&lt;br /&gt;
          full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be =~\n&amp;lt;?&amp;gt;.&amp;quot;, string, pattern)&lt;br /&gt;
          assert_block(full_message) { string =~ pattern }&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;) - If expected != actual, then the assert passes&lt;br /&gt;
&lt;br /&gt;
Example [3]:&lt;br /&gt;
      def assert_not_equal(expected, actual, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        full_message = build_message(message, &amp;quot;&amp;lt;?&amp;gt; expected to be != to\n&amp;lt;?&amp;gt;.&amp;quot;, expected, actual)&lt;br /&gt;
        assert_block(full_message) { expected != actual }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*assert_nil(object, message=&amp;quot;&amp;quot;) - This assert passes if the object is nil.&lt;br /&gt;
&lt;br /&gt;
Example[3]:&lt;br /&gt;
&lt;br /&gt;
   # File test/unit/assertions.rb, line 173&lt;br /&gt;
      def assert_nil(object, message=&amp;quot;&amp;quot;)&lt;br /&gt;
        assert_equal(nil, object, message)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are a bunch of other public class methods. This can be obtained from [4]&lt;br /&gt;
&lt;br /&gt;
=='''Test Method &amp;amp; Test Fixture'''==&lt;br /&gt;
&lt;br /&gt;
Assertions must be used inside test methods within test fixtures. Related tests are grouped inside a common test class using assert. The advantage of having a separate class for all related tests is that it keeps the actual developed code to be tested uncluttered from the test code, hence making maintainability easier. It also allows these test code to be deleted from the development code before the final delivery as these test codes are needed mainly for the developer/tester and need not be part of the final product. Main advantage is it allows you to set up a common test fixture for your tests to run against. Test fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.&lt;br /&gt;
&lt;br /&gt;
=='''Ruby on Rails'''==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] is an open source web framework for Ruby language. Rails adds some custom assertions of its own to the test/unit framework some of which are as stated below:&lt;br /&gt;
&lt;br /&gt;
*assert_difference(expressions, difference = 1, message = nil) {...}&lt;br /&gt;
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.&lt;br /&gt;
*assert_recognizes(expected_options, path, extras={}, message=nil)&lt;br /&gt;
Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. &lt;br /&gt;
Basically, it asserts that Rails recognizes the route given by expected_options.&lt;br /&gt;
*assert_template(expected = nil, message=nil)&lt;br /&gt;
Asserts that the request was rendered with the appropriate template file.&lt;br /&gt;
&lt;br /&gt;
= '''Benefits of assertions''' =&lt;br /&gt;
*Use of assertions in the program help detect errors immediately and directly, rather than at a later stage. Assertion failure usually reports the location of failure in the code which helps in pin-pointing the error without further debugging.&lt;br /&gt;
*Assertions provide run time check for assumptions made by developers&lt;br /&gt;
*Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C++, and Java. Any case which the programmer does not handle intentionally will raise an error and the program will abort rather than silently continuing in an erroneous state.&lt;br /&gt;
*Assertions can be viewed as &amp;quot;dynamic documentation&amp;quot;, since they are checked at runtime, contrary to the traditional approach of documenting assumptions via plain /* comments */.&lt;br /&gt;
&lt;br /&gt;
*Assert statements are great for helping you torefactor and optimize your code with greater confidence that you have preserved correctness&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Limitations of assertions''' =&lt;br /&gt;
&lt;br /&gt;
*Assertions rarely allow for graceful error recovery. They terminate the program abruptly and may not release some of the resources used by the program; hence it is considered bad practice to rely upon assertions for handling expected error conditions.&lt;br /&gt;
* Assertions sometime hinder execution time. For example, if the program has an assert that checks to see if the number to be returned is the smallest in the array, then the assertion will have to do the same amount of work that the method would have to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= '''Conclusion''' =&lt;br /&gt;
&lt;br /&gt;
When writing program, it is a good practice to check for violations of basic assumptions in the code. These checks help in debugging code. The assertion facility in J2SE 1.4 (and later versions) provides a unified support for assertions in Java technology as well as a convenient way for developers both to turn assertions on and off as needed. Assertions are used in Test Driven Development(TDD) in Ruby programing language. The Test::Unit library in Ruby has a variety of built in assertions that makes writing tests much easier. &lt;br /&gt;
&lt;br /&gt;
Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy to continue program execution. This means that when an exception is detected, the program aborts with no recovery mechanism. Nevertheless, assertions play an important role in debugging and designing code with testability in mind. The assertion facility can be used to support an informal design-by-contract style of programming.&lt;br /&gt;
&lt;br /&gt;
= '''Reference''' =&lt;br /&gt;
&lt;br /&gt;
[http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html]  Programming with Assertions 	&lt;br /&gt;
&lt;br /&gt;
[http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html]  Assertions in java&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M004514] Assertions in Ruby&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/testing.html#assertions-available] Ruby assertions&lt;br /&gt;
&lt;br /&gt;
[http://topfunky.com/clients/rails/ruby_and_rails_assertions.pdf] Ruby on Rails assertion cheat sheet&lt;br /&gt;
&lt;br /&gt;
[http://www.cs.clemson.edu/~malloy/papers/prospectus/prospectus.pdf] More about assertions&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38051</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38051"/>
		<updated>2010-10-15T04:55:24Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Object-relational Mapping Comparison for Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Object-relational Mapping for Ruby=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :posts          # one to many association&lt;br /&gt;
  has n, :cheers          # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Strategic Eager Loading and by using :summary&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;br /&gt;
# [http://ormbattle.net/ ORMBattle.Net]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38049</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38049"/>
		<updated>2010-10-15T04:44:45Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* '''Object-relational Mapping for Ruby''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Object-relational Mapping Comparison for Ruby=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :posts          # one to many association&lt;br /&gt;
  has n, :cheers          # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Strategic Eager Loading and by using :summary&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;br /&gt;
# [http://ormbattle.net/ ORMBattle.Net]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38048</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38048"/>
		<updated>2010-10-15T04:34:09Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :posts          # one to many association&lt;br /&gt;
  has n, :cheers          # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Strategic Eager Loading and by using :summary&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;br /&gt;
# [http://ormbattle.net/ ORMBattle.Net]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38047</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38047"/>
		<updated>2010-10-15T04:29:06Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* DataMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :posts          # one to many association&lt;br /&gt;
  has n, :cheers          # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;br /&gt;
# [http://ormbattle.net/ ORMBattle.Net]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38042</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38042"/>
		<updated>2010-10-15T04:06:03Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;br /&gt;
# [http://ormbattle.net/ ORMBattle.Net]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38041</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38041"/>
		<updated>2010-10-15T04:05:37Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=Future Work=&lt;br /&gt;
More work can be done in the area of comparison between the different ORMs available for Ruby, especially a performance comparison between the ORM's. We came across one cool website which has the ORM comparison for .Net [http://ormbattle.net/ ORMBattle.Net]. Something similar would be really helpful. Due to the time contraint and lack of indepth knowledge we were not able to work on these areas.&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38033</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38033"/>
		<updated>2010-10-15T03:51:49Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* DataMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38028</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38028"/>
		<updated>2010-10-15T03:38:11Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* DataMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true, :length =&amp;gt; 3..20 &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38027</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38027"/>
		<updated>2010-10-15T03:36:48Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* DataMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String, :required =&amp;gt; true, :unique =&amp;gt; true     &lt;br /&gt;
  property :age,        String, :required =&amp;gt; true &lt;br /&gt;
  property :email,      String &lt;br /&gt;
  &lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the example above , &amp;quot;:required = true&amp;quot; is an example for Auto Validation. Unlike ActiveRecord and Sequel, DataMapper supports auto validations , i.e. these in turn call the validation helpers to enforce basic validations such as length, uniqueness, format, presence etc.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38026</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38026"/>
		<updated>2010-10-15T03:29:48Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Sequel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age&lt;br /&gt;
      String :email}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38025</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38025"/>
		<updated>2010-10-15T03:26:16Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Sequel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38024</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38024"/>
		<updated>2010-10-15T03:25:41Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* ActiveRecord */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design. The following figure illustrates this concept of simplicity over efficiency.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38023</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38023"/>
		<updated>2010-10-15T03:03:35Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22 Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38021</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38021"/>
		<updated>2010-10-15T02:56:51Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* DataMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://datamapper.org/  DataMapper]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38018</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38018"/>
		<updated>2010-10-15T02:56:11Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=ActiveRecord=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=Sequel=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=DataMapper=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
end&lt;br /&gt;
/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://datamapper.org/  DataMapper]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38017</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38017"/>
		<updated>2010-10-15T02:54:51Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg: User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute '''name'''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://datamapper.org/  DataMapper]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38014</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38014"/>
		<updated>2010-10-15T02:48:40Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* ''' ActiveRecord ''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table.&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video. [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/ [7]]&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://datamapper.org/  DataMapper]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38013</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38013"/>
		<updated>2010-10-15T02:45:37Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: Updating References&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern [3]]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video. [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/ [7]]&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
# [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
# [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
# [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
# [http://datamapper.org/  DataMapper]&lt;br /&gt;
# [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-Relational Mapping Fall 2007]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38011</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38011"/>
		<updated>2010-10-15T02:40:52Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: Reference to past wiki&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern [3]]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video. [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/ [7]]&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
To get more information on ORM and the different type of ORM's available for Ruby , please look into the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22, Object-relational mapping Fall 2007] wiki.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
&lt;br /&gt;
[2] [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
&lt;br /&gt;
[4] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[5] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
&lt;br /&gt;
[6] [http://datamapper.org/  DataMapper]&lt;br /&gt;
&lt;br /&gt;
[7] [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
&lt;br /&gt;
[8] [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;br /&gt;
&lt;br /&gt;
[9] [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2007/wiki2_2_22,&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38010</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38010"/>
		<updated>2010-10-15T02:36:52Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORM's and it gives a very high level overview of the various ORM's such as ActiveRecord , Sequel and DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern [3]]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video. [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/ [7]]&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
&lt;br /&gt;
[2] [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
&lt;br /&gt;
[4] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[5] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
&lt;br /&gt;
[6] [http://datamapper.org/  DataMapper]&lt;br /&gt;
&lt;br /&gt;
[7] [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
&lt;br /&gt;
[8] [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38009</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=38009"/>
		<updated>2010-10-15T02:21:31Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: Adding Content to 'Sequel'&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern [3]]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.string :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_presence_of :age&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :age}&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sequel supports associations and validations similar to ActiveRecord. The following example shows how validations and associations can be enforced int he User table that has been created above. It enforces one to many relationship between the user table and the cheers , posts tables. It also validates for the presence , uniqueness and the length of the attribute 'name'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; Sequel::Model&lt;br /&gt;
  one_to_many :cheers&lt;br /&gt;
  one_to_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence [:name, :age]&lt;br /&gt;
  validates_unique(:name)&lt;br /&gt;
  validates_length_range 3..20, :name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
&lt;br /&gt;
[http://datamapper.org/ DataMapper] is an open source ORM for Ruby originally developed by Sam Smoot and first released in 2007. DataMapper provides a very flexible mapping API which allows creation of adapters to a wide variety of datastores beyond traditional SQL-based relational databases – DataMapper adapters have been created to non-standard sources such as the Salesforce API and even Google Video. [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/ [7]]&lt;br /&gt;
&lt;br /&gt;
Some of the key features of DataMapper are:&lt;br /&gt;
* API supports a wide variety of databases, including non SQL types&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager loading of child associations&lt;br /&gt;
* Lazy loading &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unlike ActiveRecord and Sequel, DataMapper does not rely on a migration scheme to create and manage DB tables.  Instead, DataMapper allows table definition as part of the model class definition which keeps the model definition contained to a single file, thus minimizing the effort required to keep the database and model definitions in sync.  When required, DataMapper can also support a migration methodology similar to other ORMs.&lt;br /&gt;
 &lt;br /&gt;
Example of table definition in the model:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :id,         Serial    # key&lt;br /&gt;
  property :name,       String     &lt;br /&gt;
  property :password,   String &lt;br /&gt;
  property :age,        String &lt;br /&gt;
  property :created_at, DateTime  &lt;br /&gt;
&lt;br /&gt;
  has n, :comments    # one to many association&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| SQLite, MySQL, PostgreSQL, Oracle, MongoDB, SimpleDB, many others, including CouchDB, Apache Solr, Google Data API&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes, but optional&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| Supported&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| Using methods (verify)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| Yes. Using the dm_ar_finders plugin&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational mapping (ORM)]&lt;br /&gt;
&lt;br /&gt;
[2] [http://ar.rubyonrails.org/  ActiveRecord]&lt;br /&gt;
&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Active_record_pattern Wikipedia, Active record pattern]&lt;br /&gt;
&lt;br /&gt;
[4] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[5] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html  Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
&lt;br /&gt;
[6] [http://datamapper.org/  DataMapper]&lt;br /&gt;
&lt;br /&gt;
[7] [http://merbist.com/2008/09/29/write-your-own-custom-datamapper-adapter/  Merbist blog on custom DM adapters - By Matt Aimonetti]&lt;br /&gt;
&lt;br /&gt;
[8] [http://en.wikipedia.org/wiki/Datamapper Wikipedia, DataMapper]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36689</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36689"/>
		<updated>2010-10-05T01:30:03Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* '''Sequel'''= */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.integer :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''=&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] [http://ar.rubyonrails.org/ Active Record]&lt;br /&gt;
&lt;br /&gt;
[3] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[4] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html Sequel Presentation - By Jeremy Evans]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36688</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36688"/>
		<updated>2010-10-05T01:29:52Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.integer :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''==&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] [http://ar.rubyonrails.org/ Active Record]&lt;br /&gt;
&lt;br /&gt;
[3] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[4] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html Sequel Presentation - By Jeremy Evans]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36687</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36687"/>
		<updated>2010-10-05T01:29:17Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=''' ActiveRecord '''=&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.integer :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
='''Sequel'''==&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=''' DataMapper'''=&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] [www.rubygarden.org/faq Fowler, C., The Ruby FAQ]&lt;br /&gt;
&lt;br /&gt;
[3] [http://ar.rubyonrails.org/ Active Record]&lt;br /&gt;
&lt;br /&gt;
[4] [http://sequel.rubyforge.org/ Sequel]&lt;br /&gt;
&lt;br /&gt;
[5] [http://jeremyevans-pres.heroku.com/mwrc2009_presentation.html Sequel Presentation - By Jeremy Evans]&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36685</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36685"/>
		<updated>2010-10-05T01:22:11Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
To create a table, ActiveRecord makes use of a migration class rather than including table definition in the actual class being modeled.  As an example, the following code creates a table ‘’users’’ to store a collection of class ‘’User’’ objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def self.up&lt;br /&gt;
    create_table :users do |t|&lt;br /&gt;
      t.string :name&lt;br /&gt;
      t.string :email&lt;br /&gt;
      t.integer :age&lt;br /&gt;
&lt;br /&gt;
      t.timestamps     # add creation and modification timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.down        # undo the table creation&lt;br /&gt;
    drop_table :users&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note in the preceeding example, that the ActiveRecord’s migration scheme provides a means for backing out changes via the ‘’self.down’’ method.  In addition, a table key ‘’id’’ is added to the new table without explicit definition and record creation and modification timestamps are included via the ‘’timestamps’’ method provided by ActiveRecord.  &lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the ‘’User’’ class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While ActiveRecord provides the flexibility to create more sophisticated table relationships to represent class hierarchy, its base scheme is a single table inheritance, which trades some storage efficiency for simplicity in the database design.  &lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with popular Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
* DB creation/management using the migrate scheme provides a means for backing out unwanted table changes&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Databases &amp;lt;/p&amp;gt;&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Migrations &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; EagerLoading &amp;lt;/p&amp;gt;&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Flexible Overriding &amp;lt;/p&amp;gt;&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! &amp;lt;p align=&amp;quot;left&amp;quot;&amp;gt; Dynamic Finders &amp;lt;/p&amp;gt;&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36683</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36683"/>
		<updated>2010-10-05T01:19:15Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:10%&amp;quot;|Features &lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|ActiveRecord&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|Sequel&lt;br /&gt;
!style=&amp;quot;width:30%&amp;quot;|DataMapper&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36665</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36665"/>
		<updated>2010-10-05T00:59:10Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! ''' Features '''&lt;br /&gt;
! ''' ActiveRecord '''&lt;br /&gt;
! ''' Sequel '''&lt;br /&gt;
! ''' DataMapper '''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| Yes. Uses 'Method Missing'&lt;br /&gt;
| No. Alternative is to use &amp;lt;Model&amp;gt;.FindOrCreate(:name=&amp;gt;&amp;quot;John&amp;quot;)&lt;br /&gt;
| N/A&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36664</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36664"/>
		<updated>2010-10-05T00:56:01Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! ''' Features '''&lt;br /&gt;
! ''' ActiveRecord '''&lt;br /&gt;
! ''' Sequel '''&lt;br /&gt;
! ''' DataMapper '''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| No. Overriding is done using alias methods.&lt;br /&gt;
| Using methods and by calling 'super'&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36663</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36663"/>
		<updated>2010-10-05T00:54:04Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! ''' Features '''&lt;br /&gt;
! ''' ActiveRecord '''&lt;br /&gt;
! ''' Sequel '''&lt;br /&gt;
! ''' DataMapper '''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| Supported by scanning the SQL fragments&lt;br /&gt;
| Supported using eager (preloading) and eager_graph (joins) &lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36662</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36662"/>
		<updated>2010-10-05T00:50:56Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! ''' Features '''&lt;br /&gt;
! ''' ActiveRecord '''&lt;br /&gt;
! ''' Sequel '''&lt;br /&gt;
! ''' DataMapper '''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| MySQL, PostgreSQL, SQLite, Oracle, SQLServer, and DB2&lt;br /&gt;
| ADO, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36657</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36657"/>
		<updated>2010-10-05T00:41:08Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* Comparison of ORM Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! ''' Features '''&lt;br /&gt;
! ''' ActiveRecord '''&lt;br /&gt;
! ''' Sequel '''&lt;br /&gt;
! ''' DataMapper '''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36656</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36656"/>
		<updated>2010-10-05T00:23:10Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM Features=&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''Features'''&lt;br /&gt;
! '''ActiveRecord'''&lt;br /&gt;
! '''Sequel'''&lt;br /&gt;
! '''DataMapper'''&lt;br /&gt;
|-&lt;br /&gt;
! '''Databases'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Migrations'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EagerLoading'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Flexible Overriding'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''Dynamic Finders'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36651</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36651"/>
		<updated>2010-10-05T00:12:42Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: Additional Sequel Changes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. It is based on the active record pattern. Sequel and Active record share a lot of common features , for example association and inheritance. But Sequel handles these features in a much more flexible manner. Currently Sequel is at version 3.16.0. Initially Sequel was had three core modules - sequel, sequel_core and sequel_model.Starting from version 1.4 , sequel and sequel_model were merged. Sequel handles validations using a validation plug-in and helpers.&lt;br /&gt;
&lt;br /&gt;
Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
==Chart example==&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''aaaaaa'''&lt;br /&gt;
! '''bbbbbbb'''&lt;br /&gt;
! '''cccc'''&lt;br /&gt;
! '''ddddddddd'''&lt;br /&gt;
|-&lt;br /&gt;
! '''AA'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''BBBBB'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''CCCC'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''DDDD'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EEEE'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36649</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36649"/>
		<updated>2010-10-04T23:57:26Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org Sequel] was originally developed by Sharon Rosner and the first release was in March 2007. Currently Sequel is at version 3.16.0. Some of the key features of sequel are,&lt;br /&gt;
* Connection Pooling&lt;br /&gt;
* Thread Safety&lt;br /&gt;
* Eager Loading / Lazy Loading&lt;br /&gt;
* Model Caching&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateUser &amp;lt; Sequel::Migration&lt;br /&gt;
  def up&lt;br /&gt;
    create_table(:user) {&lt;br /&gt;
      primary_key :id&lt;br /&gt;
      String :name&lt;br /&gt;
      String :password }&lt;br /&gt;
  end&lt;br /&gt;
  def down&lt;br /&gt;
    drop_table(:user)&lt;br /&gt;
  end&lt;br /&gt;
end # CreateUser.apply(DB, :up)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
==Chart example==&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''aaaaaa'''&lt;br /&gt;
! '''bbbbbbb'''&lt;br /&gt;
! '''cccc'''&lt;br /&gt;
! '''ddddddddd'''&lt;br /&gt;
|-&lt;br /&gt;
! '''AA'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''BBBBB'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''CCCC'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''DDDD'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EEEE'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36645</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36645"/>
		<updated>2010-10-04T23:35:45Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== &lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org]&lt;br /&gt;
&lt;br /&gt;
* table definition and mapping to object structure explicitly made in ruby vs external tool (rake) as in RoR&lt;br /&gt;
&lt;br /&gt;
prod description&lt;br /&gt;
http://sequel.rubyforge.org/rdoc/files/README_rdoc.html&lt;br /&gt;
&lt;br /&gt;
Sequel: The Database Toolkit for Ruby&lt;br /&gt;
Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby. &lt;br /&gt;
Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas. &lt;br /&gt;
Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records. &lt;br /&gt;
Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, savepoints, two-phase commit, transaction isolation, master/slave configurations, and database sharding. &lt;br /&gt;
Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, Mysql2, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3. &lt;br /&gt;
&lt;br /&gt;
'''Pros''' - &lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
==Chart example==&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''aaaaaa'''&lt;br /&gt;
! '''bbbbbbb'''&lt;br /&gt;
! '''cccc'''&lt;br /&gt;
! '''ddddddddd'''&lt;br /&gt;
|-&lt;br /&gt;
! '''AA'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''BBBBB'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''CCCC'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''DDDD'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EEEE'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36644</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36644"/>
		<updated>2010-10-04T23:34:55Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: /* '''Sequel''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''== [http://sequel.rubyforge.org]&lt;br /&gt;
&lt;br /&gt;
* table definition and mapping to object structure explicitly made in ruby vs external tool (rake) as in RoR&lt;br /&gt;
&lt;br /&gt;
prod description&lt;br /&gt;
http://sequel.rubyforge.org/rdoc/files/README_rdoc.html&lt;br /&gt;
&lt;br /&gt;
Sequel: The Database Toolkit for Ruby&lt;br /&gt;
Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby. &lt;br /&gt;
Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas. &lt;br /&gt;
Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records. &lt;br /&gt;
Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, savepoints, two-phase commit, transaction isolation, master/slave configurations, and database sharding. &lt;br /&gt;
Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, Mysql2, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3. &lt;br /&gt;
&lt;br /&gt;
'''Pros''' - &lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
==Chart example==&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''aaaaaa'''&lt;br /&gt;
! '''bbbbbbb'''&lt;br /&gt;
! '''cccc'''&lt;br /&gt;
! '''ddddddddd'''&lt;br /&gt;
|-&lt;br /&gt;
! '''AA'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''BBBBB'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''CCCC'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''DDDD'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EEEE'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36572</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3j KS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3j_KS&amp;diff=36572"/>
		<updated>2010-10-04T01:32:54Z</updated>

		<summary type="html">&lt;p&gt;Kschidam: Initial Draft from Scott's document&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Object-relational Mapping for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence.  For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. &lt;br /&gt;
&lt;br /&gt;
Typical ORM features:&lt;br /&gt;
* Automatic mapping from classes to database tables&lt;br /&gt;
** Class instance variables to database columns&lt;br /&gt;
** Class instances to table rows&lt;br /&gt;
* Aggregation and association relationships between mapped classes are managed&lt;br /&gt;
** Example, :has_many, :belongs_to associations in ActiveRecord&lt;br /&gt;
** Inheritance cases are mapped to tables&lt;br /&gt;
* Validation of data prior to table storage&lt;br /&gt;
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records&lt;br /&gt;
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)&lt;br /&gt;
&lt;br /&gt;
The diagram below depicts a simple mapping of an object to a database table….&lt;br /&gt;
&lt;br /&gt;
[[Image:3j_ks_pic1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.&lt;br /&gt;
&lt;br /&gt;
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…&lt;br /&gt;
&lt;br /&gt;
=Comparison of ORM products for Ruby=&lt;br /&gt;
&lt;br /&gt;
==''' ActiveRecord '''==&lt;br /&gt;
&lt;br /&gt;
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern]&lt;br /&gt;
&lt;br /&gt;
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design.  Add a pic for to describe…&lt;br /&gt;
&lt;br /&gt;
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition.  For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables.  Also note ActiveRecord’s integrated support for validation of table information when attempting an update.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  &lt;br /&gt;
  has_many :cheers&lt;br /&gt;
  has_many :posts&lt;br /&gt;
  &lt;br /&gt;
  validates_presence_of :name&lt;br /&gt;
  validates_uniqueness_of :name&lt;br /&gt;
  validates_length_of :name, :within =&amp;gt; 3..20&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Pros''' -&lt;br /&gt;
* Integrated with Rails development framework&lt;br /&gt;
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Sequel'''==&lt;br /&gt;
&lt;br /&gt;
[http://sequel.rubyforge.org]&lt;br /&gt;
* table definition and mapping to object structure explicitly made in ruby vs external tool (rake) as in RoR&lt;br /&gt;
&lt;br /&gt;
prod description&lt;br /&gt;
http://sequel.rubyforge.org/rdoc/files/README_rdoc.html&lt;br /&gt;
&lt;br /&gt;
Sequel: The Database Toolkit for Ruby&lt;br /&gt;
Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby. &lt;br /&gt;
Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas. &lt;br /&gt;
Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records. &lt;br /&gt;
Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, savepoints, two-phase commit, transaction isolation, master/slave configurations, and database sharding. &lt;br /&gt;
Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, Mysql2, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3. &lt;br /&gt;
&lt;br /&gt;
'''Pros''' - &lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
'''Cons''' -&lt;br /&gt;
* zzz&lt;br /&gt;
&lt;br /&gt;
==''' DataMapper'''==&lt;br /&gt;
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/&lt;br /&gt;
Ruby ORM: DataMapper&lt;br /&gt;
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. &lt;br /&gt;
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion.&lt;br /&gt;
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/.&lt;br /&gt;
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/]&lt;br /&gt;
DataMapper: A Better ORM for Ruby&lt;br /&gt;
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it.&lt;br /&gt;
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields.&lt;br /&gt;
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.&lt;br /&gt;
&lt;br /&gt;
==Chart example==&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot;  border=&amp;quot;1&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! '''aaaaaa'''&lt;br /&gt;
! '''bbbbbbb'''&lt;br /&gt;
! '''cccc'''&lt;br /&gt;
! '''ddddddddd'''&lt;br /&gt;
|-&lt;br /&gt;
! '''AA'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''BBBBB'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''CCCC'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''DDDD'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
! '''EEEE'''&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Code example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Ruby ORM bla.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://ruby-doc.org/ Ruby language]&lt;br /&gt;
&lt;br /&gt;
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq&lt;br /&gt;
&lt;br /&gt;
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby]&lt;br /&gt;
#[http://www.activestate.com/activetcl ActiveTcl for Windows]&lt;/div&gt;</summary>
		<author><name>Kschidam</name></author>
	</entry>
</feed>