Skip to content

Commit

Permalink
Add special_attributes with array
Browse files Browse the repository at this point in the history
  • Loading branch information
hallelujah committed Jan 31, 2012
1 parent 18d7b0b commit c670140
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
18 changes: 14 additions & 4 deletions lib/sge/job_helpers.rb
Expand Up @@ -5,36 +5,46 @@ def self.included(base)
base.extend ClassMethods
base.class_eval do
@attributes = []
@special_attributes = {}
end
end

module ClassMethods

def define_attributes(opts)
@attributes ||= []
opts.each do |k,v|
@attributes.push(k)
attr_accessor(v)
alias_method k, v
alias_method k, v
alias_method "#{k}=", "#{v}="
end
# If attributes changed reload attributes_path
@attributes_path = nil
@attributes
end

def special_attribute(a, path)
attr_accessor a
@special_attributes[a] = path
end

def attributes_path
@attributes_path ||= @attributes.join('|')
end


def from_document(doc)
instance = new
doc.xpath('.//' + self::attributes_path ).each do |node|
doc.xpath('.//' + self.attributes_path ).each do |node|
instance.send("#{node.name}=", node.text)
end
@special_attributes.each do |k,v|
doc.xpath('.//' + v.to_s)
instance.send("#{k}=", doc.xpath('.//' + v.to_s).map{ |n| n.text})
end
instance
end
end

end
end
end
4 changes: 2 additions & 2 deletions lib/sge/qstat/job.rb
Expand Up @@ -8,9 +8,9 @@ class Job
:JB_owner => :owner,
:JAT_start_time => :start_time,
:queue_name => :queue_name,
:JB_submission_time => :submission_time,
:state => :state
:JB_submission_time => :submission_time
)
special_attribute :state, "@state|state"

def self.load_documents(opts,&block)
documents = []
Expand Down
2 changes: 1 addition & 1 deletion spec/qstat/job_spec.rb
Expand Up @@ -31,7 +31,7 @@
lambda{ @job = SGE::QStat::Job.from_document(doc) }.should_not raise_error
@job.job_number.should == '2337'
@job.name.should == 's20100220_11;init_jour'
@job.state.should == 'r'
@job.state.should == ['r','running']
@job.owner.should == 'prodsoft'
@job.start_time.should == '2010-02-24T21:55:13'
@job.queue_name.should == 'calcul-x@wousdat-dev.in.weborama.fr'
Expand Down

0 comments on commit c670140

Please sign in to comment.