Class: Bolt::Util::OnAccess
- Inherits:
-
Object
- Object
- Bolt::Util::OnAccess
- Defined in:
- lib/bolt/util/on_access.rb
Instance Method Summary collapse
-
#initialize(&block) ⇒ OnAccess
constructor
A new instance of OnAccess.
-
#method_missing(method, *args, &block) ⇒ Object
If a method is called and we haven’t constructed the object, construct it.
Constructor Details
#initialize(&block) ⇒ OnAccess
Returns a new instance of OnAccess.
6 7 8 9 |
# File 'lib/bolt/util/on_access.rb', line 6 def initialize(&block) @constructor = block @obj = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
If a method is called and we haven’t constructed the object, construct it. Then pass the call to the object. rubocop:disable Style/MethodMissingSuper rubocop:disable Style/MissingRespondToMissing
15 16 17 18 19 20 21 |
# File 'lib/bolt/util/on_access.rb', line 15 def method_missing(method, *args, &block) if @obj.nil? @obj = @constructor.call end @obj.send(method, *args, &block) end |