Class: Decisive::StreamCSVContext

Inherits:
Struct
  • Object
show all
Defined in:
lib/decisive/stream_csv_context.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ StreamCSVContext

Returns a new instance of StreamCSVContext.



8
9
10
11
12
# File 'lib/decisive/stream_csv_context.rb', line 8

def initialize *args
  super
  @columns = []
  instance_eval &block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



5
6
7
# File 'lib/decisive/stream_csv_context.rb', line 5

def block
  @block
end

#columnsObject (readonly)

Returns the value of attribute columns.



14
15
16
# File 'lib/decisive/stream_csv_context.rb', line 14

def columns
  @columns
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



5
6
7
# File 'lib/decisive/stream_csv_context.rb', line 5

def filename
  @filename
end

#recordsObject

Returns the value of attribute records

Returns:

  • (Object)

    the current value of records



5
6
7
# File 'lib/decisive/stream_csv_context.rb', line 5

def records
  @records
end

Instance Method Details

#column(label, value = nil, &block) ⇒ Object

field, label: field.to_s.humanize, &block



16
17
18
19
20
# File 'lib/decisive/stream_csv_context.rb', line 16

def column label, value=nil, &block # field, label: field.to_s.humanize, &block
  value ||= label.parameterize.underscore.to_sym
  block ||= ->(record) { record.send(value) }
  columns << Column.new(label, block)
end

#csv?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/decisive/stream_csv_context.rb', line 33

def csv?
  true
end

#each {|header| ... } ⇒ Object

Yields:

  • (header)


22
23
24
25
26
27
28
29
30
31
# File 'lib/decisive/stream_csv_context.rb', line 22

def each
  yield header

  records.map do |record|
    row = columns.map do |column|
      column.block.call(record).to_s
    end
    yield row
  end
end