Module: Dex::DSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &blok) ⇒ Object



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

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

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



18
19
20
# File 'lib/Dex.rb', line 18

def table
  @table
end

Instance Method Details

#db(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/Dex.rb', line 24

def db *args
  return @db if args.empty? && instance_variable_defined?(:@db)
    
  case args.size
    
  when 0
    name  = default :db_name
    table = default :table_name
    
  when 1
    name  = args[0]
    table = default :table_name

  when 2
    name  = args[0]
    table = args[1]
    
  else
    args.shift
    args.shift
    raise ArgumentError, "Unknown arguments: #{args.inspect}"
    
  end # === case
  
  @db    = Sequel.sqlite(name)
  @table = @db[table.to_sym]
  
  @db.create_table?(table_name) {

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

  }

  @db
end

#db_nameObject

def db



65
66
67
# File 'lib/Dex.rb', line 65

def db_name
  db.opts[:database] || default(:db_name)
end

#default(*args) ⇒ Object



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

def default *args
  Dex.default(*args)
end

#fieldsObject



78
79
80
# File 'lib/Dex.rb', line 78

def fields
  db.schema(table_name).map(&:first)
end

#insert(e, other = Hash[]) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/Dex.rb', line 88

def insert e, other=Hash[]
  unless other.keys.empty?
    keys=other.keys.map(&:to_sym)
    new_keys = keys - fields
    unless new_keys.empty?
      db.alter_table table_name do
        new_keys.each { |k|
          add_column k, :string
        }
      end
    end
  end
  
  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



82
83
84
85
86
# File 'lib/Dex.rb', line 82

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



114
115
116
117
118
119
120
121
# File 'lib/Dex.rb', line 114

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

#remove_field(name) ⇒ Object



109
110
111
112
113
# File 'lib/Dex.rb', line 109

def remove_field name
  db.alter_table table_name do
    drop_column name
  end
end

#table_exists?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/Dex.rb', line 74

def table_exists?
  db.table_exists?(table_name)
end

#table_nameObject



69
70
71
72
# File 'lib/Dex.rb', line 69

def table_name
  return nil unless table
  table.opts[:from].first
end