Class: Comma::HeaderExtractor

Inherits:
Extractor show all
Defined in:
lib/comma/header_extractor.rb

Constant Summary collapse

DEFAULT_VALUE_HUMANIZER =
lambda do |value, _model_class|
  value.is_a?(String) ? value : value.to_s.humanize
end

Instance Method Summary collapse

Methods inherited from Extractor

#__use__, #id, #initialize, #results

Constructor Details

This class inherits a constructor from Comma::Extractor

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &_block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/comma/header_extractor.rb', line 18

def method_missing(sym, *args, &_block)
  model_class = @instance.class
  @results << value_humanizer.call(sym, model_class) if args.blank?
  args.each do |arg|
    case arg
    when Hash
      arg.each do |_k, v|
        @results << value_humanizer.call(v, get_association_class(model_class, sym))
      end
    when Symbol
      @results << value_humanizer.call(arg, get_association_class(model_class, sym))
    when String
      @results << value_humanizer.call(arg, model_class)
    else
      raise "Unknown header symbol #{arg.inspect}"
    end
  end
end

Instance Method Details

#__static_column__(header = '', &_block) ⇒ Object



37
38
39
# File 'lib/comma/header_extractor.rb', line 37

def __static_column__(header = '', &_block)
  @results << header
end