Skip to content

Commit

Permalink
The :relative option provides the relative directory. [minor]
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Dec 18, 2012
1 parent 451d06d commit e47700c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,29 @@ Using RubyGems type on a command line:
To use the loaded callback simply override `Kernel.loaded` method.

```ruby
def Kernel.loaded(feature, options={})
if options[:require]
if rel = options[:relative]
puts "#{feature} has been required relative to #{rel}!"
else
puts "#{feature} has been required!"
end
require 'loaded'
require 'loaded/require_relative'

def Kernel.loaded(feature, options={})
if options[:require]
if rel = options[:relative]
puts "#{feature} has been required relative to #{rel}!"
else
puts "#{feature} has been required!"
end
else
if wrap = options[:wrap]
puts "#{feature} has been loaded with wrap, it's #{wrap}!"
else
if wrap = options[:wrap]
puts "#{feature} has been loaded with wrap, it's #{wrap}!"
else
puts "#{feature} has been loaded!"
end
puts "#{feature} has been loaded!"
end
end
end
```

Unfortunately `#require_relative` doesn't work with the callback at this time due to
an implementation difficulty.
Becuase of implementation details, `#require_relative` has to be reimplemented completely
to make the callback work. To be on the safe side, at least for now, it therefore has to
be required separately, as the example above demonstrates.


## Feedback
Expand Down
2 changes: 2 additions & 0 deletions lib/loaded/require_relative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def require_relative(feature, options=nil)
if /\A\((.*)\)/ =~ file # eval, etc.
raise LoadError, "require_relative is called in #{$1}"
end
options[:relative] = File.dirname(file)
absolute = File.expand_path(feature, File.dirname(file))
require_without_callback absolute
)
Expand Down Expand Up @@ -61,6 +62,7 @@ def require_relative(feature, options=nil)
if /\A\((.*)\)/ =~ file # eval, etc.
raise LoadError, "require_relative is called in #{$1}"
end
options[:relative] = File.dirname(file)
absolute = File.expand_path(feature, File.dirname(file))
require_without_callback absolute
)
Expand Down
2 changes: 2 additions & 0 deletions test/test_require_relative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_require_relative
assert $load_opts[:relative]
refute $load_opts[:load]
refute $load_opts[:wrap]

assert_equal File.dirname(__FILE__), $load_opts[:relative]
end

end
Expand Down

0 comments on commit e47700c

Please sign in to comment.