Class: TableStructure::Writer
- Inherits:
-
Object
- Object
- TableStructure::Writer
- Defined in:
- lib/table_structure/writer.rb
Defined Under Namespace
Classes: Output
Instance Method Summary collapse
-
#initialize(schema, header: { context: nil }, method: :<<, row_type: :array, **deprecated_options) ⇒ Writer
constructor
A new instance of Writer.
- #write(items, to:, method: @options[:method], **deprecated_options, &block) ⇒ Object
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, ** ) if .key?(:header_omitted) header_omitted = [:header_omitted] warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead." header = !header_omitted end if .key?(:header_context) header_context = [:header_context] warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.' header = { context: header_context } end if .key?(:result_type) warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.' row_type = [: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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/table_structure/writer.rb', line 37 def write( items, to:, method: @options[:method], **, &block ) header = @options[:header] row_type = @options[:row_type] if .key?(:header) header = [:header] warn '[TableStructure] Specify :header option as an argument for initialize method.' end if .key?(:header_omitted) header_omitted = [:header_omitted] warn "[TableStructure] `header_omitted: #{!!header_omitted}` option has been deprecated. Use `header: #{!header_omitted}` option instead." header = !header_omitted end if .key?(:header_context) header_context = [:header_context] warn '[TableStructure] `:header_context` option has been deprecated. Use `header: { context: ... }` option instead.' header = { context: header_context } end if .key?(:row_type) row_type = [:row_type] warn '[TableStructure] Specify :row_type option as an argument for initialize method.' end if .key?(:result_type) warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.' row_type = [:result_type] end items = enumerize(items) @schema.create_table(row_type: row_type) do |table| output = Output.new(to, method: method) enum = Table::Iterator .new( table, header: header ) .iterate(items) if block_given? enum = enum .lazy .map { |row| block.call(row) } end enum.each { |row| output.write(row) } end nil end |