Is there a design pattern for ActiveRecord structure? - ruby

I want to know the structure about ruby's ActiveRecord. But I didn't find a design or introduction for its architecture.
Other way, how can I know its structure or how to read its source code to fast understand it?

I'm a big fan of the talk Gregg Pollack gave at the Lone Star Ruby Conference about the big refactoring of Rails 3. He does a great presentation gives you a great peek under the skirt of Rails. Rails 4 hasn't really changed much. Active record follows the same architecture he describes and you will be able to see it when you browse through the active record code after seeing the presentation. Here is the link to the talk:
http://confreaks.tv/videos/lsrc2010-de
or
https://www.youtube.com/watch?v=ldxGXxZ1CCI

Related

Coldfusion coding standards documents

I used to have a nice set of coding standards I could print for coldfusion that was almost a framework but much looser with some code examples. Like teh use of createObject, more cfscript, error and message integration, Application.cfc usage and examples. Also seperation of buisness (actions) and display using cfincludes to make an index.cfm page 'a generator' of many actions leveraginf cfswitch/cfcase...etc. I am looking for some doucmentation or anything that I can print our and sit on my desk and refer to so my coding stays clean. Any help from the community would be very much appreciated.
Although rather dated, You may find what you are looking for among the many posts listed under the "Coding Guidelines" section on the "Articles" page of the MDCFUG - Mayryland ColdFusion User's Group website.
Here are some other options:
Univ. of Maryland ColdFusion Programming Standards
Sample Applications Methodology Guidelines provided by Adobe.com
SBA ColdFusion Programming Standards contains 126 pages of coding standard written for the U.S. Small Business Administration to insure security, promote code re-use, easy readability, and easy maintenance.
Oregon Health & Science University - Coding Guidelines which contains a number of GOOD versus BAD coding examples.
I'm unaware of any "official" Coldfusion specific coding standards. Where I work we collectively came up with our own based on many years of experience working with the language. A simple one for example is to use camel case. From what I've seen camelCase is preferred over snake case in Coldfusion, so as a rule we have:
In general, for files use lowerCamelCase starting with a lower-case letter, except for CFCs which should start with an upper-case letter as they represent a class in ColdFusion.
We have other standards broken up under headings such as:
Variable Naming
Operators
Scopes
Functions
ColdFusion Native
Functions
Indentation
......
I am looking for some doucmentation or anything that I can print our
and sit on my desk and refer to so my coding stays clean.
In addition to this I recommend you create a set of generic code snippets, such as you can use in Eclipse, that conform to any standard you decide to go with. These can then be easily distributed among team members, helping to keep things consistent as well as saving typing time.
I realise this is quite an old post now, but thought it would be worth mentioning http://wiki.coldbox.org/wiki/DevelopmentBestPractices.cfm. I think this is a good set of standards for writing CFML.

Ruby Object Model / Class / Modules .... diagram anywhere?

I have been thinking about making my own diagram to better put my head around how all the classes and modules in Ruby inherit from each other. How it all ties together visually. I am sure there is a good one out there. I Googled it but was not satisfied with the results. Any suggestions?
I am also a visual learner and ended up drawing my own "homemade" diagram:
Hope this helps.
One of the best explanations and diagrammatic explanation of the ruby object model can be found in this book: Metaprogramming Ruby I tried googling the relevant pages but came up empty
For a good understanding on how the Ruby Object Model and Metaprogramming work, I would highly recommend these ScreenCasts by Pragmatic Programmer's Dave Thomas:
http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming
Dave Thomas is awesome at explaining the concepts, and the screencasts have nice graphics in them visualizing how things relate to each other.. those ScreenCasts are straight to the point, without distractions - and are worth every penny, err cent!
There is also a book "Metaprogramming Ruby" http://pragprog.com/book/ppmetr/metaprogramming-ruby
but although it covers a lot of concepts, the book is badly written, and through it's writing style makes it really hard to memorize the concepts / or use the book as a reference to look up things later..
A diagram showing superclass and eigenclass links between Ruby objects (but not the module inclusion links) is available at www.atalon.cz.
Since 11 January 2012 there is a diagram containing module inclusion links.

What is your preferred ODM in Ruby? MongoMapper, MongoID or MongoDoc?

In Ruby, there are currently 3 ODM (object data mappers) maintained:
MongoMapper
Mongoid
MongoDoc
Which is your preferred and why?
I just chose Mongoid for a new Rails 3 project based on the argument that it has the best documentation.
Since I am new to MongoDB (with many years of MySQL and a little bit of CouchDB experience), I needed good guides that take me by the hand and help me deliver something working within a reasonable span of time.
AFAIK Mongoid has just that: a nice looking site, with well documented guides for newbies like me.
Code Stats for Mongoid and MongoMapper
It looks like MongoMapper has much better code quality (if it does the same with less).
Here's the analyzer CodeStats https://github.com/alexeypetrushin/code_stats
In my opinion it's hard to say which is better, if you have DataMapper experience you'll like MongoMapper, but if you used ActiveRecord, Mongoid it's your preferred choice. I believe all of them worths a try concerning the context where you want to use them.
I am using MongoMapper. It good except it is little slow with Time conversions.
And it loads all data as Array. MyCollection.all for example gives you huge array, not cursor.
while Mongoid says: - Optimized for use with extremely large datasets.
So I guess you could try MongoID if you need speed and have big recordsets.
I was trying MongoMapper, but I think I'll go with Mongoid, because after quick reading docs it seems to me somewhat easier. Plus, it's developed by guys from Hashrocket, so that's a good reason itself.
While not a direct answer to your question, I would also consider using the basic ruby driver directly. Unlike the various SQL adapters out there Mongo's ruby class is easy to use and powerful. Because queries are hashes, composing queries is generally easy. The real advantage is access to the Atomic Modifiers. If you benefit from a Document database, these modifiers should be in your toolbox.
Having said this, I will go ahead and recommend MongoMapper because it has cleaner integration with the non-CRUD parts of MongoDB. Both projects are making gains in this area, and the situation may have changes since I did my research in December 2010.
I can recommend MongoMapper, since it also works with rails3 (beta and master). I personally didn't try the other 2 mappers you mentioned, since MM works great in my workflow and the mailinglist is very active. Furthermore the codebase is really stable and the only issue is with rails3 master, so you should use fredwu's branch, which already includes fixes for the current rails3 master changes on form_for: http://github.com/fredwu/mongomapper.git
There's also MongodbModel http://alexeypetrushin.github.com/mongodb_model

