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
73
emberjs
mribica
1
220
Other Decks in Programming
See All in Programming
Reactive Thinking with Signals and the Resource API
manfredsteyer
PRO
0
120
品質ワークショップをやってみた
nealle
0
650
Catch Up: Go Style Guide Update
andpad
0
260
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
260
エンジニアインターン「Treasure」とHonoの2年、そして未来へ / Our Journey with Hono Two Years at Treasure and Beyond
carta_engineering
0
440
What's new in Spring Modulith?
olivergierke
1
170
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
310
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
440
NIKKEI Tech Talk#38
cipepser
0
280
Ktorで簡単AIアプリケーション
tsukakei
0
110
TFLintカスタムプラグインで始める Terraformコード品質管理
bells17
2
460
Six and a half ridiculous things to do with Quarkus
hollycummins
0
220
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
How to train your dragon (web standard)
notwaldorf
97
6.3k
Scaling GitHub
holman
463
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
Facilitating Awesome Meetings
lara
57
6.6k
Into the Great Unknown - MozCon
thekraken
40
2.1k
Agile that works and the tools we love
rasmusluckow
331
21k
RailsConf 2023
tenderlove
30
1.3k
Making Projects Easy
brettharned
120
6.4k
How to Think Like a Performance Engineer
csswizardry
27
2.1k
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