Implement beautiful DSL for iOS using Ruby

Implement beautiful DSL for iOS using Ruby

At RubyConf Taiwan 2014 #ruby #rubyconftw

Ecdf9a83234e21743d77b75fd308b929?s=128

Francis Chong

April 26, 2014
Tweet

Transcript

  1. Implement beautiful DSL for iOS using Ruby Francis Chong

  2. About me

  3. 鍾振輝 Francis Chong @siuying

  4. Indie Mobile Dev

  5. Why DSL?

  6. https://github.com/tzangms/iloveck101 “I love ck101.com” ! “… I just want the

    pictures. But their site is slow, filled with ads and noise … ” @tzangms 海總理
  7. None
  8. None
  9. People found this is a good idea!

  10. • Go • https://github.com/wuman/go-ck101 • https://github.com/lazywei/iloveck101 • Ruby • https://github.com/wancw/iloveck101

    • https://github.com/shunwen/iloveck101 • PHP • https://github.com/fukuball/iloveck101 • Objective-C • https://github.com/saiday/hornyfolks • https://github.com/siuying/iloveck101 • Node.js • https://github.com/clonn/iloveck101 • Groovy • https://github.com/tangblack/ groovy_ILoveCK101 • Racket • https://github.com/Domon/iloveck101 • JavaScript • https://github.com/poying/iloveck101 • GreaseMonekey • https://github.com/tomin/iloveck101
  11. I made an iOS version too …

  12. Can we generalize it?

  13. Convert web sites into reusable data.

  14. None
  15. None
  16. Goals • Keep existing code and tools • Add dynamic

    behavior to a static app • Interact with native code • Simple to read and write
  17. DSL

  18. Domain 
 Specific 
 Language

  19. –Martin Fowler “DSLs are small languages, focused on a particular

    aspect of a software system.”
  20. External & Internal DSL

  21. How to implement DSL for iOS

  22. Internal DSL with Objective-C / RubyMotion

  23. Masonry https://github.com/cloudkite/Masonry

  24. Moria https://github.com/siuying/moria

  25. Kiwi https://github.com/allending/Kiwi

  26. Parser generator like Bison, CoreParse or PEGKit

  27. NUI https://github.com/tombenner/nui

  28. mruby

  29. objc-mruby example

  30. JavaScriptCore

  31. iOS 7.0

  32. Bi-directional Bridge http://blog.bignerdranch.com/3784-javascriptcore-and-ios-7/

  33. None
  34. … but this is a Ruby conference?

  35. Opal

  36. Ruby to JavaScript Compiler http://opalrb.org/try/

  37. Output

  38. JavaScriptCore + Opal

  39. Expose Native Classes to JS

  40. Opal Wrapper for JS

  41. Example

  42. Goal: Convert web sites into reusable data.

  43. Designing the DSL

  44. 1. Write an imaginary DSL

  45. 2. Make it valid

  46. 3. Make it work

  47. 4. Add more use case

  48. Everything

  49. http://cl.ly/3Y1y0N3y2J1V

  50. Now we’ve supported one site

  51. Let’s add more!

  52. None
  53. http://cl.ly/0R0k0t1x2v2r

  54. Users could change the code themselves

  55. Add sites without recompile

  56. Practical Tips

  57. JavaScriptCoreOpalAdditions

  58. Compile Opal in Runtime

  59. IGJavaScriptConsole

  60. Interactive Console

  61. Rake and sprockets

  62. Gemfile

  63. Rakefile

  64. Integrate the project into Xcode

  65. Custom Build Phase

  66. Add build task to project

  67. Pros and Cons

  68. Based on features in iOS SDK

  69. Dynamic behavior

  70. Interaction with native code.

  71. Easy to read and write.

  72. One more layer of indirection.

  73. –App Store Review Guidelines “Apps that download code in any

    way or form will be rejected.”
  74. Readings

  75. http://www.duokan.com/book/43469

  76. http://www.amazon.com/gp/product/0321712943

  77. Question?