Module: Bj::JobList::ClassMethods

Defined in:
lib/bj/joblist.rb

Instance Method Summary collapse

Instance Method Details

#for(jobs, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bj/joblist.rb', line 4

def for jobs, options = {}
  if Joblist === jobs
    jobs.update options
    return jobs
  end
  options.to_options!
  jobs = [jobs].flatten.compact
  list = []
  jobs.each do |arg|
    list.push(
      case arg
        when String
          case arg
            when %r/^\d+$/
              job_from_id arg
            else
              job_from_string arg
          end
        when Hash
          job_from_hash arg
        when Io
          jobs_from_io arg
        when Fixnum, Bignum
          job_from_number arg
        else
          job_from_string arg
      end
    )
  end
  list.flatten!
  list.compact!
  list.map!{|job| job.reverse_merge! options}
  list
end

#job_from_hash(arg) ⇒ Object



39
40
41
# File 'lib/bj/joblist.rb', line 39

def job_from_hash arg
  arg.to_hash.to_options!
end

#job_from_number(arg) ⇒ Object



51
52
53
54
# File 'lib/bj/joblist.rb', line 51

def job_from_number arg
  id = arg.to_i
  Table::Job.find(id).to_hash
end

#job_from_string(arg) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/bj/joblist.rb', line 43

def job_from_string arg
  unless arg.strip.empty?
    { :command => arg.to_s }
  else
    nil
  end
end

#jobs_from_io(arg) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bj/joblist.rb', line 56

def jobs_from_io arg 
  if arg == "-"
    load_from_io STDIN
  else
    if arg.respond_to? :read
      load_from_io arg 
    else
      open(arg, "r"){|fd| load_from_io fd}
    end
  end
end

#jobs_from_yaml(arg) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bj/joblist.rb', line 78

def jobs_from_yaml arg 
  object =
    if arg == "-"
      YAML.load STDIN
    else
      if arg.respond_to? :read
        YAML.load arg
      else
        open(arg, "r"){|fd| YAML.load fd}
      end
    end
  Joblist.for object
end

#load_from_io(io) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/bj/joblist.rb', line 68

def load_from_io io
  list = []
  io.each do |line|
    line.strip!
    next if line.empty?
    list << job_from_string(line)
  end
  list
end