Class: Delish::Delish_Bookmark_Search

Inherits:
Object
  • Object
show all
Defined in:
lib/delish_bookmark_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(search, database, access) ⇒ Delish_Bookmark_Search


{{{ initialize(search, database) +++



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/delish_bookmark_search.rb', line 25

def initialize(search, database, access)
  @db = database

  @TAG_SPLIT = ','
  @SPLIT = ':'
  @ACCESS = access

  @search_string = search
  @sss = []

  @date_greater_or_equal = SuperSQL.new(0)
  @date_greater_or_equal.statement = 'TYPE >= JULIANDAY(CURRENT_THING) - ?'

  @date_less_than        = SuperSQL.new(0)
  @date_less_than.statement        = 'TYPE < JULIANDAY(CURRENT_THING) - ?'

  @use_tags = false
  @tag_count = 0

  self.search

end

Instance Method Details

#[](index) ⇒ Object


}}} +++



215
216
217
# File 'lib/delish_bookmark_search.rb', line 215

def [](index)
  @results[index]
end

#build_statementObject


{{{ build_statement +++



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/delish_bookmark_search.rb', line 242

def build_statement
  variables = []

  statement = "SELECT url"

  unless @ACCESS
    statement += ", description"
  else
    statement += ", posts.hash"
  end

  statement +=  " FROM posts"
  @tag_count.times { |i| statement += ", post_tags AS pt#{i}, tags AS t#{i}" }
  statement += " WHERE "
  @tag_count.times { |i| statement += "pt#{i}.hash == posts.hash AND pt#{i}.tag_id = t#{i}.id AND " }

  tag_count = 0
  @sss.each_with_index do |ssql,i|
    while ssql.statement.include? "PLACEHOLDER"
      ssql.statement.sub!("PLACEHOLDER", "t#{tag_count}.tag")
      tag_count += 1
    end
    statement += ssql.statement
    statement += ' AND ' unless i + 1 == @sss.length

    variables += ssql.variables
  end

  statement += ' ORDER BY accessed'
  return statement, variables
end

#date_search(search_type, date_type, search_inner_type, amount) ⇒ Object

This needs cleanup


{{{ date_search +++



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/delish_bookmark_search.rb', line 153

def date_search(search_type, date_type, search_inner_type, amount)

  type = ""
  how_much = ""
  date_or_time = ""
  if search_type == 'c'
    type = 'created'
  else
    type = 'accessed'
  end
  if date_type == 'h'
    how_much = Date.time_to_day_fraction(amount.to_i, 0, 0)
  elsif date_type == ''
    how_much = amount
  elsif date_type == 'w'
    how_much = amount * 7
  end
  if search_inner_type == '-'
    date_or_time = "CURRENT_TIMESTAMP"
    @date_greater_or_equal.variables << how_much
  elsif search_inner_type == '+'
    date_or_time = "CURRENT_TIMESTAMP"
    @date_less_than.variables << how_much
  else
    date_or_time = "CURRENT_DATE"
    @date_greater_or_equal.variables << how_much
    @date_less_than.variables << (how_much.to_i + 1).to_s
  end

  @date_greater_or_equal.statement["TYPE"] = type
  @date_less_than.statement["TYPE"] = type
  @date_greater_or_equal.statement["CURRENT_THING"] = date_or_time
  @date_less_than.statement["CURRENT_THING"] = date_or_time
end

#eachObject


{{{ each +++



232
233
234
# File 'lib/delish_bookmark_search.rb', line 232

def each
  @results.each { |result| yield(result) }
end

#each_with_indexObject


{{{ each_with_index +++



222
223
224
# File 'lib/delish_bookmark_search.rb', line 222

def each_with_index
  @results.each_with_index { |result| yield(result) }
end

#executeObject


{{{ execute +++



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/delish_bookmark_search.rb', line 194

def execute
  @sss << @date_greater_or_equal unless @date_greater_or_equal.variables.empty?
  @sss << @date_less_than unless @date_less_than.variables.empty?

  @sss.sort! { |a, b| a.order <=> b.order }

  statement, variables = self.build_statement

  @results = @db.execute(statement, variables)

  if @ACCESS
    hashes = []
    @results.each { |i| hashes << i[1] }
    self.mark_accessed(hashes)
  end

end

#main_search(main_search_string) ⇒ Object


{{{ main_search +++



71
72
73
74
75
76
77
78
79
# File 'lib/delish_bookmark_search.rb', line 71

def main_search(main_search_string)
  @main_search = main_search_string

  ssql = SuperSQL.new(40)
  ssql.statement = "(url LIKE ? OR description LIKE ? OR extended LIKE ?)"
  3.times { ssql.variables << "%#{main_search_string}%" }

  @sss << ssql
end

#mark_accessed(hashes) ⇒ Object


{{{ mark_accessed(hashes) +++



280
281
282
283
# File 'lib/delish_bookmark_search.rb', line 280

def mark_accessed(hashes)
  statement = "UPDATE posts SET accessed = JULIANDAY(CURRENT_TIMESTAMP) WHERE hash IN (" + hashes.map {"?"}.join(",") + ")"
  @db.execute(statement, hashes)
end

#searchObject


{{{ search +++



54
55
56
57
58
59
60
61
62
63
# File 'lib/delish_bookmark_search.rb', line 54

def search
  if @search_string =~ /^(.*?)(\((.*)\))?$/
    self.main_search($1)
    unless $2.nil?
      @submatches = $3.split(@SPLIT)
      self.sub_search_parse
    end
    self.execute
  end
end

#sub_search_parseObject


{{{ sub_search_parse +++



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/delish_bookmark_search.rb', line 87

def sub_search_parse
  @submatches.each do |i|
    if i =~ /^t(.*)$/
      self.tag_equality_search($1)
    elsif i =~ /^s(.*)$/
      self.tag_begin_search($1)
    elsif i =~ /^i(.*)$/
      self.tag_inside_search($1)
    elsif i =~ /^([ca])([Mwhms]|)([+-]|)(\d+)$/
      self.date_search($1, $2, $3, $4)
    end
  end
end

#tag_begin_search(tag_string) ⇒ Object


{{{ tag_begin_search(i) +++



122
123
124
125
126
127
128
129
# File 'lib/delish_bookmark_search.rb', line 122

def tag_begin_search(tag_string)
  @use_tags = true

  ssql = SuperSQL.new(10)
  ssql.variables = tag_string.split(@TAG_SPLIT).map { |i| "#{i}%" }
  ssql.statement = ssql.variables.map { @tag_count += 1; 'PLACEHOLDER LIKE ?' }.join(' AND ')
  @sss << ssql
end

#tag_equality_search(tag_string) ⇒ Object


{{{ tag_equality_search(tag_string +++



107
108
109
110
111
112
113
114
# File 'lib/delish_bookmark_search.rb', line 107

def tag_equality_search(tag_string)
  @use_tags = true

  ssql = SuperSQL.new(5)
  ssql.variables = tag_string.split(@TAG_SPLIT)
  ssql.statement = ssql.variables.map { @tag_count += 1; 'PLACEHOLDER = ?' }.join(' AND ')
  @sss << ssql
end

#tag_inside_search(tag_string) ⇒ Object


{{{ tag_inside_search(i) +++



137
138
139
140
141
142
143
144
# File 'lib/delish_bookmark_search.rb', line 137

def tag_inside_search(tag_string)
  @use_tags = true

  ssql = SuperSQL.new(20)
  ssql.variables = tag_string.split(@TAG_SPLIT).map { |i| "%#{i}%" }
  ssql.statement = ssql.variables.map { @tag_count += 1; 'PLACEHOLDER LIKE ?' }.join(' AND ')
  @sss << ssql
end