Module: Hookit::Hook

Defined in:
lib/hookit/hook.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object

awesome resource-backed dsl



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hookit/hook.rb', line 72

def method_missing(method_symbol, *args, &block)
  resource_klass = Hookit.resources.get(method_symbol)
  if resource_klass
    resource = resource_klass.new(*args)
    resource.dict = dict
    resource.instance_eval(&block) if block_given?
    if resource.can_run?
      actions = resource.action
      if actions.length > 1
        res = {}
        actions.each do |action|
          res[action] = resource.run action
        end
        res
      else
        resource.run actions.first
      end
    end
  else
    super
  end
end

Instance Method Details

#converge(map, list) ⇒ Object



19
20
21
# File 'lib/hookit/hook.rb', line 19

def converge(map, list)
  Converginator.new(map, list).converge!
end

#dbObject



33
34
35
# File 'lib/hookit/hook.rb', line 33

def db
  @db ||= Hookit::DB.new
end

#detect_platformObject



61
62
63
64
65
66
67
68
69
# File 'lib/hookit/hook.rb', line 61

def detect_platform
  Hookit.platforms.each do |key, value|
    platform = value.new
    if platform.detect?
      return platform
    end
  end
  false
end

#dictObject



37
38
39
# File 'lib/hookit/hook.rb', line 37

def dict
  @dict ||= {}
end

#get(key) ⇒ Object



45
46
47
# File 'lib/hookit/hook.rb', line 45

def get(key)
  dict[key]
end

#log(level, message) ⇒ Object



49
50
51
# File 'lib/hookit/hook.rb', line 49

def log(level, message)
  logger.log level, message
end

#loggerObject



53
54
55
# File 'lib/hookit/hook.rb', line 53

def logger
  @logger ||= Hookit::Logger.new(get(:logfile), get(:log_level))
end

#parse_payloadObject



11
12
13
14
15
16
17
# File 'lib/hookit/hook.rb', line 11

def parse_payload
  if not ARGV.empty?
    MultiJson.load ARGV.first, symbolize_keys: true
  else
    {}          
  end
end

#payloadObject



7
8
9
# File 'lib/hookit/hook.rb', line 7

def payload
  @payload ||= parse_payload
end

#platformObject



57
58
59
# File 'lib/hookit/hook.rb', line 57

def platform
  @platform ||= detect_platform
end

#registry(key, value = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/hookit/hook.rb', line 23

def registry(key, value=nil)
  # normalize the catalog with keys
  key = key.to_sym
  unless value.nil?
    db.put(key, value)
  else
    db.fetch(key)
  end
end

#set(key, value) ⇒ Object



41
42
43
# File 'lib/hookit/hook.rb', line 41

def set(key, value)
  dict[key] = value
end