Class: HALPresenter::LazyEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_presenter/lazy_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, context) ⇒ LazyEvaluator

Returns a new instance of LazyEvaluator.



8
9
10
11
# File 'lib/hal_presenter/lazy_evaluator.rb', line 8

def initialize(block, context)
  @context = context
  define_singleton_method(:evaluate_block, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



30
31
32
33
34
35
36
37
38
# File 'lib/hal_presenter/lazy_evaluator.rb', line 30

def method_missing(method, *args, &block)
  return super unless context.respond_to?(method)

  define_singleton_method(method) do |*a, &b|
    context.public_send(method, *a, &b)
  end

  public_send(method, *args, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/hal_presenter/lazy_evaluator.rb', line 5

def options
  @options
end

#resourceObject (readonly) Also known as: resources

Returns the value of attribute resource.



5
6
7
# File 'lib/hal_presenter/lazy_evaluator.rb', line 5

def resource
  @resource
end

Instance Method Details

#evaluate(resource, options) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/hal_presenter/lazy_evaluator.rb', line 17

def evaluate(resource, options)
  @resource = resource
  @options = options || {}
  evaluate_block
ensure
  @resource = nil
  @options = nil
end

#update_context(context) ⇒ Object



13
14
15
# File 'lib/hal_presenter/lazy_evaluator.rb', line 13

def update_context(context)
  @context = context
end