Class: SC::Helpers::EntrySorter

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/helpers/entry_sorter.rb

Overview

Sorts a set of entries, respecting any “requires” found in the entries. To use the sorter, just call the class method EntrySorter.sort() passing the entries to sort along with any filenames you prefer to have added to the top. If you don’t specify any filenames, then the entries will be sorted alphabetically except for requires.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(preferred_filenames = []) ⇒ EntrySorter

Returns a new instance of EntrySorter.



23
24
25
# File 'lib/sproutcore/helpers/entry_sorter.rb', line 23

def initialize(preferred_filenames = [])
  @preferred_filenames = preferred_filenames
end

Instance Attribute Details

#preferred_filenamesObject (readonly)

Returns the value of attribute preferred_filenames.



27
28
29
# File 'lib/sproutcore/helpers/entry_sorter.rb', line 27

def preferred_filenames
  @preferred_filenames
end

Class Method Details

.sort(entries, preferred_filenames = []) ⇒ Object



19
20
21
# File 'lib/sproutcore/helpers/entry_sorter.rb', line 19

def self.sort(entries, preferred_filenames = [])
  self.new(preferred_filenames).sort(entries)
end

Instance Method Details

#sort(entries) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sproutcore/helpers/entry_sorter.rb', line 29

def sort(entries)
  # first sort entries by filename - ignoring case
  entries = entries.sort do |a,b| 
    a = (a.filename || '').to_s.downcase
    b = (b.filename || '').to_s.downcase

    # lproj/foo_page.js and main.js are loaded last
    a_kind = (a =~ /lproj\/.+_page\.js$/) ? 1 : -1
    a_kind = 2 if a =~ /main.js$/

    b_kind = (b =~ /lproj\/.+_page\.js$/) ? 1 : -1
    b_kind = 2 if b =~ /main.js$/
    
    if a_kind != b_kind
      a_kind <=> b_kind
    else
      a <=> b
    end
    
  end
  all_entries = entries.dup # needed for sort...
 
  # now process each entry to handle requires
  seen = [] 
  ret = [] 
  while cur = next_entry(entries)
    add_entry_to_set(cur, ret, seen, entries, all_entries)
  end

  return ret
end