Sha256: c74b2a6ec44db18443daa4822f3c514ec7ca83501776183c0893668f88a7156a

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Pige::Client
  class ChunkScheduler

    attr_accessor :day, :default_attributes, :dry_run
    alias_method :dry_run?, :dry_run

    def initialize(day = Date.today)
      self.day = day
      self.default_attributes = { :format => "wav" }
    end

    def self.define(&block)
      yield new
    end

    def on(criteria, &block)
      if day.send("#{criteria}?")
        puts "Scheduling #{criteria}"
        instance_exec &block
      end
    end

    def create(attributes = {}, options = {})
      options = { :delay => 1 }.merge(options)
      attributes = default_attributes.merge(attributes)

      [:begin, :end].each do |endpoint|
        attributes[endpoint] = Time.parse(attributes[endpoint], day) if String === attributes[endpoint]
      end

      if duration = attributes.delete(:duration)
        if attributes[:begin]
          attributes[:end] = attributes[:begin] + duration
        else
          attributes[:begin] = attributes[:end] - duration
        end
      end

      if margin = attributes.delete(:margin)
        attributes[:begin] -= margin
        attributes[:end] += margin
      end

      if attributes[:title] =~ /%[dmyHMY]/
        attributes[:title] = day.strftime(attributes[:title])
      end

      puts "Create Chunk with #{attributes.inspect}"
      unless dry_run?
        Pige::Client::Chunk.create attributes 
        sleep options[:delay].to_i if options[:delay]
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tryphon-pige-client-0.0.3 lib/pige/client/chunk_scheduler.rb