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
140
141
142
143
144
145
146
|
# File 'lib/Dex.rb', line 140
def method_missing *args, &blok
if table.respond_to?(args.first)
table.send *args, &blok
else
super
end
end
|
Instance Attribute Details
#table ⇒ Object
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
@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_name ⇒ Object
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
|
#fields ⇒ Object
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
|
# File 'lib/Dex.rb', line 88
def insert e, other=Hash[]
if e.is_a?(Hash)
e = OpenStruct.new(e)
end
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
h = Hash[
:message => e.message || "Unknown",
:exception => e.exception.is_a?(String) ? e.exception : (e.exception ? e.exception.class.name : "Unknown"),
:backtrace => (e.backtrace && e.backtrace.join("\n")) || "",
:status => 0,
:created_at => Time.now.utc
]
safe_other = other.inject({}) { |m, (k,v)|
case v
when Array, Hash
m[k] = v.inspect
else
m[k] = v
end
m
}
table.insert h.merge(safe_other)
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
131
132
133
134
135
136
137
138
|
# File 'lib/Dex.rb', line 131
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
126
127
128
129
130
|
# File 'lib/Dex.rb', line 126
def remove_field name
db.alter_table table_name do
drop_column name
end
end
|
#table_exists? ⇒ Boolean
74
75
76
|
# File 'lib/Dex.rb', line 74
def table_exists?
db.table_exists?(table_name)
end
|
#table_name ⇒ Object
69
70
71
72
|
# File 'lib/Dex.rb', line 69
def table_name
return nil unless table
table.opts[:from].first
end
|