|
| 1 | +--- |
| 2 | +layout: articles |
| 3 | +categories: articles |
| 4 | +title: The Great Refactoring |
| 5 | +created_at: 2008-03-22T15:55:50-05:00 |
| 6 | +summary: Change is afoot |
| 7 | +author: afrench and ssmoot |
| 8 | +--- |
| 9 | + |
| 10 | +{{ page.title }} |
| 11 | +================ |
| 12 | + |
| 13 | +"Tip" DataMapper (hosted on [github](http://github.com/datamapper/dm-core)) is |
| 14 | +going through a dramatic re-factor. Here's a quick summary of the anticipated |
| 15 | +NEW public API. |
| 16 | + |
| 17 | +Not Just for Databases Anymore |
| 18 | +------------------------------ |
| 19 | + |
| 20 | +DataMapper's class terminology will change and 'de-couple' itself from |
| 21 | +database-specific terminology. Gone are "database", "table", "column" and |
| 22 | +"join". Say hello to "Repository", "Resource", "Property", and "Link". |
| 23 | + |
| 24 | +"Why would you want to do that?", you ask. Ultimately it's because DataMapper |
| 25 | +will soon support different types of persistence layers, not just databases. |
| 26 | +It'll talk to all sorts of things like web services (REST and such), XML files, |
| 27 | +YAML files, non-relational databases, even custom file-types or services of your |
| 28 | +own design. Just implement an Adapter that conforms to a certain API and |
| 29 | +DataMapper could support any type of data store. No need for a completely |
| 30 | +separate library or anything. |
| 31 | + |
| 32 | +As an added benefit, DataMapper become more "RESTful". [Ryan Tomayko](http://tomayko.com/writings/rest-to-my-wife) |
| 33 | +has a very good explanation |
| 34 | +of REST that all should read entitled [How I Explained REST to My Wife](http://tomayko.com/writings/rest-to-my-wife). |
| 35 | + |
| 36 | +Model Definitions Are a Little Different |
| 37 | +---------------------------------------- |
| 38 | + |
| 39 | +Since we're changing up the terminology, model definitions are going to change |
| 40 | +up a little bit. Here's what a Planet model would look like using the new API: |
| 41 | + |
| 42 | +{% highlight ruby linenos %} |
| 43 | +class Planet |
| 44 | + include DataMapper::Resource |
| 45 | + |
| 46 | + resource_names[:legacy] = 'dying_planets' |
| 47 | + |
| 48 | + property :name, String |
| 49 | + property :age, Integer |
| 50 | + property :core, String, :private => true |
| 51 | +end |
| 52 | +{% endhighlight %} |
| 53 | + |
| 54 | +A couple of things are going on here. First, DataMapper::Base and |
| 55 | +DataMapper::Persistence are gone and replaced with <%= |
| 56 | +doc('DataMapper::Resource') %>. Next `set_table_name` has been replaced with |
| 57 | +`resource_names` hash where you specify which arena play occurs in. After that |
| 58 | +we have a couple of Property definitions that look a little different. |
| 59 | + |
| 60 | +First off, Properties will no longer take `:symbols` for their types and instead |
| 61 | +take real constants like String, Integer, DateTime. Also on the docket are the |
| 62 | +ability to define your own custom types. |
| 63 | + |
| 64 | +Think about that for a minute. If developers are able to define their own custom |
| 65 | +types with their own materialization and serialization methods, DataMapper will |
| 66 | +be able to support all kinds of wild data-types like GIS information, network |
| 67 | +information, marshaled objects, JSON...pretty much anything a developer might |
| 68 | +need, or want. |
| 69 | + |
| 70 | +A Command-Line Interface |
| 71 | +------------------------ |
| 72 | + |
| 73 | +Taking a lesson from web frameworks, DataMapper will sport an interactive |
| 74 | +command-line so that you can browse your resources without the need to load up |
| 75 | +the entire environment of your application. Here's an example session: |
| 76 | + |
| 77 | +{% highlight bash linenos %} |
| 78 | +$ dm mysql://root@localhost/great_musicians # connecting to a repository |
| 79 | +{% endhighlight %} |
| 80 | + |
| 81 | +An IRB session boots up... |
| 82 | + |
| 83 | +{% highlight ruby %} |
| 84 | +>> the_king = Person.first(:name => 'elvis') |
| 85 | +>> the_king.alive? # => maybe |
| 86 | +{% endhighlight %} |
| 87 | + |
| 88 | +This is very similar to `script/console` in Rails or `merb -i` in Merb, only it |
| 89 | +won't load up the entire environment of your application, just your DataMapper |
| 90 | +resources and their associations, methods, and such. If you prefer "fat models", |
| 91 | +this will constitute the core of your application. |
| 92 | + |
| 93 | +How This All Comes Together |
| 94 | +--------------------------- |
| 95 | + |
| 96 | +This is the coolest new feature of DataMapper: we're skipping all the way from |
| 97 | +0.3.0 to 0.9! Get excited, contact the press, fire up the blogosphere! Its a |
| 98 | +huge jump and we're honestly concerned that people may not be able to handle it. |
| 99 | + |
| 100 | +Alright, so it's not _that_ big of a deal, but we're confident that all of this |
| 101 | +will get DataMapper so close to going 1.0 that we'll be able to taste it. To get |
| 102 | +there, DataMapper's more advanced features like single table inheritance, |
| 103 | +paranoia, and chained associations will be re-implimented to use all this new |
| 104 | +stuff, and then we're sure 0.9 will need a touch up or two. |
| 105 | + |
| 106 | +So close....so very very close... |
| 107 | + |
| 108 | +Stay tuned in to the [mailing list](http://groups.google.com/group/datamapper), |
| 109 | +check up on the [wiki](http://datamapper.org/), chat it up in |
| 110 | +[#datamapper](irc://irc.freenode.net/#datamapper) and watch |
| 111 | +[github commit messages](http://github.com/datamapper/dm-core/commits/master) for updates. |
0 commit comments