Class: ModelExplorer::Export

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record:, associations: []) ⇒ Export

Returns a new instance of Export.

Parameters:

  • record (ModelExplorer::Record)

    Record to export

  • associations (Array<Hash>) (defaults to: [])

    List of associations



8
9
10
11
12
13
14
15
16
# File 'lib/model_explorer/export.rb', line 8

def initialize(record:, associations: [])
  @record = record
  @associations = associations
  @data = nil

  unless record.is_a?(ModelExplorer::Record)
    raise ArgumentError, "Record must be an instance of ModelExplorer::Record"
  end
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



4
5
6
# File 'lib/model_explorer/export.rb', line 4

def associations
  @associations
end

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/model_explorer/export.rb', line 3

def record
  @record
end

Instance Method Details

#dataObject



22
23
24
25
26
27
28
# File 'lib/model_explorer/export.rb', line 22

def data
  @data ||= {
    model: record.klass.name,
    attributes: filtered_attributes,
    associations: fetch_associations
  }
end

#to_jsonObject



18
19
20
# File 'lib/model_explorer/export.rb', line 18

def to_json(*)
  data.to_json
end