Weitere ähnliche Inhalte Mehr von Yasuko Ohba (20) Kßrzlich hochgeladen (20) The Basis of Making DSL with Ruby2. Who I am
⢠Yasuko Ohba
⢠Ruby Programmer
⢠Developing rails applications
and iPhone applications
⢠Everyleaf Corporation
2010 8 28
3. Housekeeping Book on Web
Kozuchi
⢠http://www.kozuchi.net
⢠http://github.com/everyleaf/kozuchi
2010 8 28
4. ⢠@nay3 (twitter)
⢠http://github.com/nay
⢠y.ohba@everyleaf.com
2010 8 28
15. DSL
Hospitals
2010 8 28
18. Libraries Game
DSL
Hospitals EC
2010 8 28
24. DSL examples
⢠Rails
⢠routes.rb
⢠migrations
⢠RJS
⢠RSpec
⢠Rake
2010 8 28
25. DSL-like codes : Rails
class NotesController
before_ďŹlter :ďŹnd_group
...
end
2010 8 28
28. it can be less DSL like
self.add_before_ďŹlter_methods(
:ďŹnd_group)
2010 8 28
29. much more DSL like
self.add_before_ďŹlter_methods(
:ďŹnd_group)
before_ďŹlter :ďŹnd_group
2010 8 28
32. DSL-like codes : migrations
create_table :users do |t|
t.string :name
t.timestamps
end
2010 8 28
33. DSL-like codes : RSpec
describe Group do
it âname â do
g = Group.new
g.should_not be_valid
end
2010 8 28
39. Do we just use
existing great DSLs ?
Photo by Lawrence OP
2010 8 28
53. Hey receiver do THIS!
array.clear
array[1]
array.collect!{...}
hash.delete(key)
string.empty?
2010 8 28
60. examples : declarative expression
validates_presence_of :name
before_ďŹlter ...
has_many :children
2010 8 28
61. typical features
⢠describe status or nature
⢠receivers are not always important
⢠no brackets
2010 8 28
63. No money
self.money = 0
poor
2010 8 28
65. No money
class Boy
poor
...
end
2010 8 28
67. Object task
task
task
2010 8 28
73. Example 1
Declare the class to have
some nature implemented as
a module
2010 8 28
74. Book
(Class)
class Book
include "Product"
...
end
Product
2010 8 28
75. class Book
include "Product"
...
end
class Book
acts_as_product
...
end
2010 8 28
76. class Book
acts_as_product
...
end
NameError: undeďŹned local variable or
method `acts_as_product' for Book:Class
2010 8 28
77. You need
acts_as_product
method
2010 8 28
79. Book Object
(Class) (Class)
acts_as_product
2010 8 28
80. Book Object
(Class) (Class)
acts_as_product
2010 8 28
81. Book Object
(Class) (Class)
acts_as_product
Product
2010 8 28
82. the easier way
class Object
def self.acts_as_product
include Product
end
end
the caller (Book) will be self here
2010 8 28
83. Book
(Class)
class Book
acts_as_product
...
end
Product
2010 8 28
84. class Object
def self.acts_as_product
include Product
end
end
Execute this before acts_as_product is invoked
Rails conďŹg/initializer/acts_as_product.rb
2010 8 28
85. the softer way
module ActsAsProduct
module ClassMethods
def acts_as_product
include Product
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
Object.instance_eval { include ActsAsProduct }
2010 8 28
86. Object
(Class)
ActsAsProduct
2010 8 28
87. Object
(Class)
add
ActsAsProduct acts_as_product
ClassMethods
2010 8 28
88. Book Object
(Class) (Class)
add
ActsAsProduct acts_as_product
ClassMethods
2010 8 28
89. Book Object
(Class) (Class)
call
acts_as_product
add
ActsAsProduct acts_as_product
ClassMethods
2010 8 28
90. Book Object
(Class) (Class)
call
acts_as_product
add
ActsAsProduct acts_as_product
Product
ClassMethods
2010 8 28
91. Example 2
set @title for a layout in some
actions in Rails controllers
2010 8 28
92. Request
Controller
index show new edit
@title
layout
2010 8 28
93. want to write like this
class BooksController < ApplicationController
title " ", :only => [:edit, :update]
....
end
2010 8 28
98. title " ",
:only => [:edit, :update]
2010 8 28
99. ApplicationController
class ApplicationController < ...
attr_accessor :title
def self.title(name, options={})
before_ďŹlter(options) {|controller|
controller.title = name}
end
.....
2010 8 28
102. examples : rake
namespace :myapp do
task :my_rake_task do
# rake
end
end
2010 8 28
103. Example : Rails Form
Helper
form_for :book do |f|
f.text_ďŹeld :name
f.submit
end
2010 8 28
104. Usages of block for DSL
⢠describe complex structure
⢠get some tasks into a scope
2010 8 28
106. Example 3
Write a game rule
using playing cards
2010 8 28
107. Sevens
game 'sevens' do |g|
g.use 53
g.deal 53, :to => :each_player
...
end
2010 8 28
108. Top
You Level
Game
game use
deal
2010 8 28
109. class Game
def use (card_size, options ={})
...
end
def deal (card_size, options ={})
...
end
end
2010 8 28
110. def game
g = Game.new
yield g
end
2010 8 28
111. Top Game
You Level
2010 8 28
112. Top Game
You Level
game
Block
2010 8 28
113. Top Game
You Level
game new
Block
2010 8 28
114. Top Game
You Level
game new
Block
2010 8 28
115. Top Game
You Level
use
game new
deal
Block
2010 8 28
117. without parameter g
game 'sevens' do
use 53
deal 53, :to => :each_player
...
end
2010 8 28
118. use instance_exec
def game(&block)
g = Game.new
g.instance_exec(&block)
end
2010 8 28
119. can be changed slightly
game 'sevens' do
uses 53
deals 53, :to => :each_player
...
end
2010 8 28
121. methods
that work as noun
2010 8 28
123. books_url
f.submit page.assert
session request
current_user
2010 8 28
126. should
it years_since
bytes
from_now
2010 8 28
127. Balance
REALLY Cosmos
Useful ? of OOP
2010 8 28
129. General Tips
⢠provide default values for parameters
⢠use a hash parameter
⢠use symbols
⢠naming methods nicely
2010 8 28
131. DSL is
not very special
2010 8 28