Module: Dex::DSL

Included in:
Dex
Defined in:
lib/Dex.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &blok) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/Dex.rb', line 79

def method_missing *args, &blok
  if table.respond_to?(args.first)
    table.send *args, &blok
  else
    super
  end
end

Instance Method Details

#db(name = :_RETURN_) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Dex.rb', line 31

def db name = :_RETURN_
  if name != :_RETURN_
    @db = begin
            db_file name
            db = Sequel.sqlite db_file
            db.create_table?(Dex.default_table) {

              primary_key :id
              String :message
              String :exception
              Text :backtrace
              Integer :status
              DateTime :created_at

            }
            db
          end
    @table = nil
  end

  @db ||= db(Dex.default_db)
end

#db_file(f = :_R_) ⇒ Object



20
21
22
23
# File 'lib/Dex.rb', line 20

def db_file f = :_R_
  return @db_file if f == :_R_
  @db_file = f
end

#insert(e) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/Dex.rb', line 61

def insert e
  table.insert \
    :message   => e.message, \
    :exception => e.exception.class.name, \
    :backtrace => e.backtrace.join("\n"),
    :status    => 0,
    :created_at => Time.now.utc
end

#keep_only(n = 250) ⇒ Object



25
26
27
28
29
# File 'lib/Dex.rb', line 25

def keep_only n = 250
  c = table.count
  return false unless c > 250
  table.filter( :id=> Dex.table.select(:id).limit( c-n ) ).delete
end

#recent(n = 10) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/Dex.rb', line 70

def recent n = 10
  ds = table.reverse_order(:created_at, :id).limit(n)
  if n < 2
    ds.first
  else
    ds
  end
end

#table(name = :_RETURN_) ⇒ Object



54
55
56
57
58
59
# File 'lib/Dex.rb', line 54

def table name = :_RETURN_
  if name != :_RETURN_
    @table = db[name]
  end
  @table ||= table(:dex_exceptions)
end