3. Ruby successfully
combines Smalltalk's
conceptual elegance,
Phython's ease of use
and learning, and Perl's
pragmatism
Sunday, March 22, 2009
4. Ruby is Highly Dynamic
• Uses dynamic typing (vs. static)
• Full support for introspection
• Support for method redefinition
• Create your own domain-specific languages
• Modules vs. multiple inheritance
• method_missing, etc...
Sunday, March 22, 2009
6. 5.times do
puts quot;Ruby Rocks!quot;
end
Sunday, March 22, 2009
7. class Greeter
def initialize(greeting)
@greeting = greeting
end
def salute(name = quot;youquot;)
puts quot;#{@greeting} #{name}!quot;
end
end
Sunday, March 22, 2009
9. def thrice
yield(1)
yield(2)
yield(3)
end
# Output quot;Blocks are cool!quot; three
times with the count
thrice do |i|
puts quot;#{i}: Blocks are cool!quot;
end
Sunday, March 22, 2009
33. class Video < ActiveRecord::Base
acts_as_featured
end
User.featurable? # => false
Video.featurable? # => true
Video.first.feature!
Sunday, March 22, 2009
39. Thx!
Give me Q, I’ll give you A.
Sunday, March 22, 2009
Notes de l'éditeur
Sinds 3j RoR dev
daarvoor: PHP, Java, ASP.NET C#, COBOL, HTTP, CSS
leren developen op het web
Wat is RoR.. eerst en vooral het Ruby
Created by Yukihiro Matsumoto, a.k.a. Matz in 1995
Smalltalk: Full OO, Dynamic typing, reflecting/meta programming
Phython: easy, natural syntax
Perl’s Pragamatism: Gezond verstand en realisme aanleggen en praktish gericht zijn ipv theoretish
** Principal of Least Surprise
** Completely OO
** Syntax for programmers
Dynamic typing => duck typing
introspection .methods method
redefine method in same class / redefine methods for an instance
Multiple inheritance like in c
A dynamic, object-oriented programming language with:
•a strong focus on simplicity and productivity
•an elegant, natural syntax
•roots in Lisp, Perl, and Smalltalk
Leesbaar, te verwachten syntax
5 is obviously a object
leesbaar, te verwachten syntax
No need to define the variables of an object
optional arguments (*args)
Only in Rails, ook in february
Reflection! (1.month is afhankelijk van Time.now)
code blocks!
True
nil is an Object
Class is an Object (word wat ingewikkeld)
Nu, wat is Rails dan?
Is een volledig web framework
- Abstraction (DB, environment)
- Geen SERVER
- Convention over Configuration
- MVC
Rails gebruikt het MVC pattern.
Voor die gene die MVC niet kennen:
Model: Object represenatie van de data en business logic
View: Presentatie van de data
Conroller: Het gene er tussenin (haal data op om te displayen, regageert op user actions)
De folder structuur van een Rails app ziet er zo uit.
- Controllers
- Models
- Views
helpers voor views the helpen
ten eerste de Models
ActiveRecord is de basis klasse van alle DB models in Rails
- Klass is a table
- Columns are attributes
- Rows are instances
Abstractie van alle database, en db type:
MySQL, PostgreSQL, SQLite, Oracle, MS SQLServer, and IBM DB2. out of the box
Dit is hoe een model er kan uit zien
Het aanmaken van een tabel (een migration) is Database agnostisch.
ActiveRecord zal automatisch de tabel “posts” vinden (plural van Post) en al de attributen zullen aanwezig zijn als getters/setters
Free magic:
id column
created/updated_at
counters
Enkele van de “gratis” methods
Gratis instance methods
We kunnen natuurlijk de definitie van het model aanpassen
Weinig code => veel functionaliteit
- associations (belongs_to, has_one, has_many, :through, conditions ... )
- Validations (uniqness, range, presence, :if, custom)
- named scope (pre defined pieces of queries, condition, order, grouping)
Deze method calls gebeuren dus vooral in de controllers
Controllers
- reageert op de HTTP requests “actions”
OUt of the box krijgen we
Out of the box doet deze Klasse al veel voor je
- Securing een session (encoding the session info)
- Cross site scripting attacks afweren (session VS post variables)
- Vertalen van URL’s naar methods in controllers // Routing
- Als er een AR model niet gevonden word, 404 displayen, of een 500 page waneer er zich een andere excpetion voor doet
Routing
- Controller
- Action
- Params
Gedefinieerd in routes file,
Eigen routes of standaard routes / REST
REST = Representational_State_Transfer
CRUD over HTTP
PUT en DELETE zijn niet supported door Browsers (behalve in JS) en word gemuleerd door de Rails
HTTP is meer dan GET and POST (WebDAV, CalDAV) verbs
Zolas je kan zien kan rails bepalen in welk format het moet antwoorden
HTML, XML, JSON, ICAL, JS gebaseerd op de request
De view hoeft niet expliciet aangeroepen worden (default) view heeft zelfde naam als action
Helpers zijn er om de view te helpen.
Ze bevatten stukjes logica die meer dan een regel beslaan,
zodat de view zelf niet vervuilt word met teveel logica
Bepaalde stukken logica gaan zich vaak herhalen zowel binnen 1 als in meerdere applicaties
- Login validatie,
- Rating van Items in de database (stars)
- Comments logic
- etc...
Weinig woorden en veel daden
Dankzij convention over configuration:
- Bijna lege models (mapping en configuratie automatisch)
Configuration still possible
- legacy DB, highly custom systems
Logica dat vaak herhaalt word kan in een module geplaatst worden
en included worden door objecten die het willen
acts_as_featurable eigen project
Laat toe dat Models featured kunnen worden (dmv een feature model)
Alle logica is in de module
- association
- AR methods
- instance methods
- Klass methods
- validations ...
1 lijntje brengt onmiddelijk een hele reeks code met zich mee
ActiveRecord zelf is ook aangepast, zodat elk model kan zeggen of het gefeatured kan worden
Gems en plugins zijn al vooraf geschreven stukken code, liberaries
Waarom herhalen wat anderen al hebben geschreven?
commentable: comment system voor de site
taggable: full tagging system (zijn volledige tables, maar doen zich voor als array in model)
list: als order belangrijk is
paginate: limit queries, and pagination voor HTML (AR, AC)
default: complexe default values (uuid, time based, )
Ten slotte nog enkele plugins in detail
Ten slotte
Authlogic voorziet een volledige authenticatie systeem
Definieer het User model and you are ready to roll
- Sessies gedragen zich net als andere modellen
- Password security is transparant
- tracken van login times
- create / confirmed / approved status van account
- etc...
Easy file attachment management for ActiveRecord
- Zet zelf attributen in DB
- Manage files zelf, methods zoals public_path en local_path
- Thumbnails automatisch
- Processors toevoegen (OCR, red eyes...)
-