public
Description: Because everyone else is doing it
Homepage:
Clone URL: git://github.com/joshuaclayton/dotfiles.git
dotfiles / irbrc.d / snip.rb
100644 36 lines (29 sloc) 0.552 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Snip
  class SnippetNotFoundError < StandardError; end
 
  @@snips = {}
 
  def self.create(name, &blk)
    @@snips[name.to_s] = blk
    @@snips.keys
  end
 
  def self.[](name)
    unless @@snips.has_key?(name.to_s)
      raise SnippetNotFoundError, "the snippet '#{name.to_s}' was not found"
    end
    @@snips[name.to_s].call
  end
 
  def self.clear
    @@snips.clear
  end
 
  def self.snips
    @@snips
  end
end
 
class Object
  def snip(name, &blk)
    if block_given?
      Snip.create(name, &blk)
    else
      Snip[name]
    end
  end
end