Module: LazyLoad::Mixin
- Included in:
- LazyLoad
- Defined in:
- lib/lazy_load.rb
Defined Under Namespace
Classes: Wrapper
Class Method Summary collapse
Instance Method Summary collapse
-
#best(*names) ⇒ Object
(also: #first_available, #[])
Return the first available dependency from the list of constant names.
- #const(name, action = nil, &blk) ⇒ Object (also: #dep)
- #const_missing(name) ⇒ Object
- #group(name, *constants) ⇒ Object
- #reset! ⇒ Object
- #scope(&blk) ⇒ Object
- #unmap(name) ⇒ Object
- #wrap(name, &blk) ⇒ Object
Class Method Details
.extended(obj) ⇒ Object
17 18 19 |
# File 'lib/lazy_load.rb', line 17 def self.extended(obj) obj.reset! end |
Instance Method Details
#best(*names) ⇒ Object Also known as: first_available, []
Return the first available dependency from the list of constant names.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lazy_load.rb', line 62 def best(*names) names.map do |name| @groups[name] || name end.flatten.each do |name| begin return const_get(name) rescue NameError, LoadError; end end const_get(names.first) end |
#const(name, action = nil, &blk) ⇒ Object Also known as: dep
27 28 29 30 |
# File 'lib/lazy_load.rb', line 27 def const(name, action=nil, &blk) @actions[name] = blk || action || raise( ArgumentError, "missing require path or callback") end |
#const_missing(name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lazy_load.rb', line 41 def const_missing(name) k = case action = @actions[name] when Proc then action.call when nil then super when String begin require @actions[name] rescue LoadError => boom raise(LoadError, boom. + "\nPossible fix: gem install #{@actions[name]}") end Kernel.const_get(name) else raise "Invalid action for dependency #{action.inspect}" end k = @wrappers[name].new(k) if @wrappers[name] const_set(name, k) end |
#group(name, *constants) ⇒ Object
33 34 35 |
# File 'lib/lazy_load.rb', line 33 def group(name, *constants) @groups[name] = constants end |
#reset! ⇒ Object
10 11 12 13 14 15 |
# File 'lib/lazy_load.rb', line 10 def reset! @actions = {} @groups = {} @wrappers = {} self end |
#scope(&blk) ⇒ Object
21 22 23 24 25 |
# File 'lib/lazy_load.rb', line 21 def scope(&blk) mod = Module.new.extend(LazyLoad::Mixin).reset! mod.module_eval(&blk) if blk mod end |
#unmap(name) ⇒ Object
37 38 39 |
# File 'lib/lazy_load.rb', line 37 def unmap(name) @actions.delete(name) end |
#wrap(name, &blk) ⇒ Object
75 76 77 78 79 |
# File 'lib/lazy_load.rb', line 75 def wrap(name, &blk) kls = Class.new(Wrapper) kls.class_eval(&blk) @wrappers[name] = kls end |