Created
April 30, 2009 12:11
-
-
Save neves/104430 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
esta é uma solução que tenho implementada no PHP. | |
a configuração é feita no construtor, | |
pois o PHP não suporta código a nível de classe como o Ruby. | |
Como posso converter o comportamento abaixo para nivel de classe, | |
onde eu posso configurar direto na classe, sem utilizar o construtor? | |
=end | |
class CrudController | |
attr_reader :acoes | |
def initialize | |
@acoes = [:view, :edit, :destroy] | |
end | |
end | |
class BlogController < CrudController | |
def initialize | |
super | |
@acoes << :posts | |
end | |
end | |
crud = CrudController.new | |
blog = BlogController.new | |
puts crud.acoes.inspect # [:view, :edit, :destroy] | |
puts blog.acoes.inspect # [:view, :edit, :destroy, :blog] | |
# e estaticamente tambem | |
puts CrudController.acoes.inspect # [:view, :edit, :destroy] | |
puts BlogController.acoes.inspect # [:view, :edit, :destroy, :blog] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# segue abaixo uma sugestao de como gostaria de utilizar a classe | |
class CrudController | |
acoes = [:view, :edit, :destroy] | |
end | |
class BlogController | |
acoes << :posts | |
end | |
crud = CrudController.new | |
blog = BlogController.new | |
puts crud.acoes.inspect # [:view, :edit, :destroy] | |
puts blog.acoes.inspect # [:view, :edit, :destroy, :blog] | |
# e estaticamente tambem | |
puts CrudController.acoes.inspect # [:view, :edit, :destroy] | |
puts BlogController.acoes.inspect # [:view, :edit, :destroy, :blog] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment