Class: Excelizer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/excelizer/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection = []) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/excelizer/base.rb', line 6

def initialize(collection = [])
  self.collection = collection
end

Class Attribute Details

.instance_methodObject



34
35
36
# File 'lib/excelizer/base.rb', line 34

def instance_method
  @instance_method ||= :object
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



4
5
6
# File 'lib/excelizer/base.rb', line 4

def collection
  @collection
end

Class Method Details

.attribute(name, options = {}) ⇒ Object



38
39
40
# File 'lib/excelizer/base.rb', line 38

def attribute(name, options = {})
  attributes << Attribute.new(name, options)
end

.attributesObject



42
43
44
# File 'lib/excelizer/base.rb', line 42

def attributes
  @attributes ||= []
end

.headersObject



46
47
48
# File 'lib/excelizer/base.rb', line 46

def headers
  attributes.flat_map(&:header)
end

.instance(instance_method) ⇒ Object



29
30
31
32
# File 'lib/excelizer/base.rb', line 29

def instance(instance_method)
  self.instance_method = instance_method
  define_method(instance_method) { @record }
end

Instance Method Details

#downloadObject



10
11
12
# File 'lib/excelizer/base.rb', line 10

def download
  Excelizer::Writer.write self.class.headers, records
end

#recordsObject



14
15
16
17
18
19
20
21
22
# File 'lib/excelizer/base.rb', line 14

def records
  collection.map do |item|
    @record = item
    self.class.attributes.map do |attribute|
      method = attribute.name
      (respond_to?(method) ? send(method) : item.send(method)).to_s
    end
  end
end