Class: Decisive::StreamContext

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

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ StreamContext

Returns a new instance of StreamContext.



115
116
117
118
# File 'lib/decisive/template_handler.rb', line 115

def initialize *args, &block
  super
  instance_eval &block
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns

Returns:

  • (Object)

    the current value of columns



112
113
114
# File 'lib/decisive/template_handler.rb', line 112

def columns
  @columns
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



112
113
114
# File 'lib/decisive/template_handler.rb', line 112

def filename
  @filename
end

#recordsObject

Returns the value of attribute records

Returns:

  • (Object)

    the current value of records



112
113
114
# File 'lib/decisive/template_handler.rb', line 112

def records
  @records
end

Instance Method Details

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

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



120
121
122
123
124
# File 'lib/decisive/template_handler.rb', line 120

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)


137
138
139
# File 'lib/decisive/template_handler.rb', line 137

def csv?
  true
end

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

Yields:

  • (header)


126
127
128
129
130
131
132
133
134
135
# File 'lib/decisive/template_handler.rb', line 126

def each
  yield header

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