Class: QbIif::IIF

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

Constant Summary collapse

COL_SEP =
"\t"

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.



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

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



49
50
51
52
53
54
55
56
# File 'lib/qb_iif/iif.rb', line 49

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



39
40
41
42
43
44
45
46
47
# File 'lib/qb_iif/iif.rb', line 39

def output
  lines = []
  @output.values.each do |values|
    values[:headers].uniq.each{|header| lines << header.join(COL_SEP) }
    values[:rows].each{|row| lines << row.join(COL_SEP) }
  end
  lines << ''
  lines.join("\n")
end