Sha256: c377a415460b6bb1367473535de3343afc119c4323c09aa6c15d0ab2ba9e7c77

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

class Cronicle::DSL::Context::Job
  def initialize(target, &block)
    @result = Hash.new {|hash, key|
      hash[key] = {
        :servers => Array(target[:servers]),
        :roles => Array(target[:roles]),
        :job => {}
      }
    }

    instance_eval(&block)
  end

  attr_reader :result

  def job(name, opts = {}, &block)
    name = name.to_s

    raise ArgumentError, %!Job name is required! if (name || '').strip.empty?

    if @result.has_key?(name)
      raise "Job `#{name}`: already defined"
    end

    unless opts.kind_of?(Hash)
      raise TypeError, "Job `#{name}`: wrong argument type #{opts.class} (expected Hash)"
    end

    unless opts[:user]
      raise ArgumentError, "Job `#{name}`: :user is required"
    end

    opts.assert_valid_keys(:schedule, :user, :content)

    if opts[:content] and block
      raise ArgumentError, 'Can not pass :content and block to `job` method'
    elsif not opts[:content] and not block
      raise ArgumentError, "Job `#{name}`: :context or block is required"
    end

    job_hash = @result[name][:job]
    job_hash[:name] = name
    job_hash[:user] = opts.fetch(:user).to_s
    job_hash[:schedule] = opts[:schedule].to_s if opts[:schedule]

    if block
      source = block.to_raw_source(:strip_enclosure => true).each_line.to_a
      source = source.shift + source.join.unindent

      job_hash[:content] = <<-RUBY
#!/usr/bin/env ruby
#{source}
      RUBY
    else
      job_hash[:content] = opts[:content].to_s.unindent
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cronicle-0.1.4 lib/cronicle/dsl/context/job.rb
cronicle-0.1.3 lib/cronicle/dsl/context/job.rb
cronicle-0.1.2 lib/cronicle/dsl/context/job.rb