Class: Fortitude::Rails::YieldedObjectOutputter

Inherits:
YIELDED_OBJECT_OUTPUTTER_SUPERCLASS
  • Object
show all
Defined in:
lib/fortitude/rails/yielded_object_outputter.rb

Constant Summary collapse

EMPTY_RETURN_VALUE =
''.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_target, yielded_object, for_method_name, method_names) ⇒ YieldedObjectOutputter


18
19
20
21
22
23
24
25
26
# File 'lib/fortitude/rails/yielded_object_outputter.rb', line 18

def initialize(output_target, yielded_object, for_method_name, method_names)
  @output_target = output_target
  @yielded_object = yielded_object
  @for_method_name = for_method_name
  @method_names_hash = { }
  method_names.each do |method_name|
    @method_names_hash[method_name.to_sym] = true
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


30
31
32
33
34
35
36
37
38
39
# File 'lib/fortitude/rails/yielded_object_outputter.rb', line 30

def method_missing(method_name, *args, &block)
  if @method_names_hash[method_name.to_sym]
    block = ::Fortitude::Rails::YieldedObjectOutputter.wrap_block_as_needed(@output_target, method_name, block, @method_names_hash.keys)
    return_value = @yielded_object.send(method_name, *args, &block)
    @output_target.rawtext(return_value)
    EMPTY_RETURN_VALUE
  else
    @yielded_object.send(method_name, *args, &block)
  end
end

Class Method Details

.wrap_block_as_needed(output_target, for_method_name, original_block, yielded_methods_to_output) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'lib/fortitude/rails/yielded_object_outputter.rb', line 6

def wrap_block_as_needed(output_target, for_method_name, original_block, yielded_methods_to_output)
  if original_block && yielded_methods_to_output
    lambda do |yielded_object, *args|
      outputter = new(output_target, yielded_object, for_method_name, yielded_methods_to_output)
      original_block.call(outputter, *args)
    end
  else
    original_block
  end
end

Instance Method Details

#respond_to?(symbol, include_all = false) ⇒ Boolean


41
42
43
# File 'lib/fortitude/rails/yielded_object_outputter.rb', line 41

def respond_to?(symbol, include_all = false)
  @yielded_object.respond_to?(symbol, include_all)
end