Class: WatchmonkeyCli::Application::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/watchmonkey_cli/application/configuration.rb

Defined Under Namespace

Modules: AppHelper

Instance Method Summary collapse

Constructor Details

#initialize(app, file = nil, tags = []) ⇒ Configuration

Returns a new instance of Configuration.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/watchmonkey_cli/application/configuration.rb', line 62

def initialize app, file = nil, tags = []
  @app = app
  @file = file
  @tags = tags
  return unless file
  begin
    eval File.read(file, encoding: "utf-8"), binding, file
  rescue
    app.error "Invalid config file #{file}"
    raise
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/watchmonkey_cli/application/configuration.rb', line 83

def method_missing meth, *args, &block
  if c = @app.checkers[meth.to_s]
    opts = args.extract_options!
    only = @app.opts[:tag_only]
    except = @app.opts[:tag_except]

    # build tags
    tags = (@tags + (opts[:tags] || []).map(&:to_sym))
    if @app.opts[:autotag]
      tags << :"WMC-#{c.class.checker_name}" # checker name
      if args[0].is_a?(Symbol)
        tags << :"WMS-#{args[0]}" # ssh/local connection
      elsif args[0].is_a?(String) && args[0].match(/\Ahttp(s)?:\/\//i)
        uri = URI.parse(args[0]) rescue false
        tags << :"WMH-#{uri.hostname.gsub(".", "_")}" if uri # hostname from URL
      end
    end
    tags = tags.uniq
    @app.tag_list.merge(tags)

    if only.any?
      if tags.any?{|t| only.include?(t) }
        if tags.any?{|t| except.include?(t) }
          return @app.debug "Skipping #{meth} with #{args} and #{opts} due to tag_except filter..."
        end
      else
        return @app.debug "Skipping #{meth} with #{args} and #{opts} due to tag_only filter..."
      end
    elsif tags.any?{|t| except.include?(t) }
      return @app.debug "Skipping #{meth} with #{args} and #{opts} due to tag_except filter..."
    end
    c.enqueue(*args, opts.merge(tags: tags))
  else
    super
  end
end

Instance Method Details

#ssh_connection(name, opts = {}, &b) ⇒ Object



75
76
77
# File 'lib/watchmonkey_cli/application/configuration.rb', line 75

def ssh_connection name, opts = {}, &b
  @app.fetch_connection(:ssh, name, opts, &b)
end

#tag_all!(*tags) ⇒ Object



79
80
81
# File 'lib/watchmonkey_cli/application/configuration.rb', line 79

def tag_all! *tags
  @tags = tags.map(&:to_sym)
end