Class: Cronicle::DSL::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cronicle/dsl/context.rb

Defined Under Namespace

Classes: Job

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}, &block) ⇒ Context

Returns a new instance of Context.



12
13
14
15
16
17
# File 'lib/cronicle/dsl/context.rb', line 12

def initialize(path, options = {}, &block)
  @path = path
  @options = options
  @result = []
  instance_eval(&block)
end

Instance Attribute Details

#resultObject (readonly)

of class methods



10
11
12
# File 'lib/cronicle/dsl/context.rb', line 10

def result
  @result
end

Class Method Details

.eval(dsl, path, opts = {}) ⇒ Object



3
4
5
6
7
# File 'lib/cronicle/dsl/context.rb', line 3

def eval(dsl, path, opts = {})
  self.new(path, opts) {
    Kernel.eval(dsl, binding, path)
  }
end

Instance Method Details

#on(target, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cronicle/dsl/context.rb', line 31

def on(target, &block)
  unless block
    raise ArgumentError, "Block is required for `on` method"
  end

  unless target.kind_of?(Hash)
    raise TypeError, "wrong argument type #{target.class} (expected Hash)"
  end

  if target.empty?
    raise ArgumentError, ':servers or :roles is not passed to `on` method'
  end

  target.assert_valid_keys(:servers, :roles)
  values = Cronicle::DSL::Context::Job.new(target, &block).result.values

  @result.concat(values.empty? ? [{
    :servers => Array(target[:servers]),
    :roles => Array(target[:roles]),
    :job => {}
  }] : values)
end

#require(file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cronicle/dsl/context.rb', line 19

def require(file)
  cronfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))

  if File.exist?(cronfile)
    instance_eval(File.read(cronfile), cronfile)
  elsif File.exist?(cronfile + '.rb')
    instance_eval(File.read(cronfile + '.rb'), cronfile + '.rb')
  else
    Kernel.require(file)
  end
end