Quickest way to learn Linq to Entities

I am developing my first ASP.NET MVC application using the Entity Framework. I have no previous .NET experience, although I'm fluent in other OO languages. I am having trouble understanding Linq to Entity query expressions, more specifically its syntax and how to render the results on an ASP page.
For example, I am joining three tables (entities) together and trying to iterate over the results, but I get all sorts of type errors. Every example I find on Google, SO, or MSFT hasn't been straightforward and each site takes their own approach. I looked at the book Programming Entity Framework, but this too seems to be over my head at the moment.
I am looking for clear, concise examples of executing the equivalent of complex multiple joins and iterating over the results.
Thanks
I've just started this myself, and I bought both the wrox book and the o'reilly one.
I've found the wrox one a little more inaccessible - the language used in it is a bit heavier, and the layout/organisation isn't quite so 'flowy' (is that a word? hehe).
The o'reilly one on the other hand is a cracking read, and really does lead you nicely through it. It's available on safaribooks too if you have a sub, I got it from Amazon who seemed one of the few places to have it in stock. Very much recommended.
take a look at these samples - http://blogs.msdn.com/adonet/archive/2007/03/13/101-linq-samples-updated.aspx
i picked up the concept from these examples itself
Scott Guthrie's blog has also really good explanation & samples
Sounds to me like you should get yourself a good book on the subject. Since you have no previous experience in .NET a general book that covers LINQ might be a start and then a ASP.NET-MVC specific one.
Perhaps the reason why being fluent in "other" OO languages isn't helping is that LINQ isn't an OO language, its much more functional and therefore takes a different way of reasoning to understand well.

Designing a poker parser in Ruby

I'm writing a small program in Ruby to parse a hand history log from a poker site.
The log is split over several lines and looks a bit like this:
Table 123456 NL Hold'em $1/$2
5 Players
Seat 3 is the button
Seat 1: randomGuy112 $152.56
Seat 2: randomGirl99 $200
Seat 3: PokerPro $357.12
Seat 4: FishCake556 $57.19
Seat 6: MooMoo $188.98
Dealt to MooMoo [Ah, Ks]
randomGuy112 folds
randomGirl99 raises to $7
etc.. etc..
I want to summarise this information in an object which then might, for example,
render it differently or save it to database.
When I originally thought of this problem I thought I'd just have one realativly straight forward class with a number of regexes and several if/else statements. I then realised this could turn into quite a large method and potentially be a nightmare to debug/maintain. Keep in mind it needs to loop at each stage of the game (preflop,flop etc) to collect player's actions.
I also want to tackle this with a TDD approach, but the 'one long method' way means that the tests for with checking later input will kind of rely on earlier tests.
I'm quite new to Ruby and havn't yet clicked on the 'Ruby way' to do things. I'm catching myself writing C# code
in a different language.
Can you give me some pointers on how to design the parser so it isn't one huge mess of if/else statements and more testable?
Use Treetop
It does look like you are on the borderline between what ad hoc string matching and RE's are good for, and what requires an actual parser.
There is nothing wrong with handwritten parsers, and as long as you keep your methods short, without a lot of complexity in any given one, it's OK to have as many if statements in total as the parser requires.
I'm not sure 10 lines with incomprehensible regular expressions is any better than 30 lines of nice looking code.
Now, Ruby does have an advanced PEG parser generator. I think in this case I wouldn't worry about whether it was overkill, I would just go ahead and use Treetop.
State Machine, anyone?
At any point in the play of a poker hand there is a clearly-defined set of possible next actions. I'd think you could encapsulate them into a state machine. There are a few around, amongst which (no recommendations, I'm afraid - not enough experience with any) are
Alter Ego (updated July this year)
ruby-state-machine (seems also to be alive)
statemachine (looks a bit stale)
You can checkout this open source poker game hand parser
It looks like they created a hash of regular expressions and then they probably iterate over the regex data structures. It is a more simple machine than a parser and probably a more light weight approach.
I wrote hand history parser for PokerStars log files https://github.com/malikbakt/pokerstars
You may want to look at: StringScanner.
I have two different pointers for you, which will point you to the solution, on how to write code in the ruby way.
Get a ruby book. The ruby book will have a lot of examples on how to write code in the ruby way. From my personal expirience I can recommend you the pixake(is this spelled right?) book: http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html
Read existing ruby code. You seem to know enough ruby to write code? Then you should certainly be able to read existing code. I assume you already have installed ruby on your system. If so, you will find plenty of sourcecode on your harddrive. If not just use the internet.
I'd recommend the book Refactoring by Martin Fowler (available in both dead-tree and electronic formats, IIRC). He covers object-oriented remedies for exactly the design problems you're asking about, all in a test-driven context. This is one of those books that everyone in the profession should read.

Resources