Class: Skygrepe::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, config) ⇒ Context

Returns a new instance of Context.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# File 'lib/skygrepe.rb', line 12

def initialize(keyword, config)
  raise ArgumentError, "keyword is empty" if keyword.nil? || keyword.empty?
  @keyword = keyword
  @config = config
  @condition = Condition.new(@keyword)
  @offset = 0
  @limit = 30
  @quit = false
  @current_id = nil
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



10
11
12
# File 'lib/skygrepe.rb', line 10

def count
  @count
end

#current_idObject (readonly)

Returns the value of attribute current_id.



10
11
12
# File 'lib/skygrepe.rb', line 10

def current_id
  @current_id
end

#limitObject (readonly)

Returns the value of attribute limit.



10
11
12
# File 'lib/skygrepe.rb', line 10

def limit
  @limit
end

Instance Method Details

#dbObject



27
28
29
# File 'lib/skygrepe.rb', line 27

def db
  @db ||= SQLite3::Database.new(@config["main_db_path"])
end

#detail(id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/skygrepe.rb', line 56

def detail(id)
  sql = "SELECT m.id, m.timestamp, c.displayname, m.author, m.body_xml FROM Messages as m inner join Conversations as c on m.convo_id = c.id"
  sql << " WHERE m.id = #{id}"
  if d = db.execute(sql).first
    @current_id = d.first.to_i
    formatter.detail(d)
  else
    nil
  end
end

#formatterObject



31
32
33
# File 'lib/skygrepe.rb', line 31

def formatter
  @formatter ||= Formatter.new(@keyword, {"time_format" => @config["time_format"]})
end

#next_detail(d = 1) ⇒ Object



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

def next_detail(d = 1)
  detail(@current_id + d)
end

#next_page(page = 1) ⇒ Object



48
49
50
# File 'lib/skygrepe.rb', line 48

def next_page(page = 1)
  @offset += (@limit * page)
end

#prev_detail(d = 1) ⇒ Object



71
72
73
# File 'lib/skygrepe.rb', line 71

def prev_detail(d = 1)
  self.next_detail( -1 * d)
end

#prev_page(page = 1) ⇒ Object



52
53
54
# File 'lib/skygrepe.rb', line 52

def prev_page(page = 1)
  self.next_page( -1 * page)
end

#quitObject



75
76
77
# File 'lib/skygrepe.rb', line 75

def quit
  @quit = true
end

#quit?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/skygrepe.rb', line 23

def quit?
  @quit
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/skygrepe.rb', line 35

def run
  @count ||= db.execute(@condition.count_sql).flatten.first.to_i
  sql = @condition.grep_sql(@limit, @offset)
  rows = db.execute(sql).map{|row| formatter.list(row) }
  unless rows.empty?
    @current_id = rows.first.first.to_i
  end
  if @count <= @limit
    @quit = true
  end
  rows
end