SlideShare a Scribd company logo
Monkeybars
     in the manor
Monkeybars in the Manor
What do you think?
    about




      Java?
DIE, DIE           DIE, DIE          DIE, DIE


                  DIE, DIE
    DIE, DIE                         DIE, DIE

                      DIE, DIE
                                 DIE, DIE
  DIE, DIE



                   DIE, DIE

       DIE, DIE                  DIE, DIE
                     DIE, DIE
Monkeybars in the Manor
Monkeybars in the Manor
What do you think?
    About




    JRuby?
What do you think?
     About




     JRuby?
    (it’s not so bad really)
Before...

JRuby Installed

Netbeans Downloaded

Gems
Before...

JRuby Installed

Netbeans Downloaded

Gems
Before...

JRuby Installed

Netbeans Downloaded

Gems
Before...

JRuby Installed

Netbeans Downloaded

Gems
Twitter Client
  The Spec

 •Login
 •Report errors
 •Display friend tweets
 •Post tweets
Creating...
the app
Initial Setup
        + scaffolding



$> monkeybars projectname
$> cd projectname
$> rawr install
  (choose option 2)
$> jruby -S rake generate ALL='src/login'
Install Gems
          and freeze Gems




$>   jruby -S gem install twitter
$>   cd lib/ruby
$>   jruby -S gem unpack twitter
$>   jruby -S gem unpack hpricot
Skeleton App
  will look something like this
Net Beans
        New Project



1. Import Existing Java project with sources
2. Add JRuby complete library
3. Add Swing..




                                     Net
                                     Beans
Net Beans
        New Project



1. Import Existing Java project with sources
2. Add JRuby complete library
3. Add Swing..



         LIVE DEMO!

                                     Net
                                     Beans
Under the hood
   Like Rails but not as you know it




       M VC
Under the hood
   Like Rails but not as you know it




       M VC
username.text


                             View
             password.text

              login_button




                                     username
Controller                   Model   password
Sample Code
     a quick look




controllers, models, views
     event handling
Controller
class LoginController < ApplicationController
  set_model 'LoginModel'
  set_view 'LoginView'
  set_close_action :exit

  def login_button_action_performed
    model, view_transfer = view_state

    if @user = model.login(model.username, model.password)
      TweetController.instance.setup(model)
      TweetController.instance.open
      close
    else
      transfer[:errors] = model.errors
      signal(:login_failed)
    end

  end
end
Model
class LoginModel
  attr_accessor :username, :password, :message, :errors, :user


  def login(username, password)
    begin
      @user = Twitter::Base.new(username, password)
      @user.timeline
      self
    rescue Twitter::CantConnect => e
      @errors ||= []
      @errors << $!.to_s
      return false
    end

  end

end
View
class LoginView < ApplicationView
  set_java_class 'login.LoginFrame'
  map :view => quot;username.textquot;, :model => :username
  map :view => quot;password.textquot;, :model => :password



  define_signal :name => :login_failed, :handler => :login_failed

  def login_failed(model, transfer)
    message.text = transfer[:errors].to_s
  end
                                                         nom
end
                                                      nom..
The App
pre alpha 0.333333333333
Monkeybars in the Manor
Result
Package
       For cross platform distro

                    rawr install


                               Text




2: Keep the current Rakefile, and add the rawr content to the top.


                                                             http://rawr.rubyforge.org/
Package
Commands
For cross platform distro




             Text




  Remember to edit your
build_configuration.yaml 1st
Package
        Sample Config
                for RAWR to generate app
# Name of the created jar file
project_name: tweetobix

# Directory to create and place produced project files in
output_dir: package

----------<SNIP>----------------

# Individual files that should be loaded on the classpath
classpath_files:
   - lib/java/jruby-complete.jar
   - lib/java/swing-layout-1.0.3.jar




                see build_configuration.yaml
Tricks
and tips
Monkeybars in the Manor
Why didn’t you
  use the twitter4r gem




        ?
jruby --command gem install twitter4r
      Building native extensions. This could take a while...
   mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:7: JRuby does not
