Class: Cronicle::DSL::Context::Job
- Inherits:
-
Object
- Object
- Cronicle::DSL::Context::Job
- Defined in:
- lib/cronicle/dsl/context/job.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize(target, &block) ⇒ Job
constructor
A new instance of Job.
- #job(name, opts = {}, &block) ⇒ Object
Constructor Details
#initialize(target, &block) ⇒ Job
Returns a new instance of Job.
2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/cronicle/dsl/context/job.rb', line 2 def initialize(target, &block) @result = Hash.new {|hash, key| hash[key] = { :servers => Array(target[:servers]), :roles => Array(target[:roles]), :job => {} } } instance_eval(&block) end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
14 15 16 |
# File 'lib/cronicle/dsl/context/job.rb', line 14 def result @result end |
Instance Method Details
#job(name, opts = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cronicle/dsl/context/job.rb', line 16 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 |