mongoid-colors 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,42 +2,46 @@
2
2
 
3
3
  require 'mongoid'
4
4
  require 'coderay'
5
+ require 'colorize'
5
6
 
6
7
  module MongoidColors::Colorizer
7
8
  def self.setup
8
9
  return unless Mongoid.logger
9
10
 
10
11
  old_formatter = Mongoid.logger.formatter
11
- Mongoid.logger.formatter = proc do |severity, datetime, progname, msg|
12
+ Mongoid.logger.formatter = lambda do |severity, datetime, progname, msg|
12
13
  m = parse(msg)
14
+ return if m == :ignore
15
+
13
16
  if m.nil?
14
- unless msg =~ /which could negatively impact client-side performance/
15
- old_formatter.call(severity, datetime, progname, msg)
16
- end
17
- else
18
- m[:query].gsub!(/BSON::ObjectId\('([^']+)'\)/, '0x\1')
19
- m[:duration] = m[:duration].split('.')[0] if m[:duration]
20
-
21
- line = "\033[1;32m\033[1;37mMongoDB\033[0m "
22
- line << "(#{m[:duration]}ms) " if m[:duration]
23
-
24
- if m[:database]
25
- if m[:collection]
26
- line << colorize("[#{m[:database]}::#{m[:collection]}] ")
27
- else
28
- line << colorize("[#{m[:database]}] ")
29
- end
17
+ old_formatter.call(severity, datetime, progname, msg)
18
+ return
19
+ end
20
+
21
+ m[:query].gsub!(/BSON::ObjectId\('([^']+)'\)/, '0x\1')
22
+ m[:duration] = m[:duration].split('.')[0] if m[:duration]
23
+
24
+ line = "☘ ".green + "MongoDB ".white
25
+ line << "(#{m[:duration]}ms) " if m[:duration]
26
+
27
+ if m[:database]
28
+ if m[:collection]
29
+ line << "[#{m[:database]}::#{m[:collection]}] "
30
+ else
31
+ line << "[#{m[:database]}] "
30
32
  end
33
+ end
31
34
 
32
- line << "#{colorize(m[:operation])} " if m[:operation]
33
- line << colorize(m[:query])
34
- line << "\n"
35
+ line << case m[:operation]
36
+ when 'QUERY' then 'QUERY '.colorize(:green)
37
+ when 'INSERT' then 'INSERT '.red
38
+ when 'UPDATE' then 'UPDATE '.red
39
+ else m[:operation].to_s
35
40
  end
36
- end
37
- end
38
41
 
39
- def self.colorize(msg)
40
- CodeRay.scan(msg, :ruby).term
42
+ line << CodeRay.scan(m[:query], :ruby).term
43
+ line << "\n"
44
+ end
41
45
  end
42
46
 
43
47
  def self.parse(msg)
@@ -46,10 +50,11 @@ module MongoidColors::Colorizer
46
50
  {:duration => $1, :database => $2, :collection => $3, :query => $4}
47
51
  when /^MONGODB (.*)\['(.*)'\]\.(.*)$/
48
52
  {:database => $1, :collection => $2, :query => $3}
49
- when /^ *MOPED: (\S+:\S+) (\S+) +database=(\S+) collection=(\S+) (.*) \((.*)ms\)/
50
- {:host => $1, :operation => $2, :database => $3, :collection => $4, :query => $5, :duration => $6}
51
- when /^ *MOPED: (\S+:\S+) (\S+) +database=(\S+) (.*) \((.*)ms\)/
52
- {:host => $1, :operation => $2, :database => $3, :query => $4, :duration => $5}
53
+ when /^ *MOPED: (\S+:\S+) (\S+) +database=(\S+)( collection=(\S+))? (.*[^)])( \((.*)ms\))?$/
54
+ {:host => $1, :operation => $2, :database => $3, :collection => $5, :query => $6, :duration => $8}
55
+ when /which could negatively impact client-side performance/
56
+ when /COMMAND.*getlasterror/
57
+ :ignore
53
58
  end
54
59
  end
55
60
 
@@ -1,3 +1,3 @@
1
1
  module MongoidColors
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-colors
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-22 00:00:00.000000000 Z
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: colorize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description: Colorize your Mongoid debug statments
47
63
  email:
48
64
  - nicolas@viennot.biz
@@ -50,9 +66,9 @@ executables: []
50
66
  extensions: []
51
67
  extra_rdoc_files: []
52
68
  files:
53
- - lib/mongoid-colors/version.rb
54
- - lib/mongoid-colors/colorizer.rb
55
69
  - lib/mongoid-colors.rb
70
+ - lib/mongoid-colors/colorizer.rb
71
+ - lib/mongoid-colors/version.rb
56
72
  - README.md
57
73
  homepage: http://github.com/nviennot/rspec-console
58
74
  licenses: []