Comparing Rails and Scooter code side by side
Scooter framework adopts many excellent APIs implemented in other softwares. You've probably found some methods of Scooter framework actually already exist in other non-Java softwares such as Rails, PHP, and Oracle.
There is no doubt that Ruby-on-Rails (ROR) has the most significant impact on the development of Scooter, especially ROR's ActiveRecord and its convention-over-configuration principle.
The following is a brief comparison of Rails and Scooter. A more detailed comparison will be posted soon.
| Rails code in Ruby | Scooter code in Java |
|---|---|
| Model (basic) | |
class Order < ActiveRecord::Base end |
public class Order extends ActiveRecord {
}
|
| Model | |
class Person < ActiveRecord::Base
validates_presence_of :name, :address
validates_length_of :bio, :maximum => 500
has_many :posts, :dependent => :destroy
end
class Post < ActiveRecord::Base
belongs_to :person, :counter_cache => true
end
#Creating an object
p = Person.new(:name => "John Doe");
#Find the first
p = Person.first;
#Find the last
p = Person.last;
#Retrieving a single object using a primary key
person = Person.find(10)
#Where condition with a string
person = Person.where("name = 'John Doe'")
#Where condition with an array
person = Person.where("name = ? and address= ?",
params[:name], params[:address])
#Ordering
person = Person.order("created_at DESC")
#Limit and Offset
person = Person.limit(5).offset(30)
#Retrieving a person's posts
@person.posts
#Deleting a person and all his/her posts
@person.destroy
#Retrieving a post's author
@post.person
|
public class Person extends ActiveRecord {
public void validatesRecord() {
validators().validatesPresenceOf("name, address");
validators().validatesLengthMaximum("bio", 500);
}
public void registerRelations() {
hasMany("posts", "cascade:delete");
}
}
public class Post extends ActiveRecord {
public void registerRelations() {
belongsTo("person", "counter_cache:true");
}
}
//Creating an object
p = Person.newRecord();
p.setData("name", "John Doe");
//Find the first
p = Person.findFirst();
//Find the last
p = Person.findLast();
//Retrieving a single object using a primary key
person = Person.findById(10);
//Where condition with a string
person = Person.where("name = 'John Doe'");
//Where condition with an array
person = Person.where("name = ? and address= ?",
{params("name"), params("address")});
//Ordering
person = Person.orderBy("created_at DESC");
//Limit and Offset
person = Person.limit(5).offset(30);
//Retrieving a person's posts
person.allAssociated("posts");
//Deleting a person and all his/her posts
person.delete();
//Retrieving a post's author
post.associated("person");
|
| View | |
#displaying name in html-escaped text
Name: <%=h @person.name %>
#displaying edit person link
<%= link_to 'Edit', edit_person_path(@person) %>
#displaying alternate colors in a loop
<tr class="<%=cycle("odd, even")%>">
|
//displaying name in html-escaped text
Name: <%=O.hp(person, "name") %> or <%=O.hv("person.name") %>
//displaying edit person link
<%=W.labelLink("Edit", R.editResourceRecordPath("people", person))%>
//displaying alternate colors in a loop
<tr class="<%=W.cycle("odd, even")%>">
|
| Controller | |
class PeopleController < ApplicationController
# GET /people/1/edit
def edit
flash[:notice] = 'enter edit.'
@person = Person.find(params[:id])
end
|
public class PeopleController extends ApplicationController {
//GET /people/1/edit
public String edit() {
flash("notice", "enter edit.");
setViewData("person", Post.findById(p("id")));
return null;
}
}
|