Class: QbIif::IIF

Inherits:
Object
  • Object
show all
Includes:
Keywords
Defined in:
lib/qb_iif/iif.rb

Constant Summary

Constants included from Keywords

Keywords::ESCAPED_KEYWORDS

Instance Method Summary collapse

Methods included from Keywords

#escaped

Constructor Details

#initialize(&block) ⇒ IIF

Returns a new instance of IIF.



30
31
32
33
34
35
36
37
38
39
# File 'lib/qb_iif/iif.rb', line 30

def initialize(&block)
  @output = {}
  if block_given?
    if block.arity == 1
      yield(self)
    else
      instance_eval(&block)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
58
59
60
61
62
# File 'lib/qb_iif/iif.rb', line 55

def method_missing(method_name, *args, &block)
  class_name = escaped(method_name).to_s.split('_').map(&:capitalize).join('')
  result = QbIif::DSL.const_get(class_name).new.build(&block)

  @output[method_name] ||= { headers: [], rows: [] }
  @output[method_name][:headers].concat(result[:headers])
  @output[method_name][:rows].concat(result[:rows])
end

Instance Method Details

#outputObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qb_iif/iif.rb', line 41

def output
  CSV.generate(col_sep: "\t") do |tsv|

    @output.each do |_, list|
      list[:headers].uniq.each do |header|
        tsv << header
      end
      list[:rows].each do |row|
        tsv << row
      end
    end
  end
end