Module: Machined::Helpers::LocalsHelpers

Included in:
Context
Defined in:
lib/machined/helpers/locals_helpers.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc:



41
42
43
44
45
46
47
# File 'lib/machined/helpers/locals_helpers.rb', line 41

def method_missing(method, *args, &block) # :nodoc:
  if args.empty? && has_local?(method)
    locals[method]
  else
    super
  end
end

Instance Method Details

#has_local?(name) ⇒ Boolean

Returns true if the given name has been set as a local variable.

Returns:

  • (Boolean)


37
38
39
# File 'lib/machined/helpers/locals_helpers.rb', line 37

def has_local?(name)
  locals.key? name
end

#localsObject

Returns the locals hash. It’s actually an instance of ‘ActiveSupport::HashWithIndifferentAccess`, so strings and symbols can be used interchangeably.



9
10
11
# File 'lib/machined/helpers/locals_helpers.rb', line 9

def locals
  @locals ||= ActiveSupport::HashWithIndifferentAccess.new
end

#locals=(locals) ⇒ Object

Adds psuedo local variables from the given hash, where the key is the name of the variable. This is provided so processors can add local variables without having access to the next processor or template.



17
18
19
20
21
22
23
# File 'lib/machined/helpers/locals_helpers.rb', line 17

def locals=(locals)
  if locals.nil?
    @locals = nil
  else
    self.locals.merge! locals
  end
end

#respond_to?(method) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


49
50
51
# File 'lib/machined/helpers/locals_helpers.rb', line 49

def respond_to?(method) # :nodoc:
  super or has_local?(method)
end

#with_locals(temporary_locals) ⇒ Object

Temporarily changes the locals. The given temporary_locals will be merged into the current locals. After the block is executed, the locals will be restored to their original state.



28
29
30
31
32
33
# File 'lib/machined/helpers/locals_helpers.rb', line 28

def with_locals(temporary_locals)
  old_locals, self.locals = self.locals.dup, temporary_locals
  yield
ensure
  @locals = old_locals
end