support native extensions. Check wiki.jruby.org for alternatives.
                    (NotImplementedError)
     from mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:1:in `require'
                          from extconf.rb:1
               ERROR: Error installing twitter4r:
            ERROR: Failed to build gem native extension.
    mypath/jruby-1.1.4/bin/jruby extconf.rb install twitter4r


 Gem files will remain installed in /mypath/jruby-1.1.4/lib/ruby/
            gems/1.8/gems/json-1.1.3 for inspection.
 Results logged to mypath/jruby-1.1.4/lib/ruby/gems/1.8/gems/
         json-1.1.3/ext/json/ext/parser/gem_make.out
Requiring Gems
                   Frozen
require 'manifest'
# Add vendored gems to load path and require them automatically
Dir[File.expand_path(File.dirname(__FILE__) + quot;/../lib/ruby/**quot;)].map
do |dir|
  lib = quot;#{dir}/libquot;
  lib = dir if lib =~ /.w+$/ #File.directory? is broken in current
JRuby for dirs inside jars
  $: << lib

  gem_name = File.basename(dir.gsub(/-d+.d+.d+$/, ''))
  gem_path = dir + quot;/lib/quot; + gem_name + quot;.rbquot;
  begin
    require gem_path if File.exists? gem_path
  rescue
     p quot;could not load gem #{gem_path}quot;
  end
end
Gotchas
       a few


java.lang.NullPointerException: null




                                build paths
gems
-1’s
  The not so good
        bits



• Assumes knowledge of Java
• Quirky MVC
• Learning curve
• Early days...
Monkeybars in the Manor
+1’s
        The good
        bits


• GUI Builder (using Netbeans)
• Interface with mature Java libs
• It’s all Ruby!
• Cross platform distribution
• No platform dependancy on Ruby
• Cutting edge
Monkeybars in the Manor
Blinked?
     Go here




http://tweetobix.com
   Steps plus code to download
Monkeybars in the Manor
Fin.




martinbtt
         http://www.beyondthetype.com

More Related Content

Monkeybars in the Manor

  • 1. Monkeybars in the manor
  • 3. What do you think? about Java?
  • 4. DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE
  • 7. What do you think? About JRuby?
  • 8. What do you think? About JRuby? (it’s not so bad really)
  • 13. Twitter Client The Spec •Login •Report errors •Display friend tweets •Post tweets
  • 15. Initial Setup + scaffolding $> monkeybars projectname $> cd projectname $> rawr install (choose option 2) $> jruby -S rake generate ALL='src/login'
  • 16. Install Gems and freeze Gems $> jruby -S gem install twitter $> cd lib/ruby $> jruby -S gem unpack twitter $> jruby -S gem unpack hpricot
  • 17. Skeleton App will look something like this
  • 18. Net Beans New Project 1. Import Existing Java project with sources 2. Add JRuby complete library 3. Add Swing.. Net Beans
  • 19. Net Beans New Project 1. Import Existing Java project with sources 2. Add JRuby complete library 3. Add Swing.. LIVE DEMO! Net Beans
  • 20. Under the hood Like Rails but not as you know it M VC
  • 21. Under the hood Like Rails but not as you know it M VC
  • 22. username.text View password.text login_button username Controller Model password
  • 23. Sample Code a quick look controllers, models, views event handling
  • 24. Controller class LoginController < ApplicationController set_model 'LoginModel' set_view 'LoginView' set_close_action :exit def login_button_action_performed model, view_transfer = view_state if @user = model.login(model.username, model.password) TweetController.instance.setup(model) TweetController.instance.open close else transfer[:errors] = model.errors signal(:login_failed) end end end
  • 25. Model class LoginModel attr_accessor :username, :password, :message, :errors, :user def login(username, password) begin @user = Twitter::Base.new(username, password) @user.timeline self rescue Twitter::CantConnect => e @errors ||= [] @errors << $!.to_s return false end end end
  • 26. View class LoginView < ApplicationView set_java_class 'login.LoginFrame' map :view => quot;username.textquot;, :model => :username map :view => quot;password.textquot;, :model => :password define_signal :name => :login_failed, :handler => :login_failed def login_failed(model, transfer) message.text = transfer[:errors].to_s end nom end nom..
  • 27. The App pre alpha 0.333333333333
  • 30. Package For cross platform distro rawr install Text 2: Keep the current Rakefile, and add the rawr content to the top. http://rawr.rubyforge.org/
  • 31. Package Commands For cross platform distro Text Remember to edit your build_configuration.yaml 1st
  • 32. Package Sample Config for RAWR to generate app # Name of the created jar file project_name: tweetobix # Directory to create and place produced project files in output_dir: package ----------<SNIP>---------------- # Individual files that should be loaded on the classpath classpath_files: - lib/java/jruby-complete.jar - lib/java/swing-layout-1.0.3.jar see build_configuration.yaml
  • 35. Why didn’t you use the twitter4r gem ?
  • 36. jruby --command gem install twitter4r Building native extensions. This could take a while... mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError) from mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:1:in `require' from extconf.rb:1 ERROR: Error installing twitter4r: ERROR: Failed to build gem native extension. mypath/jruby-1.1.4/bin/jruby extconf.rb install twitter4r Gem files will remain installed in /mypath/jruby-1.1.4/lib/ruby/ gems/1.8/gems/json-1.1.3 for inspection. Results logged to mypath/jruby-1.1.4/lib/ruby/gems/1.8/gems/ json-1.1.3/ext/json/ext/parser/gem_make.out
  • 37. Requiring Gems Frozen require 'manifest' # Add vendored gems to load path and require them automatically Dir[File.expand_path(File.dirname(__FILE__) + quot;/../lib/ruby/**quot;)].map do |dir| lib = quot;#{dir}/libquot; lib = dir if lib =~ /.w+$/ #File.directory? is broken in current JRuby for dirs inside jars $: << lib gem_name = File.basename(dir.gsub(/-d+.d+.d+$/, '')) gem_path = dir + quot;/lib/quot; + gem_name + quot;.rbquot; begin require gem_path if File.exists? gem_path rescue p quot;could not load gem #{gem_path}quot; end end
  • 38. Gotchas a few java.lang.NullPointerException: null build paths gems
  • 39. -1’s The not so good bits • Assumes knowledge of Java • Quirky MVC • Learning curve • Early days...
  • 41. +1’s The good bits • GUI Builder (using Netbeans) • Interface with mature Java libs • It’s all Ruby! • Cross platform distribution • No platform dependancy on Ruby • Cutting edge
  • 43. Blinked? Go here http://tweetobix.com Steps plus code to download
  • 45. Fin. martinbtt http://www.beyondthetype.com