Class: Pipeline::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeline/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Plugin

Returns a new instance of Plugin.



29
30
31
32
33
34
# File 'lib/pipeline/plugin.rb', line 29

def initialize(root)
  @root = Pathname.new(root)
  @config = Hash.new

  self.client
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



28
29
30
# File 'lib/pipeline/plugin.rb', line 28

def root
  @root
end

Class Method Details

.inherited(subclass) ⇒ Object



11
12
13
# File 'lib/pipeline/plugin.rb', line 11

def self.inherited(subclass)
  Pipeline::Plugin.plugins << subclass
end

.pluginsObject



7
8
9
# File 'lib/pipeline/plugin.rb', line 7

def self.plugins
  @@plugins ||= Array.new
end

.run(root) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pipeline/plugin.rb', line 15

def self.run(root)
  plugin = self.new(root)

  Dir.chdir(plugin.root.to_s) do
    EM.run do
      # First runs next tick from Client.boot.
      EM.next_tick do
        plugin.run!
      end
    end
  end
end

Instance Method Details

#clientObject



50
51
52
# File 'lib/pipeline/plugin.rb', line 50

def client
  @client ||= Pipeline::Client.boot(self.config('amqp'))
end

#config(key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pipeline/plugin.rb', line 36

def config(key)
  @config[key] ||= begin
    path = self.root.join("config/#{key}.json")
    data = JSON.parse(path.read)

    path = self.root.join("config/#{key}.local.json")
    data.merge!(JSON.parse(path.read)) if path.exist?

    data.reduce(Hash.new) do |buffer, (key, value)|
      buffer.merge!(key.to_sym => value)
    end
  end
end

#run!Object



54
55
56
57
58
59
60
61
# File 'lib/pipeline/plugin.rb', line 54

def run!
  self.client.on_open do
    EM.add_timer(0.1) do # Oh my, yeah, whatever, it's for now only, dammit!
      puts "~ Running #{self.class}#run."
      self.run
    end
  end
end