Class: Fargo::Search
- Inherits:
-
Object
- Object
- Fargo::Search
- Defined in:
- lib/fargo/search.rb
Constant Summary collapse
- ANY =
1
- AUDIO =
2
- COMPRESSED =
3
- DOCUMENT =
4
- EXECUTABLE =
5
- PICTURE =
6
- VIDEO =
7
- FOLDER =
8
- TTH =
9
- EXTENSIONS =
See www.teamfair.info/wiki/index.php?title=$Search for the extensions
{ AUDIO => [/mp(2|3)/, 'wav', 'au', /(r|s)m/, 'mid', 'flac', 'm4a'], COMPRESSED => ['zip', 'arj', 'rar', 'lzh', 'gz', 'z', 'arc', 'pak'], DOCUMENT => [/docx?/, 'txt', 'wri', 'pdf', 'ps', 'tex'], EXECUTABLE => ['pm', 'exe', 'bat', 'com'], PICTURE => ['gif', /jpe?g/, 'bmp', 'pcx', 'png', 'wmf', 'psd'], VIDEO => [/mpe?g/, 'avi', 'asf', 'mov', 'mkv'] }
Instance Attribute Summary collapse
-
#filetype ⇒ Object
Returns the value of attribute filetype.
-
#is_minimum_size ⇒ Object
Returns the value of attribute is_minimum_size.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#size ⇒ Object
Returns the value of attribute size.
-
#size_restricted ⇒ Object
Returns the value of attribute size_restricted.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Search
constructor
A new instance of Search.
- #matches?(map) ⇒ Boolean
- #queries ⇒ Object
- #query ⇒ Object
- #query=(query) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Search
Returns a new instance of Search.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fargo/search.rb', line 27 def initialize opts = {} @size_restricted = opts[:size_restricted] @is_minimum_size = opts[:is_minimum_size] @size = opts[:size] @filetype = opts[:filetype] || ANY if opts[:pattern] @pattern = opts[:pattern] elsif opts[:query] self.query = opts[:query] end end |
Instance Attribute Details
#filetype ⇒ Object
Returns the value of attribute filetype.
25 26 27 |
# File 'lib/fargo/search.rb', line 25 def filetype @filetype end |
#is_minimum_size ⇒ Object
Returns the value of attribute is_minimum_size.
25 26 27 |
# File 'lib/fargo/search.rb', line 25 def is_minimum_size @is_minimum_size end |
#pattern ⇒ Object
Returns the value of attribute pattern.
25 26 27 |
# File 'lib/fargo/search.rb', line 25 def pattern @pattern end |
#size ⇒ Object
Returns the value of attribute size.
25 26 27 |
# File 'lib/fargo/search.rb', line 25 def size @size end |
#size_restricted ⇒ Object
Returns the value of attribute size_restricted.
25 26 27 |
# File 'lib/fargo/search.rb', line 25 def size_restricted @size_restricted end |
Instance Method Details
#matches?(map) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/fargo/search.rb', line 52 def matches? map if map.is_a?(Listing) listing = map map = { :file => listing.name, :size => listing.size, :tth => listing.tth } end file = map[:file].try(:downcase) || '' if @filetype == TTH matches_query = (@pattern =~ /^TTH:(\w+)$/ && map[:tth] == $1) else matches_query = queries.inject(true) do |last, word| last && file.index(word.downcase) end patterns = EXTENSIONS[@filetype] if patterns && matches_query ext = File.extname file matches_query = patterns.any?{ |p| ext =~ /^\.#{p}$/i } end end if size_restricted if is_minimum_size matches_query && map[:size] > size else matches_query && map[:size] < size end else matches_query end end |
#queries ⇒ Object
44 45 46 |
# File 'lib/fargo/search.rb', line 44 def queries pattern.split('$') end |
#query ⇒ Object
48 49 50 |
# File 'lib/fargo/search.rb', line 48 def query pattern.gsub('$', ' ') end |
#query=(query) ⇒ Object
40 41 42 |
# File 'lib/fargo/search.rb', line 40 def query= query @pattern = query.split(' ').join('$') end |
#to_s ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/fargo/search.rb', line 90 def to_s if size_restricted "#{size_restricted ? 'T' : 'F' }?#{!size_restricted || is_minimum_size ? 'T' : 'F'}?#{size || 0}?#{filetype}?#{pattern}" else "F?T?#{size || 0}?#{filetype}?#{pattern}" end end |