View
2.046
Download
1
Category
Preview:
DESCRIPTION
Palestra de Desenvolvimento Mobile com Ruby, no evento Rock And Rails, www.rockandrails.com.br
Citation preview
Rhomobile – Ruby no MobileRhomobile – Ruby no Mobile
Quem sou?
Rodrigo Martins
rodrigo@rrmartins.com
@rr_martins
www.rrmartins.com
Quem sou?
Rodrigo Martins
rodrigo@rrmartins.com
@rr_martins
www.rrmartins.com
AntesAntes→ → PHPPHP→ → JavaJava→ → ClipperClipper→ → ZimZim
Quem sou?
Rodrigo Martins
rodrigo@rrmartins.com
@rr_martins
www.rrmartins.com
AntesAntes→ → PHPPHP→ → JavaJava→ → ClipperClipper→ → ZimZim
Hoje, e Feliz!Hoje, e Feliz!→ → RubyRuby→ → PythonPython
Qual o futuro?
O que forma o Rhomobile?
Estrutura
Rhodes O que é? É um framework open-source, permite aos
desenvolvedores criar aplicações nativas para telefones móveis com a portabilidade de tecnologias web tais como XHTML, CSS, JavaScript e do poder do Ruby
Instalação:
$ gem install rhodes
$ rhodes-setup
Rhodes Generando aplicação:
$ rhodes app ror http://localhost:3000/application
rhodes app <name_application> <path_server> rhoconfig.txt ->
syncserver = 'http://localhost:3000/application'
Rhodes Generando aplicação:
$ rhodes app ror http://localhost:3000/application
rhodes app <name_application> <path_server> rhoconfig.txt ->
syncserver = 'http://localhost:3000/application'
Rhodes index.erb.htmlEste é o arquivo de início, desta forma
<nome_aplicacao>/app/. A partir de agora todo o código é um pouco semelhante a escrever um RubyOnRails aplicação.
Running the Application Rhodes Executando:
$ cd applicatonTest
$ rake run:iphone Rodando para outros Devises:
$ rake run:iphone
$ rake run:android
$ rake run:bb
rake tasks
$ rake -T
Rhodes Generando Models$ cd application$ rhodes model pessoa nome, idade, sobrenome, sexo, cpf Generating with model generator: [ADDED] app/Pessoa/index.erb [ADDED] app/Pessoa/edit.erb [ADDED] app/Pessoa/new.erb [ADDED] app/Pessoa/show.erb [ADDED] app/Pessoa/index.bb.erb [ADDED] app/Pessoa/edit.bb.erb [ADDED] app/Pessoa/new.bb.erb [ADDED] app/Pessoa/show.bb.erb [ADDED] app/Pessoa/pessoa_controller.rb [ADDED] app/Pessoa/pessoa.rb [ADDED] app/test/pessoa_spec.rb
Views Rhodes edition Exemplo rapido (app/index.erb.html):
<div id="pageTitle"><h1>ApplicationTest</h1></div>
<div id="toolbar"> <div id="leftItem" class="blueButton"> <%= link_to "Sync", :controller => :Settings, :action => :do_sync %> </div> <% if SyncEngine::logged_in > 0 %> <div id="rightItem" class="regularButton"> <%= link_to "Logout", :controller => :Settings, :action => :logout %> </div> <% else %> <div id="rightItem" class="regularButton"> <%= link_to "Login", :controller => :Settings, :action => :login %> </div> <% end %></div>
Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):
<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home" data-
direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>
Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):
<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home"
data-direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>
He came out of where?He came out of where?
Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):
<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home"
data-direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>
He came out of where?He came out of where?
start_path = '/app' # rhoconfig.txtstart_path = '/app' # rhoconfig.txt
RhoSync O que é?
Um framework de sincronização é uma estrutura composta por um componente de cliente no dispositivo e um componente de servidor que roda em qualquer servidor que podem executar o Ruby.
Instalar dependencias RhoSync$ sudo gem install rhosync
$ wget http://redis.googlecode.com/files/redis-2.2.7.tar.gz
$ tar xzf redis-2.2.7.tar.gz
$ cd redis-2.2.7
$ cd src; make
$ make install
$ cp ../redis.conf /usr/local/etc
More: www.redis.io
Instalar RhoSync$ rhosync app applicationTest-server
$ cd applicationTest-server/ If you are running first on the Mac or Linux,
you must install dtach:
$ sudo rake dtach:install
Rodando a aplicação RhoSync
$ rake redis:start
$ rake rhosync:start
If all went well you should see:
[12:30:15 PM 2011-07-22] Rhosync Server v2.1.0 started...
Definindo Adapters RhoSync
Ligar a um serviço de back-end com RhoSync requer que você escreva uma pequena quantidade de código Ruby para a consulta, criar, atualizar e excluir as operações de seu backend empresa particular.
$ cd /applicationTest
$ rhosync source pessoa
---
Generating with source generator:
[ADDED] sources/pessoa.rb
[ADDED] spec/sources/pessoa_spec.rb
Definindo Adapters RhoSync
pessoa.rbclass Pessoa < SourceAdapter def initialize(source) super(source) end
def login # TODO: Login to your data source here if necessary end
def query # TODO: Query your backend data source and assign the records # to a nested hash structure called @result. For example: # @result = { # "1"=>{"name"=>"Acme","industry"=>"Electronics"}, # "2"=>{"name"=>"Best", "industry"=>"Software"} # } raise SourceAdapterException.new("Please provide some code to read records from the backend data source") end
def sync # Manipulate @result before it is saved, or save it # yourself using the Rhosync::Store interface. # By default, super is called below which simply saves @result super end
def create(create_hash,blob=nil) # TODO: Create a new record in your backend data source # If your rhodes rhom object contains image/binary data # (has the image_uri attribute), then a blob will be provided raise "Please provide some code to create a single record in the backend data source using the create_hash" End
def update(update_hash) raise "Please provide some code to update a single record in the backend data source using the update_hash" end
pessoa.rbdef delete(object_id) # TODO: write some code here if applicable # be sure to have a hash key and value for "object" # for now, we'll say that its OK to not have a delete operation # raise "Please provide some code to delete a single object in the backend #application using the hash values in name_value_list" end
def logoff # TODO: Logout from the data source if necessary endend
settings/settings.yml The generator will modify this file:
#Sources
:sources:
Pessoa:
:poll_interval: 300
Testando synchronization$ rake rhosync:start
$ rake rhosync:stop
$ rake rhosync:restart
Sincronizando o model
class Pessoa
include Rhom::PropertyBag
enable :sync
end
Creando Objetos com RhoSyncdef create(create_hash, blob=nil)
result = RestClient.post(@base, :pessoa => create_hash)
location = "#{result.headers[:location]}.json"
new_record = RestClient.get(location).body
JSON.parse(new_record)["pessoa"]["id"].to_s
end
--------create_hash :
{
“name” => “Lucas”,
“idade” => “21”
}
Atualizar o adaptador de fonte
$ rake rhosync:restart
O objeto será criado imediatamente no cliente e enviado para o servidor na próxima sincronização.
Autenticação O arquivo application.rb, editando o metodo de authenticate:
def authenticate(username, password, session) # ... connect to backend using API and authenticate ... if success # save the data for later use in the source adapter Store.put_value("username:#{username}:token",username) end return successend
Source
http://docs.rhomobile.com/
http://rhomobile.com/
http://groups.google.com/group/rhomobile
Doubt? :D
Contact
Rodrigo Martins
rodrigo@rrmartins.com
@rr_martins
www.rrmartins.com
Recommended