Class: Riif::IIF

Inherits:
Object
  • Object
show all
Defined in:
lib/riif/iif.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ IIF

Returns a new instance of IIF.



26
27
28
29
30
31
32
33
34
35
# File 'lib/riif/iif.rb', line 26

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



51
52
53
54
55
56
57
58
# File 'lib/riif/iif.rb', line 51

def method_missing(method_name, *args, &block)
  result = eval("::Riif::DSL::#{method_name.capitalize}").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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/riif/iif.rb', line 37

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