Class: Smuggle::Exporter::Base

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/smuggle/exporter/base.rb

Direct Known Subclasses

ApplicationExporter

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.attribute_labels(labels) ⇒ Object



23
24
25
# File 'lib/smuggle/exporter/base.rb', line 23

def attribute_labels(labels)
  @attribute_labels.merge!(labels)
end

.attributes(*names) ⇒ Object



15
16
17
# File 'lib/smuggle/exporter/base.rb', line 15

def attributes(*names)
  @attributes.concat names
end

Class Method Details

.attribute_labels?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/smuggle/exporter/base.rb', line 27

def attribute_labels?
  @attribute_labels.any?
end

.attributes?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/smuggle/exporter/base.rb', line 19

def attributes?
  @attributes.any?
end

.headerObject



31
32
33
34
35
36
37
# File 'lib/smuggle/exporter/base.rb', line 31

def header
  return @attributes unless attribute_labels?

  @attributes.map do |attribute|
    @attribute_labels.fetch(attribute, attribute)
  end
end

.inherited(base) ⇒ Object



10
11
12
13
# File 'lib/smuggle/exporter/base.rb', line 10

def inherited(base)
  base.attributes = []
  base.attribute_labels = {}
end

Instance Method Details

#defined_attributesObject



46
47
48
49
50
51
# File 'lib/smuggle/exporter/base.rb', line 46

def defined_attributes
  return self.class.attributes if self.class.attributes?
  return attribute_names if __getobj__.respond_to?(:attribute_names)

  keys if __getobj__.respond_to?(:keys)
end

#to_csvObject



40
41
42
43
44
# File 'lib/smuggle/exporter/base.rb', line 40

def to_csv
  defined_attributes.map do |name|
    respond_to?(name) ? public_send(name) : __getobj__[name]
  end
end