Class: Xporter::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/xporter/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, title = nil, &block) ⇒ Column

Returns a new instance of Column.



5
6
7
8
9
# File 'lib/xporter/column.rb', line 5

def initialize(attribute_name, title = nil, &block)
  @attribute_name = attribute_name
  @title = title
  @block = block || ->(model){ model.public_send(attribute_name) }
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



3
4
5
# File 'lib/xporter/column.rb', line 3

def attribute_name
  @attribute_name
end

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/xporter/column.rb', line 3

def block
  @block
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/xporter/column.rb', line 3

def title
  @title
end

Instance Method Details

#data(model) ⇒ Object



11
12
13
# File 'lib/xporter/column.rb', line 11

def data(model)
  block.call(model)
end

#title_from(resource_class) ⇒ Object



15
16
17
18
19
20
# File 'lib/xporter/column.rb', line 15

def title_from(resource_class)
  return title if title.present?
  return attribute_name.to_s.titleize unless resource_class.respond_to?(:human_attribute_name)

  resource_class.human_attribute_name(attribute_name)
end