Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
make your own gem
Search
mmr
December 19, 2013
Programming
0
100
make your own gem
Step by step process for building your own gem including two examples
mmr
December 19, 2013
Tweet
Share
More Decks by mmr
See All by mmr
rails decorators
mribica
0
72
emberjs
mribica
1
220
Other Decks in Programming
See All in Programming
Claude Codeで実装以外の開発フロー、どこまで自動化できるか?失敗と成功
ndadayo
4
1.9k
Claude Codeで挑むOSSコントリビュート
eycjur
0
200
時間軸から考えるTerraformを使う理由と留意点
fufuhu
14
4.4k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
300
速いWebフレームワークを作る
yusukebe
5
1.7k
アプリの "かわいい" を支えるアニメーションツールRiveについて
uetyo
0
200
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
240
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
250
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
230
Microsoft Orleans, Daprのアクターモデルを使い効率的に開発、デプロイを行うためのSekibanの試行錯誤 / Sekiban: Exploring Efficient Development and Deployment with Microsoft Orleans and Dapr Actor Models
tomohisa
0
240
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
The Art of Programming - Codeland 2020
erikaheidi
55
13k
How to train your dragon (web standard)
notwaldorf
96
6.2k
Rails Girls Zürich Keynote
gr2m
95
14k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Fireside Chat
paigeccino
39
3.6k
Unsuck your backbone
ammeep
671
58k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Done Done
chrislema
185
16k
Bash Introduction
62gerente
615
210k
Transcript
ruby gems proces pravljenja i korisni paterni
Sadrzaj • struktura i proces kreiranja gem-a • short-url •
gitshot • https://github.com/mribica
Struktura % short-url . |__short-url.gemspec |__lib/ |__short-url/ |__short-url.rb
gemspec • specifikacija gem-a • osnovne informacije (naziv, opis, autor)
• verzija • specifikacija datoteka koje ce biti ukljucene u gem • specifikacija izvrsnih skripti
% short-url.gemspec Gem::Specification.new do |s| s.name = 'short-url' s.version =
'0.0.1' s.date = '2012-11-22' s.summary = "short url generator" s.description = "short url generator using bit shuffle" s.authors = ["Muamer Ribica"] s.email = '
[email protected]
' s.files = ["lib/short_url.rb"] s.homepage = 'http://www.github.com/mribica/short-url' s.license = 'MIT' end
Buildanje i instalacija • $ gem build short-url.gemspec • $
gem install ./short-url-0.0.1.gem • $ gem push ./short-url-0.0.1.gem • ... login • gem install short-url (sa rubygems.org)
short-url gem • generisanje nepredvidivih, jedinstvenih kljuceva • class i
instance metode za generisanje kljuca • konfigurabilan modul (initializer)
# short_url.rb module ShortUrl ... end # test.rb class Test
include ShortUrl end Test.short_url(2) Test.new.short_url
class i instance metode
module ShortUrl def self.included(base) base.extend(ClassMethods) end def short_url # instance
metoda end module ClassMethods def short_url(id) # class metoda end end end
konfigurabilan modul
• konfiguracija modula (alfabet, duzinu kljuca) • gdje cuvati konfiguraciju
(izolovanje) • cattr_accessor (Rails kroz ActiveSupport) • class varijabla u modulu
Meta klasa
• u ruby-u je sve objekat • svaki objekat ima
klasu • svaki objekat ima i meta klasu • meta klasa moze imati metode, koje su dostupne samo toj instanci
module Test class << self # self je sada meta
klasa modula attr_accessor :configuration end def self.configure self.configuration |= Configuration.new yield(configuration) end end # bilo gdje u aplikaciji Test::configure {|config| config.attr = ‘foo’} Test::configuration # => configuration obj
• konfiguracija je sada izolovana samo na modul • modul
se moze konfigurisati iz initializera • klasa koja ukljuci modul dobija samo metode koje ce koristiti
gitshot gem • slika nakon svakog komita • jednostavna instalacija
i inicijalizacija skripte • % gem install gitshot • gitrepo% gitshot install
gemspec executable Gem::Specification.new do |s| ... s.default_executable = %q{gitshot} s.executables
= ["gitshot"] ... end
require 'fileutils' module Gitshot class Hook HOOK_PATH = '.git/hooks/post-commit' def
install tmpl = File.expand_path("hook-templ", _FILE_) FileUtils.cp(tmpl, HOOK_PATH) FileUtils.chmod(0755, HOOK_PATH) end end end
gitrepo% gitshot install Gitshot installed .git/hooks/post-commit gitrepo% git commit -am
“commit” [Cheese!] Capturing image from device "Built-in iSight" b2bc7.jpeg