Skip to content

Commit

Permalink
Application should be a module
Browse files Browse the repository at this point in the history
It has only class methods.

Application.camel_case is expected to be private, but it was not.
  • Loading branch information
tomykaira committed Sep 26, 2012
1 parent 43ab31f commit 0250bbf
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/goliath/application.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Goliath
# handler to run the server. # handler to run the server.
# #
# @private # @private
class Application module Application
# Most of this stuff is straight out of sinatra. # Most of this stuff is straight out of sinatra.


# Set of caller regex's to be skipped when looking for our API file # Set of caller regex's to be skipped when looking for our API file
Expand All @@ -22,15 +22,17 @@ class Application
# @todo add rubinius (and hopefully other VM impls) ignore patterns ... # @todo add rubinius (and hopefully other VM impls) ignore patterns ...
CALLERS_TO_IGNORE.concat(RUBY_IGNORE_CALLERS) if defined?(RUBY_IGNORE_CALLERS) CALLERS_TO_IGNORE.concat(RUBY_IGNORE_CALLERS) if defined?(RUBY_IGNORE_CALLERS)


module_function

# Like Kernel#caller but excluding certain magic entries and without # Like Kernel#caller but excluding certain magic entries and without
# line / method information; the resulting array contains filenames only. # line / method information; the resulting array contains filenames only.
def self.caller_files def caller_files
caller_locations.map { |file, line| file } caller_locations.map { |file, line| file }
end end


# Like caller_files, but containing Arrays rather than strings with the # Like caller_files, but containing Arrays rather than strings with the
# first element being the file, and the second being the line. # first element being the file, and the second being the line.
def self.caller_locations def caller_locations
caller(1). caller(1).
map { |line| line.split(/:(?=\d|in )/)[0,2] }. map { |line| line.split(/:(?=\d|in )/)[0,2] }.
reject { |file, line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } } reject { |file, line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
Expand All @@ -39,7 +41,7 @@ def self.caller_locations
# Find the app_file that was used to execute the application # Find the app_file that was used to execute the application
# #
# @return [String] The app file # @return [String] The app file
def self.app_file def app_file
c = caller_files.first c = caller_files.first
c = $0 if !c || c.empty? c = $0 if !c || c.empty?
c c
Expand All @@ -48,15 +50,15 @@ def self.app_file
# Returns the userland class which inherits the Goliath API # Returns the userland class which inherits the Goliath API
# #
# @return [String] The app class # @return [String] The app class
def self.app_class def app_class
@app_class @app_class
end end


# Sets the userland class that should use the Goliath API # Sets the userland class that should use the Goliath API
# #
# @param app_class [String|Symbol|Constant] The new app class # @param app_class [String|Symbol|Constant] The new app class
# @return [String] app_class The new app class # @return [String] app_class The new app class
def self.app_class=(app_class) def app_class=(app_class)
@app_class = app_class.to_s @app_class = app_class.to_s
end end


Expand All @@ -68,7 +70,7 @@ def self.app_class=(app_class)
# #
# @param args [Array] Any arguments to append to the path # @param args [Array] Any arguments to append to the path
# @return [String] path for the given arguments # @return [String] path for the given arguments
def self.app_path(*args) def app_path(*args)
@app_path ||= File.expand_path(File.dirname(app_file)) @app_path ||= File.expand_path(File.dirname(app_file))
File.join(@app_path, *args) File.join(@app_path, *args)
end end
Expand All @@ -77,7 +79,7 @@ def self.app_path(*args)
# #
# @param args [Array] Any arguments to append to the path # @param args [Array] Any arguments to append to the path
# @return [String] path for the given arguments # @return [String] path for the given arguments
def self.root_path(*args) def root_path(*args)
return app_path(args) if Goliath.env?(:test) return app_path(args) if Goliath.env?(:test)


@root_path ||= File.expand_path("./") @root_path ||= File.expand_path("./")
Expand All @@ -87,7 +89,7 @@ def self.root_path(*args)
# Execute the application # Execute the application
# #
# @return [Nil] # @return [Nil]
def self.run! def run!
unless @app_class unless @app_class
file = File.basename(app_file, '.rb') file = File.basename(app_file, '.rb')
@app_class = camel_case(file) @app_class = camel_case(file)
Expand Down Expand Up @@ -115,7 +117,7 @@ def self.run!
# #
# @param str [String] The string to convert # @param str [String] The string to convert
# @return [String] The camel cased string # @return [String] The camel cased string
def self.camel_case(str) def camel_case(str)
return str if str !~ /_/ && str =~ /[A-Z]+.*/ return str if str !~ /_/ && str =~ /[A-Z]+.*/


str.split('_').map { |e| e.capitalize }.join str.split('_').map { |e| e.capitalize }.join
Expand Down

0 comments on commit 0250bbf

Please sign in to comment.