Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
The simplest gem you'll ever use
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Grzegorz Witek
February 25, 2016
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
The simplest gem you'll ever use
Grzegorz Witek
February 25, 2016
Transcript
12 slides
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
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
130
Coercion in Ruby
arnvald
1
210
Writing config files in Ruby
arnvald
0
190
Speaking at RDRC
arnvald
0
190
Read more
arnvald
2
160
Your API is too slow!
arnvald
0
790
International to global
arnvald
0
170
Patterns, patterns everywhere
arnvald
0
180
Nomadic programmer - Baruco 2014 edition
arnvald
0
160
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
900
Color Theory Basics | Prateek | Gurzu
gurzu
1
530
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Code Review Best Practice
trishagee
74
20k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
The browser strikes back
jonoalderson
0
2k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
610