scribbler 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Scribbler
2
- class CLI
2
+ class CLIClient
3
3
  # Run a shell command and output clean text explaining what happened
4
4
  #
5
5
  # command - Shell command to run
@@ -20,28 +20,12 @@ module Scribbler
20
20
  # # => nothing
21
21
  #
22
22
  # Returns the backtick return of the command
23
- def self.run_command(command, poptions={})
23
+ def run_command(command, poptions={})
24
24
  options = {:output => true}.merge(poptions)
25
25
  output command if options[:output]
26
26
  `#{command}`
27
27
  end
28
28
 
29
- # Central method for outputting text. Will serve
30
- # as a central location for changing how Scribbler outputs
31
- #
32
- # text - Text to output
33
- #
34
- # Examples:
35
- #
36
- # CLI.say "Output stuff"
37
- # # "Output stuff"
38
- # # => "Output stuff"
39
- #
40
- # Returns whatever `puts` command returns
41
- def self.say(text)
42
- puts text
43
- end
44
-
45
29
  # Copy a list of files to one location with one output
46
30
  # for the whole copy
47
31
  #
@@ -55,7 +39,7 @@ module Scribbler
55
39
  # # => Nothing
56
40
  #
57
41
  # Returns Nothing
58
- def self.mass_copy(files, destination)
42
+ def mass_copy(files, destination)
59
43
  output 'cp'
60
44
  files.each do |file|
61
45
  run_command "cp #{file} #{destination}", :output => false
@@ -64,6 +48,22 @@ module Scribbler
64
48
 
65
49
  private
66
50
 
51
+ # Central method for outputting text. Will serve
52
+ # as a central location for changing how Scribbler outputs
53
+ #
54
+ # text - Text to output
55
+ #
56
+ # Examples:
57
+ #
58
+ # CLI.say "Output stuff"
59
+ # # "Output stuff"
60
+ # # => "Output stuff"
61
+ #
62
+ # Returns whatever `puts` command returns
63
+ def say(text)
64
+ puts text
65
+ end
66
+
67
67
  # Get the command and try to output a human description
68
68
  # of what's happening
69
69
  #
@@ -80,16 +80,16 @@ module Scribbler
80
80
  # # => "Checking necessary directories are in place"
81
81
  #
82
82
  # Returns Nothing
83
- def self.output(command)
84
- final_out = []
85
- pieces = command.split(' ')
86
- case pieces.first
87
- when 'mkdir'
88
- final_out << "Checking necessary directories are in place"
89
- when 'cp'
90
- final_out << "Coping files"
91
- end
92
- say final_out.join ' '
83
+ def output(command)
84
+ base_command = command.split(' ').first
85
+ say out_definitions[base_command]
86
+ end
87
+
88
+ def out_definitions
89
+ Hash.new { |h,k| h[k.to_s] ||= "Running command: #{k}" }.tap { |hash|
90
+ hash["cp"] = "Coping files"
91
+ hash["mkdir"] = "Checking necessary directories are in place"
92
+ }
93
93
  end
94
94
  end
95
95
  end
@@ -1,7 +1,55 @@
1
1
  module Scribbler
2
2
  class Configurator
3
- class << self
4
- attr_accessor :logs, :application_include, :template, :use_template_by_default, :log_directory
3
+ attr_accessor :logs, :application_include, :template, :use_template_by_default, :log_directory
4
+
5
+ # USED FOR CONFIGURE BLOCK DEPRECATION
6
+ def config
7
+ self
8
+ end
9
+ private :config
10
+
11
+ # Gets the path of this Gem
12
+ #
13
+ # Examples:
14
+ #
15
+ # Configurator.gem_path
16
+ # # => '/some/home/.rvm/gems/ruby-1.9.3-p125/gems/scribbler-0.0.1/'
17
+ #
18
+ # Returns String of the current gem's directory path
19
+ def gem_path
20
+ File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
21
+ end
22
+
23
+ # Gets all the paths to the template files in the gem's template directory
24
+ #
25
+ # Examples:
26
+ #
27
+ # Configurator.templates
28
+ # # => ['/some/home/.rvm/gems/ruby-1.9.3-p125/gems/scribbler-0.0.1/templates/1.rb',
29
+ # # '/some/home/.rvm/gems/ruby-1.9.3-p125/gems/scribbler-0.0.1/templates/2.rb]
30
+ #
31
+ # Returns Array of Strings of the gem's template files
32
+ def templates
33
+ Dir.glob(File.join(gem_path, 'templates', '*'))
34
+ end
35
+
36
+ # Gets the path to the default install directory. If Rails is present
37
+ # it will default to the Rails.root/config/initializers/. Otherwise
38
+ # it assumes its the $PWD/config/initializer. Should look at a cleaner
39
+ # abstraction of this
40
+ #
41
+ # Examples:
42
+ #
43
+ # Configurator.default_install_path
44
+ # # => '/some/home/projects/rails_app/config/initializers/'
45
+ #
46
+ # Returns String for best guess of a good install path
47
+ def default_install_path
48
+ begin
49
+ ::Rails.root.join 'config', 'initializers', ''
50
+ rescue NameError
51
+ File.join Dir.pwd, 'config', 'initializers', ''
52
+ end
5
53
  end
6
54
 
7
55
  # Provides location for getting the directory Scribbler will place
@@ -14,10 +62,10 @@ module Scribbler
14
62
  # # => "/some/path/to/log/"
15
63
  #
16
64
  # Returns String for log directory location
17
- def self.log_directory
65
+ def log_directory
18
66
  @log_directory ||= begin
19
67
  Rails.root.join('log')
20
- rescue NameError
68
+ rescue => e
21
69
  File.join Dir.pwd, 'log'
22
70
  end
23
71
  end
@@ -36,12 +84,12 @@ module Scribbler
36
84
  # Returns boolean
37
85
  #
38
86
  # TODO: Allow the class we're sending the include to to be custom
39
- def self.application_include
87
+ def application_include
40
88
  @application_include || false
41
89
  end
42
90
 
43
91
  # Boolean for deciding if we should use the logger template by
44
- # by default when calling Base.log
92
+ # by default when calling Scribbler.log
45
93
  #
46
94
  # Default: false
47
95
  #
@@ -51,15 +99,15 @@ module Scribbler
51
99
  # # => false
52
100
  #
53
101
  # Returns boolean
54
- def self.use_template_by_default
102
+ def use_template_by_default
55
103
  @use_template_by_default || false
56
104
  end
57
105
 
58
106
  # The method that sets a template for each log made with
59
- # Base.log
107
+ # Scribbler.log
60
108
  #
61
109
  # The template proc is given the whole options hash that is
62
- # passed through Base.log or YourApplication.log. So if you
110
+ # passed through Scribbler.log or YourApplication.log. So if you
63
111
  # had:
64
112
  #
65
113
  # YourApplication.log :a_log,
@@ -79,16 +127,16 @@ module Scribbler
79
127
  # "Message: options[:message]"
80
128
  # end
81
129
  #
82
- # From Scribbler::Base.configure that would be:
130
+ # From Scribbler.configure that would be:
83
131
  #
84
132
  # config.template = proc do |options|
85
133
  # "Message: options[:message]"
86
134
  # end
87
135
  #
88
136
  # **Keep in mind** that the template can be ignored at any
89
- # Base.log call with:
137
+ # Scribbler.log call with:
90
138
  #
91
- # Base.log :your_log, :template => false, :message "..."
139
+ # Scribbler.log :your_log, :template => false, :message "..."
92
140
  #
93
141
  # Default:
94
142
  #
@@ -108,7 +156,7 @@ module Scribbler
108
156
  #
109
157
  # TODO: Block input that would break this
110
158
  # TODO: Test
111
- def self.template
159
+ def template
112
160
  @template ||= proc do |options|
113
161
  begin
114
162
  if_id = options[:object].present? ? options[:object].try(:id) : 'no id'
@@ -5,10 +5,18 @@ module Scribbler
5
5
  # options - Options from command in shell
6
6
  # :path - changes the path its installing config files too
7
7
  #
8
- def self.install(options={})
9
- install_path = options[:path] || Base.default_install_path
10
- CLI.run_command "mkdir -p #{install_path}"
11
- CLI.mass_copy Base.templates, install_path
8
+ def install(options={})
9
+ install_path = options[:path] || config.default_install_path
10
+ cli.run_command "mkdir -p #{install_path}"
11
+ cli.mass_copy config.templates, install_path
12
+ end
13
+
14
+ def config
15
+ Scribbler.config
16
+ end
17
+
18
+ def cli
19
+ CLIClient.new
12
20
  end
13
21
  end
14
22
  end
@@ -1,23 +1,20 @@
1
1
  module Scribbler
2
- class BaseIncluder # I don't love this
3
- # Wonky way of allowing Base to include the Includeables.
4
- # Receives require errors with this currently.
5
- #
6
- # Examples:
7
- #
8
- # BaseIncluder.include_includeables
9
- # # => Nothing
10
- #
11
- # Returns Nothing
12
- # TODO Rework; there must be a more sane way of including these
13
- def self.include_includeables
14
- Base.send :include, Scribbler::Includeables
15
- end
16
- end
17
-
18
2
  module Includeables
19
3
  extend ActiveSupport::Concern
4
+
20
5
  module ClassMethods
6
+ def logger
7
+ Logger
8
+ end
9
+
10
+ def log(*args)
11
+ logger.log *args
12
+ end
13
+
14
+ def log_at(location)
15
+ LogLocation.new.find_path location
16
+ end
17
+
21
18
  def log_location_regex
22
19
  /(?<file>.*)_log_location$/
23
20
  end
@@ -40,33 +37,6 @@ module Scribbler
40
37
  def respond_to?(name)
41
38
  (m = name.to_s.match log_location_regex) ? !!m : super
42
39
  end
43
-
44
- def log_at(file_name)
45
- File.join Scribbler::Configurator.log_directory, "#{file_name}.log"
46
- end
47
-
48
- # Public: Save ourselves some repetition. Notifies error to NewRelic
49
- # and drops given string into a given log.
50
- #
51
- # location - Either a pathname from the above method or symbol for an above
52
- # method
53
- # options - Hash of options for logging on Ngin
54
- # :error - Error object, mostly for passing to NewRelic
55
- # :message - Message to log in the actual file
56
- # :new_relic - Notify NewRelic of the error (default: true)
57
- #
58
- # Examples
59
- #
60
- # log(Ngin.subseason_log_location, :error => e, :message => "Error message stuff", :new_relic => false)
61
- #
62
- # log(:subseason, :error => e, :message => "Error message stuff")
63
- #
64
- # log(:subseason, :message => "Logging like a bauss")
65
- #
66
- # Returns Nothing.
67
- def log(location, options={})
68
- Scribbler::Base.log(location, options)
69
- end
70
40
  end
71
41
  end
72
42
  end
@@ -0,0 +1,13 @@
1
+ require 'pathname'
2
+ module Scribbler
3
+ class LogLocation
4
+ def find_path(file_name)
5
+ Pathname.new File.join(config.log_directory, "#{file_name}.log")
6
+ end
7
+
8
+ private
9
+ def config
10
+ Scribbler.config
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,152 @@
1
+ module Scribbler
2
+ class Logger
3
+ private
4
+ attr_accessor :location
5
+ attr_accessor :options
6
+
7
+ public
8
+
9
+ def self.log(location, options = {})
10
+ new(location, options).log
11
+ end
12
+
13
+ def initialize(location, options = {})
14
+ self.location = location
15
+ self.options = gather_log_options options
16
+ end
17
+
18
+ # Public: Save ourselves some repetition. Notifies error to NewRelic
19
+ # and drops given string into a given log.
20
+ #
21
+ # location - Either a pathname from the above method or symbol for an above
22
+ # method
23
+ # options - Hash of options for logging on Ngin
24
+ # :error - Error object, mostly for passing to NewRelic
25
+ # :message - Message to log in the actual file
26
+ # :custom_fields - Custom fields dropped into the default template
27
+ # :template - Whether or not to use the template at this log
28
+ # :new_relic - Notify NewRelic of the error (default: true)
29
+ #
30
+ # Examples
31
+ #
32
+ # log(Ngin.subseason_log_location, :error => e, :message => "Error message stuff", :new_relic => false)
33
+ #
34
+ # log(:subseason, :error => e, :message => "Error message stuff")
35
+ #
36
+ # log(:subseason, :message => "Logging like a bauss")
37
+ #
38
+ # Returns Nothing.
39
+ def log
40
+ notify_new_relic options[:error], options[:new_relic]
41
+ apply_to_log
42
+ end
43
+
44
+ private
45
+
46
+ def actual_log_location
47
+ LogLocation.new.find_path location
48
+ end
49
+
50
+ def notify_new_relic(error, new_relic)
51
+ NewRelic::Agent.notice_error(error) if error and new_relic != false
52
+ rescue NameError
53
+ nil
54
+ end
55
+
56
+ def gather_log_options(given_options)
57
+ {
58
+ :template => config.use_template_by_default,
59
+ :stack_frame => given_options[:call_stack] ? Kernel.caller[1..-1] : nil
60
+ }.merge given_options
61
+ end
62
+
63
+ def config
64
+ Scribbler.config
65
+ end
66
+
67
+ # Builds the message and any other options into a string
68
+ # using the template defined in the configure.
69
+ #
70
+ # NOTE: These "options" come from the instance
71
+ #
72
+ # options - options hash that comprises most of the important log pieces
73
+ # :message - The message we're wrapping into the templater [required]
74
+ # :template - Whether or not to use the template method
75
+ # **Other option information given in the .log docs
76
+ #
77
+ # Examples
78
+ #
79
+ # build_with_template(:message => "...", :template => false)
80
+ # # => "..."
81
+ #
82
+ # build_with_template
83
+ # # => nil
84
+ #
85
+ # build_with_template(:message => "...", :template => true)
86
+ # # => <<-EXAMPLE
87
+ # --------------------
88
+ # TEMPLATE STUFF
89
+ # ....
90
+ # EXAMPLE
91
+ #
92
+ # Returns nil, a string, or a string built with calling the Configurator.template method
93
+ def build_with_template
94
+ options[:message] = options[:message].to_s.strip_heredoc.rstrip
95
+ template.call options
96
+ end
97
+
98
+ def no_template
99
+ ->(o) { o[:message] }
100
+ end
101
+
102
+ def decypher_template_option
103
+ if options[:template]
104
+ options[:template].is_a?(Proc) ? options[:template] : default_template
105
+ else
106
+ no_template
107
+ end
108
+ end
109
+
110
+ def template_option_given?
111
+ options.key?(:template)
112
+ end
113
+
114
+ def default_template
115
+ config.template
116
+ end
117
+
118
+ def template
119
+ template_option_given? ? decypher_template_option : default_template
120
+ end
121
+
122
+ # Drops built message into the log with the given location
123
+ #
124
+ # location - location either found with Scribbler.*_log_location or by hoping a valid
125
+ # path string or Path object were passed
126
+ # options - options hash
127
+ # :message - Message to be built and put in log file [required]
128
+ # ** Other hash information given in Scribbler.log
129
+ #
130
+ # Examples
131
+ #
132
+ # apply_to_log # WITH :some_loc, :message => "...", :template => false, :error => e
133
+ # # => Nothing
134
+ #
135
+ # Returns Nothing
136
+ def apply_to_log
137
+ if can_apply_to_log?
138
+ File.open actual_log_location, 'a' do |f|
139
+ f.puts build_with_template
140
+ end
141
+ end
142
+ end
143
+
144
+ # TODO: Fix to work with any template
145
+ def can_apply_to_log?
146
+ location.present? and
147
+ (options[:message].present? or
148
+ options[:object].present? or
149
+ options[:custom_fields].present?)
150
+ end
151
+ end
152
+ end