Created
October 31, 2012 16:23
-
-
Save madwork/3988043 to your computer and use it in GitHub Desktop.
Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Manager | |
def self.configure(&block) | |
::Template.new(&block) | |
end | |
end | |
class Template | |
attr_reader :assets, :events | |
def initialize(&block) | |
@assets = {} | |
@events = {} | |
instance_eval(&block) | |
end | |
def asset(value, properties = {}) | |
@assets[value] ||= [] | |
@assets[value] << properties | |
end | |
def event(name, &block) | |
@events[name] = block | |
end | |
def send(action, options = {}) | |
puts "send #{action} #{options}" | |
end | |
def update(asset, name) | |
@assets[asset].select{|asset| asset[:name] == name}.pop[:reboot] ||= 0 | |
@assets[asset].select{|asset| asset[:name] == name}.pop[:reboot] += 1 | |
end | |
end | |
t = Manager.configure do | |
asset :serveur, | |
:name => 'DBX-001', | |
:ip => '10.0.10.1' | |
asset :serveur, | |
:name => 'DBX-002', | |
:ip => '10.0.10.2' | |
event :down do | |
send :mail, :to => '[email protected]' | |
send :notificaion, :to => '[email protected]' | |
end | |
event :restart do | |
update :serveur, 'DBX-002' | |
end | |
end | |
puts t.assets | |
puts t.events | |
t.events[:down].call | |
t.events[:restart].call | |
t.events[:restart].call | |
puts t.assets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment