Slide 1

Slide 1 text

Implement beautiful DSL for iOS using Ruby Francis Chong

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

鍾振輝 Francis Chong @siuying

Slide 4

Slide 4 text

Indie Mobile Dev

Slide 5

Slide 5 text

Why DSL?

Slide 6

Slide 6 text

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 海總理

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

People found this is a good idea!

Slide 10

Slide 10 text

• 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

Slide 11

Slide 11 text

I made an iOS version too …

Slide 12

Slide 12 text

Can we generalize it?

Slide 13

Slide 13 text

Convert web sites into reusable data.

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Goals • Keep existing code and tools • Add dynamic behavior to a static app • Interact with native code • Simple to read and write

Slide 17

Slide 17 text

DSL

Slide 18

Slide 18 text

Domain 
 Specific 
 Language

Slide 19

Slide 19 text

–Martin Fowler “DSLs are small languages, focused on a particular aspect of a software system.”

Slide 20

Slide 20 text

External & Internal DSL

Slide 21

Slide 21 text

How to implement DSL for iOS

Slide 22

Slide 22 text

Internal DSL with Objective-C / RubyMotion

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Parser generator like Bison, CoreParse or PEGKit

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

mruby

Slide 29

Slide 29 text

objc-mruby example

Slide 30

Slide 30 text

JavaScriptCore

Slide 31

Slide 31 text

iOS 7.0

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

… but this is a Ruby conference?

Slide 35

Slide 35 text

Opal

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Output

Slide 38

Slide 38 text

JavaScriptCore + Opal

Slide 39

Slide 39 text

Expose Native Classes to JS

Slide 40

Slide 40 text

Opal Wrapper for JS

Slide 41

Slide 41 text

Example

Slide 42

Slide 42 text

Goal: Convert web sites into reusable data.

Slide 43

Slide 43 text

Designing the DSL

Slide 44

Slide 44 text

1. Write an imaginary DSL

Slide 45

Slide 45 text

2. Make it valid

Slide 46

Slide 46 text

3. Make it work

Slide 47

Slide 47 text

4. Add more use case

Slide 48

Slide 48 text

Everything

Slide 49

Slide 49 text

http://cl.ly/3Y1y0N3y2J1V

Slide 50

Slide 50 text

Now we’ve supported one site

Slide 51

Slide 51 text

Let’s add more!

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

http://cl.ly/0R0k0t1x2v2r

Slide 54

Slide 54 text

Users could change the code themselves

Slide 55

Slide 55 text

Add sites without recompile

Slide 56

Slide 56 text

Practical Tips

Slide 57

Slide 57 text

JavaScriptCoreOpalAdditions

Slide 58

Slide 58 text

Compile Opal in Runtime

Slide 59

Slide 59 text

IGJavaScriptConsole

Slide 60

Slide 60 text

Interactive Console

Slide 61

Slide 61 text

Rake and sprockets

Slide 62

Slide 62 text

Gemfile

Slide 63

Slide 63 text

Rakefile

Slide 64

Slide 64 text

Integrate the project into Xcode

Slide 65

Slide 65 text

Custom Build Phase

Slide 66

Slide 66 text

Add build task to project

Slide 67

Slide 67 text

Pros and Cons

Slide 68

Slide 68 text

Based on features in iOS SDK

Slide 69

Slide 69 text

Dynamic behavior

Slide 70

Slide 70 text

Interaction with native code.

Slide 71

Slide 71 text

Easy to read and write.

Slide 72

Slide 72 text

One more layer of indirection.

Slide 73

Slide 73 text

–App Store Review Guidelines “Apps that download code in any way or form will be rejected.”

Slide 74

Slide 74 text

Readings

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

Question?