contagion的ruby/rails投影片

Post on 27-Jan-2015

107 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

有點舊了:P

TRANSCRIPT

OSDC.TW 2006

RUBY on

contagious

How Hot is RoR

0

112,500

225,000

337,500

450,000

2004 July 2004 Dec. 2005 Oct. 2006 April

over 400,000 downloads

• 400 seats, sold out in one week

• extra 150 seats, sold out in 24 hours

David Heinemeier

Hansson

RoR is so HOT Everybody wants to

clone it

What exactly is Ruby On Rails

Ruby on Rails is an open-source web

framework

optimized for programmer

happiness

What Makes Programmer Happy?

•Simplicity

• Easy to learn

•Easy to Write

•Well organized

•Easy to Maintain

SIMPLEEasy to Learn and Write

No separation between Business Logic and Display Logic

Corporate ApprovalWell Structured

Steep Learn CurveComplex Configuration

No fast turnaround

Finding the middle ground with

Full Stack of MVC

Real World Usage

One Language - Ruby

•Model - View - Controller

•Database Schema generation

• Javascript generation

•XML generation

•Makefile

Convention Over

Configuration

CREATE TABLE orders ( id INTEGER PRIMARY KEY AUTO_INCREMENT, order_date TIMESTAMP NOT NULL, price_total DOUBLE NOT NULL);

CREATE TABLE products ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(80) NOT NULL, price DOUBLE NOT NULL);

CREATE TABLE order_items ( id INTEGER PRIMARY KEY AUTO_INCREMENT, order_id INTEGER NOT NULL, product_id INTEGER NOT NULL, amount INTEGER NOT NULL, price DOUBLE NOT NULL);

An example from a

Hibernate tutorial

public class Order{ private Integer id; private Date date; private double priceTotal; private Set orderItems = new HashSet();

public Order() { this.date = new Date(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getDate() { return date; } public void setDate(date) { this.date = date; } public void addProduct(Product p, int amount) { OrderItem orderItem = new OrderItem(this, p, amount); this.priceTotal = this.priceTotal + p.getPrice() * amount; this.orderItems.add(orderItem); }}

Java Object Model:Order

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="test.hibernate.Order" table="orders"> <id name="id" type="string" unsaved-value="null" > <column name="id" sql-type="integer" not-null="true"/> <generator class="native"/> </id> <property name="date"> <column name="order_date" sql-type="datetime" not-null="true"/> </property> <property name="priceTotal"> <column name="price_total" sql-type="double" not-null="true"/> </property>

<set name="orderItems" table="order_items" inverse="true" cascade="all"> <key column="order_id" /> <one-to-many class="test.hibernate.OrderItem" /> </set> </class></hibernate-mapping>

Java Database Mapping:Order

class Order < ActiveRecord::Base has_many :order_items

def add_product(product, amount) order_item = OrderItem.new(product, amount) self.price_total += product.price * amount order_items << order_item endend

Rails Data Model:Order

"opinionated software" — sacrificing some

flexibility in favour of simplicity

“much greater productivity and a better developer experience”

Action grouped in Controllerclass OrdersController < ApplicationController def index list # Default to app/views/orders/index.rhtml render :action => 'list' end

def list @order_pages, @orders = paginate :orders, :per_page => 10 end

def show @order = Order.find(params[:id]) endend

def update @order = Order.find(params[:id]) if @order.update_attributes(params[:order]) flash[:notice] = 'Order was successfully updated.' redirect_to :action => 'show', :id => @order else render :action => 'edit' endend

Rendering and Redirection

Query Data

the_order = Order.find(2) book = Product.find_by_name(”Programming Ruby”)

small_order = Order.find :first, :condition => [”price_total <= ?”, 50.0]web_books = Product.find :all, :condition => [”name like ?”,”%Web%”]

print book.nameprint the_order.price_total

class Order < ActiveRecord::Base has_many :order_itemsend

the_order = Order.find :firstthe_oder.oder_items.each do |item| print item.nameend

Associations

* Some other associations: has_one, belongs_to, has_and_belongs_to_many...

Validations

Hooksclass Order < ActiveRecord::Base def before_destory order_items.destory_all endend

class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :priceend

Views - Erb<!-- app/views/product/list.rhtml --><% for product in @products -%> <p> <ul> <li> name: <%= product.name %> </li> <li> price: <%= product.price %> </li> </ul> <%= link_to 'Show', :action => 'show', :id => product %> </p><% end -%>

Sounds Great !!

But it is written in ....

?? Ruby ??

Rails makes it fast to develop, but the true power behind Rails is

RUBY

Blocks• Iteration

• Resource Management

• Callback

File.open(’t.txt’) do |f| #do somethingend

TkButton.new do text “EXIT” command { exit}end

5.times { |i| puts i }[1,2,3].each { |i| puts i }

Rakefile task :test => [:compile, :dataLoad] do # run the tests end

Markaby html do body do h1 ‘Hello World’ end end

New Language Construct

Dynamic Nature

•Open Class

•Runtime Definition

•Code Evaluation

•Hooks

Meta - Programming

class Order < ActiveRecord::Base has_many :order_itemsend

class Product < ActiveRecord::Base validates_presenc_of :name validtaes_numericality :priceend

Syntax Matters

•Succinct

•Easy to Read

•Principle of Least Surprise

Ruby is designed to make programmers

Happy

Where Rails Sucks

• Internationalization

•Speed

•Template System

•Stability

•Legacy System

LackResources

In Taiwan

Q&A

top related