Class: Fargo::Search

Inherits:
Object
  • Object
show all
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 =
{
  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

Instance Method Summary collapse

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

#filetypeObject

Returns the value of attribute filetype.



25
26
27
# File 'lib/fargo/search.rb', line 25

def filetype
  @filetype
end

#is_minimum_sizeObject

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

#patternObject

Returns the value of attribute pattern.



25
26
27
# File 'lib/fargo/search.rb', line 25

def pattern
  @pattern
end

#sizeObject

Returns the value of attribute size.



25
26
27
# File 'lib/fargo/search.rb', line 25

def size
  @size
end

#size_restrictedObject

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

Returns:

  • (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

#queriesObject



44
45
46
# File 'lib/fargo/search.rb', line 44

def queries
  pattern.split('$')
end

#queryObject



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_sObject



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