Class: Hudkins::Jobs

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Common
Defined in:
lib/hudkins/jobs.rb

Overview

this class is just a convenience wrapper around an array of jobs. FIX consider that Enumerable methods returns a Hudkins::Jobs obj instead of an array..

Instance Method Summary collapse

Methods included from Common

#check_host_availability, #get, #host_available?, #hudkins, #post, #url_escape

Constructor Details

#initialize(hudkins) ⇒ Jobs

Returns a new instance of Jobs.



8
9
10
11
12
13
14
15
# File 'lib/hudkins/jobs.rb', line 8

def initialize(hudkins)
  @hudkins = hudkins
  data = @hudkins.get_parsed( "/api/json", :accept => "application/json" )
  @jobs = Array.new
  data["jobs"].each do |job|
    @jobs << Hudkins::Job.new( @hudkins, job )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

nodoc



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hudkins/jobs.rb', line 55

def method_missing sym, *args, &block # :nodoc:
  meth, name = missing_method_name( sym )
  if name
    self.send(meth) do |job|
      # I can't remember why I'm using union... :/
      # job.send(name).to_s.gsub(/-/, "_") =~ Regexp.union( args.map(&:to_s) )
      # args is an array because of method_missing. but find(_all)_by only
      # takes one parameter (for now..)
      # to_s for symbols, also removes warnings if given a regexp with options.
      arg = args.first.to_s #.gsub(/-/, "_")
      regex = Regexp.new(arg, Regexp::IGNORECASE)
      # gsub used so I can pass in symbols as names (which don't allow dashes
      # to_s as job.url returns a URI
      job.send(name).to_s.gsub(/-/, "_") =~ regex
    end
  else
    super sym, *args, &block
  end
end

Instance Method Details

#eachObject

Enumerable This returns @jobs if no block_given.. so in effect we can use array methods like this: jobs.each.last is this hacky? Normal?



25
26
27
28
29
# File 'lib/hudkins/jobs.rb', line 25

def each
  @jobs.each do |job|
    yield job if block_given?
  end
end

#inspectObject



17
18
19
# File 'lib/hudkins/jobs.rb', line 17

def inspect
  super
end

#names(name = "") ⇒ Object

Description

convenience method for returning all (or part) of just the names of jobs



38
39
40
# File 'lib/hudkins/jobs.rb', line 38

def names name = ""
  find_all_by_name( name ).map(&:name)
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/hudkins/jobs.rb', line 75

def respond_to? sym
  missing_method_name sym or super sym
end

#sizeObject



31
32
33
# File 'lib/hudkins/jobs.rb', line 31

def size
  @jobs.size
end