diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2009-11-13 11:51:21 +0100 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2009-11-13 11:51:21 +0100 |
commit | 41e5d1e6160115356c54d2907552b9a943126ae1 (patch) | |
tree | 8b011c251ecfbd69d2435d07b91e275f56fc1682 /report/util.rb | |
parent | 14904ee55e85000b97fa7b735c22a82529fbdf10 (diff) |
merged reports into this rep
Diffstat (limited to 'report/util.rb')
-rw-r--r-- | report/util.rb | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/report/util.rb b/report/util.rb new file mode 100644 index 0000000..b460d48 --- /dev/null +++ b/report/util.rb @@ -0,0 +1,102 @@ + +# graph-files are generated in the tmp-dir before they are stored +ENV['TMP_DIR'] = File.join(FileUtils.pwd,"reports","tmp") unless ENV['TMP_DIR'] + +class Object + + # checks weather two objects have the same values for __equal_attributes__ + # + def has_equal_attributes?(equal_attributes, object) + equal_attributes.each{ |a| return false if send(a) != object.send(a) } + return true + end + +end + +# = Reports::Util +# +# utilities +# +module Reports::Util + + # groups attributes the into groups with equal values for __equal_attributes__, returns an array with sub-arrays for each group + # * see Reports::UtilTest for an example + # + # call-seq: + # self.group( objects, equal_attributes ) => array + # + def self.group( objects, equal_attributes ) + + result = Array.new + + objects.each do |o| + + match = false + result.each do |r| + if o.has_equal_attributes?(equal_attributes, r[0]) + match = true + r.push(o) + break + end + end + + if !match + result.push(Array.new) + result[-1].push(o) + end + end + + return result + end + + # checks weather all groups in a grouping (generated by self.group) have have corresponding objects + # * i.e. for each object in a group, there must be a object in each other group that has equal values for __match_attributes__ + # * see Reports::UtilTest for an example + # * raises exception if no matching + # + # call-seq: + # self.check_group_matching( grouped_objects, match_attributes ) + # + def self.check_group_matching( grouped_objects, match_attributes ) + + raise Reports::BadRequest.new("less then 2 groups, no matching possible") if grouped_objects.size<2 + first_group = grouped_objects[0] + other_groups = grouped_objects[1..-1].collect{ |g| g.collect{|o| o }} + other_groups.each{ |g| raise Reports::BadRequest.new("groups are not equally sized, matching impossible") if g.size != first_group.size } + + first_group.each do |o| + + #puts "match "+o.to_s + other_groups.each do |group| + + match = false + group.each do |o2| + #puts "try "+o2.to_s + if o.has_equal_attributes?(match_attributes, o2) + match = true + group.delete(o2) + break + end + end + raise Reports::BadRequest.new("no match found for "+o.to_s) unless match + end + end + end + + # returns the path of a file in directory ENV['TMP_DIR'], beginning with __tmp_file_name__, that does not exist yet + # + # call-seq: + # self.create_tmp_file(tmp_file_name) => string + # + def self.create_tmp_file(tmp_file_name) + + tmp_file_path = nil + FileUtils.mkdir ENV['TMP_DIR'] unless File.directory?(ENV['TMP_DIR']) + raise "TMP_DIR does not exist and cannot be created" unless File.directory?(ENV['TMP_DIR']) + while (!tmp_file_path || File.exist?(tmp_file_path) ) + tmp_file_path = ENV['TMP_DIR']+"/#{tmp_file_name}.#{Time.now.strftime("%Y-%-m-%d_%H-%M-%S")}.#{rand(11111).to_s}" + end + return tmp_file_path + end + +end
\ No newline at end of file |