Class: LcpRuby::Export::DataGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/export/data_generator.rb

Overview

Generates export files (CSV/XLSX) from records and selected fields.

Constant Summary collapse

BOM =
"\xEF\xBB\xBF"

Instance Method Summary collapse

Constructor Details

#initialize(records:, selected_fields:, model_definition:, evaluator:, formatting_options: {}, raw: false) ⇒ DataGenerator

Returns a new instance of DataGenerator.

Parameters:

  • records (Array<ActiveRecord::Base>)

    loaded records

  • selected_fields (Array<String>)

    field paths (e.g., [“name”, “company.name”])

  • model_definition (Metadata::ModelDefinition)
  • evaluator (Authorization::PermissionEvaluator)
  • formatting_options (Hash) (defaults to: {})

    options for ValueFormatter

  • raw (Boolean) (defaults to: false)

    when true, emit native types and use field paths as headers (no i18n). Used by raw ‘.json` / `.csv` endpoints.



16
17
18
19
20
21
22
23
24
# File 'lib/lcp_ruby/export/data_generator.rb', line 16

def initialize(records:, selected_fields:, model_definition:, evaluator:, formatting_options: {}, raw: false)
  @records = records
  @selected_fields = selected_fields
  @model_definition = model_definition
  @evaluator = evaluator
  @raw = raw
  @formatter = ValueFormatter.new(formatting_options)
  @field_value_resolver = Presenter::FieldValueResolver.new(model_definition, evaluator)
end

Instance Method Details

#to_csv(delimiter: ",") ⇒ String

Returns CSV content with BOM.

Parameters:

  • delimiter (String) (defaults to: ",")

    CSV delimiter character

Returns:

  • (String)

    CSV content with BOM



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lcp_ruby/export/data_generator.rb', line 28

def to_csv(delimiter: ",")
  csv_string = CSV.generate(col_sep: delimiter) do |csv|
    csv << headers(output: :csv)
    @records.each do |record|
      csv << row_values(record, output: :csv)
    end
  end

  # BOM is for Excel UTF-8 detection — useful for human-driven export,
  # but pollutes the first header cell when raw CSV is parsed by programs.
  @raw ? csv_string : BOM + csv_string
end

#to_json_arrayArray<Hash>

Returns one hash per record, keyed by field path.

Returns:

  • (Array<Hash>)

    one hash per record, keyed by field path



42
43
44
45
46
# File 'lib/lcp_ruby/export/data_generator.rb', line 42

def to_json_array
  @records.map do |record|
    @selected_fields.zip(row_values(record, output: :json)).to_h
  end
end

#to_xlsxString

Returns XLSX binary content.

Returns:

  • (String)

    XLSX binary content



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lcp_ruby/export/data_generator.rb', line 49

def to_xlsx
  unless defined?(Axlsx)
    raise LcpRuby::Error, "XLSX export requires the 'caxlsx' gem. Add `gem 'caxlsx'` to your Gemfile."
  end

  package = Axlsx::Package.new
  package.workbook.add_worksheet(name: "Export") do |sheet|
    bold = sheet.workbook.styles.add_style(b: true)
    sheet.add_row(headers, style: bold)

    @records.each do |record|
      sheet.add_row(row_values(record), types: column_types)
    end
  end

  stream = package.to_stream
  stream.read
end