Class: ActiveModel::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/exporter.rb,
lib/active_model/exporter/types.rb,
lib/active_model/exporter/version.rb

Constant Summary collapse

TYPES =
%i[csv xls].freeze
VERSION =
"0.7.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Exporter

Returns a new instance of Exporter.



24
25
26
27
# File 'lib/active_model/exporter.rb', line 24

def initialize(object, options = {})
  @object = object
  @scope  = options[:scope]
end

Class Attribute Details

._attributesObject

Returns the value of attribute _attributes.



6
7
8
# File 'lib/active_model/exporter.rb', line 6

def _attributes
  @_attributes
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



22
23
24
# File 'lib/active_model/exporter.rb', line 22

def object
  @object
end

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'lib/active_model/exporter.rb', line 22

def scope
  @scope
end

Class Method Details

.attributes(*attrs) ⇒ Object



13
14
15
# File 'lib/active_model/exporter.rb', line 13

def attributes(*attrs)
  @_attributes.concat(attrs)
end

.exporter_for(resource) ⇒ Object



17
18
19
# File 'lib/active_model/exporter.rb', line 17

def exporter_for(resource)
  "#{resource.class.name}Exporter".safe_constantize
end

.inherited(base) ⇒ Object



8
9
10
11
# File 'lib/active_model/exporter.rb', line 8

def inherited(base)
  super
  base._attributes = (_attributes || []).dup
end

Instance Method Details

#attributesObject



43
44
45
# File 'lib/active_model/exporter.rb', line 43

def attributes
  self.class._attributes.dup
end

#filter(attrs) ⇒ Object



39
40
41
# File 'lib/active_model/exporter.rb', line 39

def filter(attrs)
  attrs
end

#valuesObject



29
30
31
32
33
34
35
36
37
# File 'lib/active_model/exporter.rb', line 29

def values
  attrs = filter(attributes)
  attributes.map do |attr|
    if attrs.include?(attr)
      next send(attr) if respond_to?(attr)
      object.send(attr)
    end
  end
end