Class: CsvTemplateGenerator::Template

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/csv_template_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_spec, csv_options = {}) ⇒ Template

For examples of usage, look at the module-level methods in this very file!



67
68
69
70
71
72
73
74
75
76
77
78
# File 'backend/app/lib/csv_template_generator.rb', line 67

def initialize(template_spec, csv_options = {})
  @template_spec = template_spec
  @fields = @template_spec.columns.keys

  @headers = [[@template_spec.sheet_description] + ([""] * (@template_spec.columns.count - 1))]

  @headers << [@template_spec.group_text] + @fields.map {|k| get_group(k) } if @template_spec.group_text
  @headers << [@template_spec.description_text] + @fields.map {|k| get_desc(k) } if @template_spec.description_text

  @headers += ([[@template_spec.field_name_text, @template_spec.title_text]] + @fields.map {|k| [k.to_s, get_title(k).to_s]}).transpose
  @csv_options = {encoding: 'UTF-8'}.merge(csv_options)
end

Instance Method Details

#each(dataset) ⇒ Object

creates an enumerator over a dataset, which returns successive lines of CSV if a block is provided, iterates over the enumerator



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'backend/app/lib/csv_template_generator.rb', line 142

def each(dataset)
  enum = Enumerator.new do |y|
    @headers.each do |hdr_line|
      y.yield CSV.generate_line(hdr_line, **@csv_options)
    end
    dataset.paged_each do |row|
      y.yield CSV.generate_line([''] + @fields.map do |field| formatted_value(field, row[field]) end, **@csv_options)
    end
  end
  if not block_given?
    return enum
  else
    enum.each do |el|
      yield el
    end
  end
end

#formatted_value(field, value) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'backend/app/lib/csv_template_generator.rb', line 129

def formatted_value(field, value)
  return nil if is_blank? field

  formatter = get_format(field)
  if formatter
    formatter.call(value)
  else
    value
  end
end

#get_desc(field) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'backend/app/lib/csv_template_generator.rb', line 90

def get_desc(field)
  spec = @template_spec.columns[field]
  case spec
  when Hash
    spec[:description]
  else
    nil
  end
end

#get_format(field) ⇒ Object



110
111
112
113
114
115
116
117
# File 'backend/app/lib/csv_template_generator.rb', line 110

def get_format(field)
  spec = @template_spec.columns[field]
  if Hash === spec
    spec[:formatter]
  else
    nil
  end
end

#get_group(field) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'backend/app/lib/csv_template_generator.rb', line 100

def get_group(field)
  spec = @template_spec.columns[field]
  case spec
  when Hash
    spec[:group]
  else
    nil
  end
end

#get_title(field) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'backend/app/lib/csv_template_generator.rb', line 80

def get_title(field)
  spec = @template_spec.columns[field]
  case spec
  when Hash
    spec[:title] + (spec[:required] ? ' (required)' : '')
  else
    spec + (spec[:required] ? ' (required)' : '')
  end
end

#is_blank?(field) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'backend/app/lib/csv_template_generator.rb', line 119

def is_blank?(field)
  spec = @template_spec.columns[field]
  Hash === spec && spec[:blank]
end

#is_required?(field) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
# File 'backend/app/lib/csv_template_generator.rb', line 124

def is_required?(field)
  spec = @template_spec.columns[field]
  Hash === spec && spec[:required]
end