bruce (owner)

Revisions

gist: 146455 Download_button fork
public
Public Clone URL: git://gist.github.com/146455.git
Embed All Files: show embed
tyranny.rb #
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# lib/tyranny.rb
 
require 'rubygems'
require 'rufus/tokyo/tyrant'
 
module Tyranny
  
  def self.configure(config)
    configuration.update(config) if config
  end
  
  def self.configuration
    @configuration ||= {}
  end
  
  def self.extractor
    @extractor ||= Extractor.new
  end
  
  def self.[](name)
    extractor[name]
  end
  
  class Extractor
  
    def [](name)
      tables[name.to_s] ||= table(name.to_s)
    end
  
    def tables
      @tables ||= {}
    end
    
    def close
      @tables.values.each do |table|
        table.close
      end
    end
    
    def forget
      close
      tables.clear
    end
  
    private
  
    def table(name)
      config = Tyranny.configuration[name]
      if config['filename'] && config['socket']
        local_table(config)
      else
        raise NotImplementedError, "Remote configuration not implemented"
      end
    end
  
    def local_table(config)
      local_server!(config)
      Rufus::Tokyo::TyrantTable.new(config['socket'])
    end
  
    def local_server!(config)
      return if File.socket?(config['socket'])
      system "ttserver -host '#{config['socket']}' -port 0 '#{config['filename']}' &"
    end
    
  end
      
end
tyranny.yml #
1
2
3
4
5
6
# config/tyranny.yml
 
development:
  extractors:
    filename: db/extractors.tct
    socket: tmp/sockets/tt-extractors.sock
tyranny_init.rb #
1
2
3
4
5
6
7
8
9
10
# config/initializers/tyranny_init.rb
 
require File.dirname(__FILE__) << "/../../lib/tyranny"
 
configuration = YAML.load(File.read(File.join(Rails.root, 'config/tyranny.yml')))
 
tyranny_config = configuration[Rails.env]
if tyranny_config
  Tyranny.configure(tyranny_config)
end