Class: Mushy::BuildCsv

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/build_csv.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mushy/fluxs/build_csv.rb', line 4

def self.details
  {
    name: 'BuildCsv',
    title: 'Build CSV',
    description: 'Build a CSV.',
    fluxGroup: { name: 'CSV' },
    config: {
      input_path: {
        description: 'The path to the set of records to include in the CSV.',
        type: 'text',
        value: 'records'
      },
      output_path: {
        description: 'The path to the CSV content in the outgoing event.',
        type: 'text',
        value: 'records'
      },
      headers: {
        description: 'The values to include in the CSV, as well as the header values.',
        type: 'keyvalue',
        value: {}
      },
      header_row: {
        description: 'Include a header row?',
        type: 'boolean',
        value: true
      }
    },
    examples: {
      'Build a Simple CSV' => {
        description: 'Converts a set of records to a CSV.',
        input: {
            things: [
              { name: 'Apple', color: 'Red' },
              { name: 'Banana', color: 'Yellow' },
              { name: 'Pear', color: 'Green' }
            ]
        },
        config: {
          input_path: 'things',
          output_path: 'records',
          headers: { name: 'Name', color: 'Color' },
          header_row: true
        },
        result: {
          records: 'Name,Color\nApple,Red\nBanana,Yellow\nPear,Green\n'
        }
      },
    }
  }
end

Instance Method Details

#process(event, config) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mushy/fluxs/build_csv.rb', line 56

def process(event, config)
  records = event[config[:input_path].to_sym] || event[config[:input_path].to_s]

  headers = config[:headers]

  {
    config[:output_path] => CSV.generate do |c|
      c << headers.map { |h| h[1] } if config[:header_row].to_s == 'true'
      records.each { |x| c << headers.map { |h| x[h[0].to_sym] || x[h[0].to_s] } }
    end
  }
end