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

Returns a new instance of YieldedObjectOutputter.



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

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



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

def method_missing(method_name, *args, &block)
  method_name = method_name.to_sym
  method_name = args.shift if method_name == :send

  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
16
# 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 && original_block.arity > 0
    lambda do |*args|
      yielded_object = args.shift
      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

Returns:

  • (Boolean)


45
46
47
# File 'lib/fortitude/rails/yielded_object_outputter.rb', line 45

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