Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
The simplest gem you'll ever use
Grzegorz Witek
February 25, 2016
0
18
The simplest gem you'll ever use
Grzegorz Witek
February 25, 2016
Tweet
Share
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
38
Coercion in Ruby
arnvald
1
33
Writing config files in Ruby
arnvald
0
25
Speaking at RDRC
arnvald
0
28
Read more
arnvald
2
28
Your API is too slow!
arnvald
0
360
International to global
arnvald
0
20
Patterns, patterns everywhere
arnvald
0
17
Nomadic programmer - Baruco 2014 edition
arnvald
0
74
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
4
450
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
181
15k
Mobile First: as difficult as doing things right
swwweet
212
7.5k
Making the Leap to Tech Lead
cromwellryan
113
6.9k
Three Pipe Problems
jasonvnalue
89
8.6k
Streamline your AJAX requests with AmplifyJS and jQuery
dougneiner
125
8.5k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
100
5.9k
Teambox: Starting and Learning
jrom
121
7.6k
Writing Fast Ruby
sferik
612
57k
GitHub's CSS Performance
jonrohan
1020
410k
How New CSS Is Changing Everything About Graphic Design on the Web
jensimmons
212
11k
Building Your Own Lightsaber
phodgson
94
4.6k
Transcript
The simplest gem you’ll ever use @arnvald, 2016
The simplest (shortest) gems • activesupport-json_encoder (~200 LOC) • rails-patch-json-encode
(~35 LOC) • oj_mimic_json (10 LOC - without comments would fit into a tweet)
So I wrote a gem • gem ‘simple_operation’ • version
0.1.2 - 33 LOC • version 1.0.0 - 42 LOC (2 new features!) • ok, ok, it’s not the simplest gem ever (but close!)
Services / Actions / Operations • operation class is a
class that performs a set of steps • it has input (parameters) and output (result) • it has one public method • its name is a verb • it does not keep any state
Example - validate records class SelectValidRecords < SimpleOperation.new(:records) def call
records.select {|r| valid?(r) } end private def valid?(record); …; end end ValidateRecords.([invalid, invalid2, valid]) # => [valid]
Example - validate records class ValidateRecords < SimpleOperation.new(:records) result :valid_records,
:invalid_records def call split_records = records.group_by {|r| valid?(r)} result split_records[true], split_records[false] end private def valid?(record); …; end end ValidateRecords.([invalid, invalid2, valid]) # => <struct valid_records=[valid], invalid_records=[invalid,invalid2]>
Let’s see the code
Ruby “tricks” used • overriding Class.new • using class_eval •
using class name as a method name (in extension) • assigning class to a constant • using sugar syntax for call method
Resources • http://trailblazer.to/gems/operation/ • https://github.com/arnvald/simple_operation
We’re hiring! http://bit.do/kaligo-dev
Questions?
The simplest gem you’ll ever use @arnvald, 2016