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



146
147
148
149
150
151
152
# File 'lib/Dex.rb', line 146

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/Dex.rb', line 88

def insert e, other=Hash[]
  msg     = :message
  bt      = :backtrace
  ex      = :exception
  stat    = :status
  created = :created_at
  
  final = Hash[ msg => 'Unknown', ex => 'Unknown', stat => 0, created =>Time.now.utc ]
  if e.respond_to?(ex)
    final[ msg ] = e.message
    final[ bt  ] = e.backtrace
    final[ ex  ] = e.exception.class.name
  else
    final.update e
  end

  final.update other

  keys     = final.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
  
  final[ bt ] = [ final[bt] ].compact.join("\n")
  
  final.keys.each { |k|
    v = final[k]
    case v
    when Array, Hash
      final[k] = v.inspect 
    else
      final[k] = v
    end
  }
  
  table.insert final
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



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

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



132
133
134
135
136
# File 'lib/Dex.rb', line 132

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