Module: Orasaurus::DB::Builder

Defined in:
lib/orasaurus/db.rb

Class Method Summary collapse

Class Method Details

.sort(arr, opts = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/orasaurus/db.rb', line 71

def self.sort(arr,opts={})
  firsts = Array.new
  if tmp = Array.try_convert(opts[:first])
    firsts = opts[:first].to_ary
  elsif tmp = String.try_convert(opts[:first])
    firsts = Array.new.push(opts[:first])
  end

  if opts[:method] == :SQL then
    return self.sort_by_sql(arr, opts[:db_connection])
  else
    return ( firsts + ( arr - firsts ).sort ).compact
  end

end

.sort_by_sql(arr, dbconnection) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/orasaurus/db.rb', line 87

def self.sort_by_sql(arr,dbconnection)
  puts("*********SORT BY SQL")
  ordered_list = Array.new
  done_collecting = false
  loop_idx = 0
  stripped_items = {}
  
  while not done_collecting

    loop_idx += 1

    arr.each{ |object|

      if not ordered_list.include?(object) then

        dependency_list = dbconnection.get_dependencies(dbconnection.username,strip_item(object))
        dependencies = Array.new
        dependency_list.each{ |i| dependencies.push(i.fetch(:object_name).downcase) }
        if dependencies.length == 0 then
          ordered_list.push(object)
        else
          matches = ordered_list.select do |match| 
            dependencies.include?(strip_item(match)) 
          end
          matches = matches.sort
          if matches.collect{ |m| strip_item(m)}.eql?(dependencies)
            ordered_list.push(object)
          end
        end
        
      end
    }

    if ordered_list.sort.eql?(arr.sort) then
      done_collecting = true
      puts "done colecting dependency matches. all objects have been collected and matched."
    end

    if loop_idx > 30
      done_collecting = true
      puts "giving up on finishing collection"
      puts "difference (missing ones): "
      puts arr - ordered_list
    end

  end

  return ordered_list

end

.strip_item(item) ⇒ Object



67
68
69
# File 'lib/orasaurus/db.rb', line 67

def self.strip_item(item)
  return item.gsub(/\..*$/,'')
end