Slide 1

Slide 1 text

Micro Libraries FTW Piotr Solnica wroclove.rb 2014

Slide 2

Slide 2 text

Who am I? Ruby and Rails developer OSS ruby hacker github.com/solnic @_solnic_

Slide 3

Slide 3 text

Ruby Object Mapper github.com/rom-rb

Slide 4

Slide 4 text

Agenda Big libraries vs micro libraries Micro libraries and frameworks Example micro libraries microrb.com

Slide 5

Slide 5 text

Big Libraries vs Micro Libraries

Slide 6

Slide 6 text

BIG LIBRARIES MICRO LIBRARIES Solving many problems Solving just one problem Big LOC Small LOC Wide interfaces Narrow interfaces High churn even after 1.0 Low churn especially after 1.0

Slide 7

Slide 7 text

Rails Influence

Slide 8

Slide 8 text

Ruby developers <3 Rails

Slide 9

Slide 9 text

Rails has a huge impact on how we write Ruby

Slide 10

Slide 10 text

Rails is in favor of huge libraries ActiveSupport and a wide adoption of monkey- patching ActiveRecord-like style - one object with a plethora of responsibilities and mixing seemingly related concerns

Slide 11

Slide 11 text

Rails is great but let’s move on

Slide 12

Slide 12 text

Let’s embrace Unix philosophy

Slide 13

Slide 13 text

“Many UNIX programs do quite trivial things in isolation, but, combined with other programs, become general and useful tools” The UNIX Programming Environment

Slide 14

Slide 14 text

Micro Libraries FTW

Slide 15

Slide 15 text

Why building a micro library?

Slide 16

Slide 16 text

Complex functionality that could be reused Constantly repeated design pattern A specific problem known up front

Slide 17

Slide 17 text

Building frameworks based on micro libraries

Slide 18

Slide 18 text

bottom-up vs top-down

Slide 19

Slide 19 text

bottom-up Really great when you know exactly what you’re doing

Slide 20

Slide 20 text

Does not happen very often

Slide 21

Slide 21 text

top-down Really great even when you don’t know exactly what you’re doing

Slide 22

Slide 22 text

Happens almost all the time

Slide 23

Slide 23 text

Unfortunately top-down approach can be misleading

Slide 24

Slide 24 text

DSL + core domain logic == disaster

Slide 25

Slide 25 text

Frameworks should provide high-level interfaces that are built on top of lower level micro libraries

Slide 26

Slide 26 text

Challenges

Slide 27

Slide 27 text

Dealing with many dependencies is hard

Slide 28

Slide 28 text

rubygems + bundler = slow “meta-gems” PANIC when seeing lots of gems being installed

Slide 29

Slide 29 text

Dependency Hell

Slide 30

Slide 30 text

2 gems depending on different versions of another gem == HELL

Slide 31

Slide 31 text

Version conflicts between frameworks are the biggest challenge

Slide 32

Slide 32 text

Semantic Versioning

Slide 33

Slide 33 text

Be responsible!

Slide 34

Slide 34 text

Changelogs

Slide 35

Slide 35 text

Tools can help!

Slide 36

Slide 36 text

ruby-toolbox.com lots of information about gems gemnasium.com detailed information about dependencies of gems

Slide 37

Slide 37 text

Example micro libraries

Slide 38

Slide 38 text

github.com/dkubb/equalizer

Slide 39

Slide 39 text

require "equalizer"! ! class GeoLocation! include Equalizer.new(:lat, :lng)! ! attr_reader :lat, :lng! ! def initialize(attributes)! @lat, @lng = attributes.values_at(:lat, :lng)! end! end! ! loc1 = GeoLocation.new(:lat => 123, :lng => 321)! loc2 = GeoLocation.new(:lat => 123, :lng => 321)! ! loc1 == loc2 # true! ! loc1.inspect! => #

Slide 40

Slide 40 text

github.com/mbj/anima

Slide 41

Slide 41 text

equalizer + anima

Slide 42

Slide 42 text

require "anima"! ! class GeoLocation! include Anima.new(:lat, :lng)! end! ! loc1 = GeoLocation.new(:lat => 123, :lng => 321)! loc2 = GeoLocation.new(:lat => 123, :lng => 321)! ! loc1 == loc2! # => true

Slide 43

Slide 43 text

virtus + equalizer

Slide 44

Slide 44 text

require "virtus"! ! class GeoLocation! include Virtus.value_object! ! values do! attribute :lat, Float! attribute :lng, Float! end! end! ! loc1 = GeoLocation.new(:lat => "123", :lng => "321")! loc2 = GeoLocation.new(:lat => "123", :lng => "321")! ! loc1 == loc2! # => true

Slide 45

Slide 45 text

How about a framework?

Slide 46

Slide 46 text

Ruby Object Mapper github.com/rom-rb/rom

Slide 47

Slide 47 text

morpher github.com/mbj/morpher

Slide 48

Slide 48 text

rom + morpher

Slide 49

Slide 49 text

module ROM! class Mapper! ! # Abstract loader class! #! # @private! class Loader! ! # some stuff is here! ! # @api public! def call(tuple)! transformer.call(tuple)! end! ! end # Loader! ! end # Mapper! end # ROM This is morpher’s transformer and it takes care of everything

Slide 50

Slide 50 text

virtus 2.0 = anima + morpher

Slide 51

Slide 51 text

microrb.com

Slide 52

Slide 52 text

Gather libraries that meet specific criteria Make it easy to find the right tool for the job Help in connecting maintainers and contributors Promote style of development that adheres to the unix philosophy Promote frameworks that are built on top of micro libraries

Slide 53

Slide 53 text

Thank You! <3<3<3