Module: Subload
- Included in:
- Object
- Defined in:
- lib/subload.rb
Overview
TODO convert to yardoc
Constant Summary collapse
- VERSION =
'1.1.0'- MODES =
To add modes to subload, simply add them to this hash. Please use the a namespace convention, starting with
:projectname_operationdescription. { :autoload => lambda { |o,s,p,ops| o.__send__ :autoload, s, p }, :require => lambda { |o,s,p,ops| o.__send__ :require, p }, :load => lambda { |o,s,p,ops| o.__send__ :load, p } }
- LONG_UPPER_CONSTS =
[/([A-Z\d]+)([A-Z][a-z]+)/, '\1_\2']
- TAIL_UPPER_CONSTS =
[/([a-z])([A-Z])/, '\1_\2']
- DOUBLE_UNDERSCORE =
'__'- DOUBLE_COLON =
'::'- SLASH =
'/'
Class Attribute Summary collapse
-
.default_mode ⇒ Object
Subload defaults to autoload, which is appropriate and performant for single threaded applications and libraries.
-
.override_mode ⇒ Object
Subload defaults to autoload, which is appropriate and performant for single threaded applications and libraries.
Class Method Summary collapse
-
.to_path(s) ⇒ Object
Subloads snake_case method, this is very similar to that of facets, rails, and merb, but potentially has minor differences.
Instance Method Summary collapse
-
#subload(symbol, options = {}) ⇒ Object
Load the given constant name from the path corresponding to the conventional mapping of the class name underscored.
-
#subload_with(mode = nil) ⇒ Object
Sets the subload mode locally for the current class.
Class Attribute Details
.default_mode ⇒ Object
Subload defaults to autoload, which is appropriate and performant for single threaded applications and libraries. Setting the default mode will affect all future subloads that do not specify another mode explicitly, proided Subload.override_mode has not been set.
19 20 21 |
# File 'lib/subload.rb', line 19 def default_mode @default_mode end |
.override_mode ⇒ Object
Subload defaults to autoload, which is appropriate and performant for single threaded applications and libraries. Sometimes users may want to switch to eager loading or some other loading mechanism forcefully. Override mode should not be set in normal library code, but should be reserved for application code only. It may be acceptable for use in appropriate locations in frameworks.
26 27 28 |
# File 'lib/subload.rb', line 26 def override_mode @override_mode end |
Class Method Details
.to_path(s) ⇒ Object
Subloads snake_case method, this is very similar to that of facets, rails, and merb, but potentially has minor differences.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/subload.rb', line 75 def self.to_path(s) # similar to facets/string/pathize, only supports TCPServer, etc. s = s.to_s.dup # We're modifying, lets be careful s.gsub!(*LONG_UPPER_CONSTS) s.gsub!(*TAIL_UPPER_CONSTS) s.gsub!(DOUBLE_UNDERSCORE, SLASH) s.gsub!(DOUBLE_COLON, SLASH) s.downcase! s end |
Instance Method Details
#subload(symbol, options = {}) ⇒ Object
Load the given constant name from the path corresponding to the conventional mapping of the class name underscored. Example:
class A
subload :B # performs A.autoload(:B, 'a/b')
end
You can overload various operation styles in subload using the options hash:
-
:path - explicitly alter the path that will be loaded, this is passed on unmodified.
-
:mode - explicitly override the loading mode. N.B. Subload.override_mode takes precidence over this.
-
:expand_path - when set to true, the path will be expanded before being passed on to the loading mechanism.
For custom loading mechanisms, further options are passed on, as such, you may find other behaviors if subload is being used inside a framework.
57 58 59 60 61 62 63 64 65 |
# File 'lib/subload.rb', line 57 def subload(symbol, = {}) sub_path, mode = *.values_at(:path, :mode) klass = self.instance_of?(Class) || self.instance_of?(Module) klass = klass ? self.__name__ : self.class.__name__ path = File.join(sub_path || Subload.to_path("#{klass}::#{symbol}")) path = File.(path) if [:expand_path] $stdout.puts [:subload, symbol, path, mode].inspect if $DEBUG subload_with(mode)[self, symbol, path, ] end |
#subload_with(mode = nil) ⇒ Object
Sets the subload mode locally for the current class. This is how one would use a custom loading mode
31 32 33 34 35 36 37 38 |
# File 'lib/subload.rb', line 31 def subload_with(mode = nil) @_subload_mode = mode unless mode.nil? MODES[ Subload.override_mode || (defined?(@_subload_mode) && @_subload_mode) || Subload.default_mode ] end |