Class: TableStructure::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/writer.rb

Defined Under Namespace

Classes: Output

Instance Method Summary collapse

Constructor Details

#initialize(schema, header: { context: nil }, method: :<<, row_type: :array, **deprecated_options) ⇒ Writer

Returns a new instance of Writer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/table_structure/writer.rb', line 5

def initialize(
  schema,
  header: { context: nil },
  method: :<<,
  row_type: :array,
  **deprecated_options
)
  if deprecated_options.key?(:header_omitted)
    header_omitted = deprecated_options[:header_omitted]
    warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead."
    header = !header_omitted
  end

  if deprecated_options.key?(:header_context)
    header_context = deprecated_options[:header_context]
    warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.'
    header = { context: header_context }
  end

  if deprecated_options.key?(:result_type)
    warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
    row_type = deprecated_options[:result_type]
  end

  @schema = schema
  @options = {
    header: header,
    method: method,
    row_type: row_type
  }
end

Instance Method Details

#write(items, to:, method: @options[:method], **deprecated_options, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/table_structure/writer.rb', line 37

def write(
  items,
  to:,
  method: @options[:method],
  **deprecated_options,
  &block
)
  header = @options[:header]
  row_type = @options[:row_type]

  if deprecated_options.key?(:header)
    header = deprecated_options[:header]
    warn '[TableStructure] Use :header option on initialize method.'
  end

  if deprecated_options.key?(:header_omitted)
    header_omitted = deprecated_options[:header_omitted]
    warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead."
    header = !header_omitted
  end

  if deprecated_options.key?(:header_context)
    header_context = deprecated_options[:header_context]
    warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.'
    header = { context: header_context }
  end

  if deprecated_options.key?(:row_type)
    row_type = deprecated_options[:row_type]
    warn '[TableStructure] Use :row_type option on initialize method.'
  end

  if deprecated_options.key?(:result_type)
    warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
    row_type = deprecated_options[:result_type]
  end

  output = Output.new(to, method: method)

  Iterator
    .new(@schema, header: header, row_type: row_type)
    .iterate(items, &block)
    .each { |row| output.write(row) }

  nil
end