Dex 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/Dex.rb +32 -27
  2. data/lib/Dex/version.rb +1 -1
  3. data/spec/Dex.rb +16 -0
  4. metadata +1 -1
data/lib/Dex.rb CHANGED
@@ -86,41 +86,46 @@ class Dex
86
86
  end
87
87
 
88
88
  def insert e, other=Hash[]
89
- if e.is_a?(Hash)
90
- e = OpenStruct.new(e)
89
+ msg = :message
90
+ bt = :backtrace
91
+ ex = :exception
92
+ stat = :status
93
+
94
+ final = Hash[ msg => 'Unknown', ex => 'Unknown', stat => 0, :created_at=>Time.now.utc ]
95
+ if e.respond_to?(:exception)
96
+ final[ msg ] = e.message
97
+ final[ bt ] = e.backtrace
98
+ final[ ex ] = e.exception.class.name
99
+ else
100
+ final.update e
91
101
  end
92
102
 
93
- unless other.keys.empty?
94
- keys=other.keys.map(&:to_sym)
95
- new_keys = keys - fields
96
- unless new_keys.empty?
97
- db.alter_table table_name do
98
- new_keys.each { |k|
99
- add_column k, :string
100
- }
101
- end
103
+ final.update other
104
+
105
+ keys = final.keys.map(&:to_sym)
106
+ new_keys = keys - fields
107
+
108
+ unless new_keys.empty?
109
+ db.alter_table table_name do
110
+ new_keys.each { |k|
111
+ add_column k, :string
112
+ }
102
113
  end
103
114
  end
104
115
 
105
- h = Hash[
106
- :message => e.message || "Unknown",
107
- :exception => e.exception.is_a?(String) ? e.exception : (e.exception ? e.exception.class.name : "Unknown"),
108
- :backtrace => (e.backtrace && e.backtrace.join("\n")) || "",
109
- :status => 0,
110
- :created_at => Time.now.utc
111
- ]
116
+ final[ bt ] = (final[bt] || []).join("\n")
112
117
 
113
- safe_other = other.inject({}) { |m, (k,v)|
114
- case v
115
- when Array, Hash
116
- m[k] = v.inspect
117
- else
118
- m[k] = v
119
- end
120
- m
118
+ final.keys.each { |k|
119
+ v = final[k]
120
+ case v
121
+ when Array, Hash
122
+ final[k] = v.inspect
123
+ else
124
+ final[k] = v
125
+ end
121
126
  }
122
127
 
123
- table.insert h.merge(safe_other)
128
+ table.insert final
124
129
  end
125
130
 
126
131
  def remove_field name
@@ -1,3 +1,3 @@
1
1
  class Dex
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -139,6 +139,22 @@ describe "Dex :insert" do
139
139
  }
140
140
  end
141
141
 
142
+ it "accepts a single Hash with an overriding :created_at" do
143
+ dex = new_dex "custom hash"
144
+ rollback(dex) {
145
+ dex.insert :exception=>"Nginx Error", :message=>"upstream not found", :backtrace=>["file:12:txt"], :created_at=>Time.parse("2001/01/01 01:01:01")
146
+ last(dex)[:created_at].to_s.should == Time.parse("2001/01/01 01:01:01").to_s
147
+ }
148
+ end
149
+
150
+ it "accepts a single Hash with an overriding :status" do
151
+ dex = new_dex "custom hash"
152
+ rollback(dex) {
153
+ dex.insert :exception=>"Nginx Error", :message=>"upstream not found", :backtrace=>["file:12:txt"], :status=>1
154
+ last(dex)[:status].should == 1
155
+ }
156
+ end
157
+
142
158
  end # === Dex :insert
143
159
 
144
160
  describe "Dex :keep_only" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Dex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: