Class: Bumblebee::Column

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

Overview

This is main piece of logic that defines a column which can go from objects to csv cell values and csv rows to objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, property: nil, through: [], to_csv: nil, to_object: nil) ⇒ Column

Returns a new instance of Column.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bumblebee/column.rb', line 20

def initialize(
  header,
  property: nil,
  through: [],
  to_csv: nil,
  to_object: nil
)
  raise ArgumentError, 'header is required' if header.to_s.empty?

  @header       = header.to_s
  @property     = property || @header
  @through      = Array(through)
  @to_csv       = Mutator.new(to_csv)
  @to_object    = Mutator.new(to_object)
  @resolver     = Objectable.resolver

  freeze
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



14
15
16
# File 'lib/bumblebee/column.rb', line 14

def header
  @header
end

#propertyObject (readonly)

Returns the value of attribute property.



14
15
16
# File 'lib/bumblebee/column.rb', line 14

def property
  @property
end

#throughObject (readonly)

Returns the value of attribute through.



14
15
16
# File 'lib/bumblebee/column.rb', line 14

def through
  @through
end

#to_csvObject (readonly)

Returns the value of attribute to_csv.



14
15
16
# File 'lib/bumblebee/column.rb', line 14

def to_csv
  @to_csv
end

#to_objectObject (readonly)

Returns the value of attribute to_object.



14
15
16
# File 'lib/bumblebee/column.rb', line 14

def to_object
  @to_object
end

Instance Method Details

#csv_set(data_object, hash) ⇒ Object

Extract from object and set on hash



40
41
42
43
44
# File 'lib/bumblebee/column.rb', line 40

def csv_set(data_object, hash)
  value = resolver.get(data_object, full_property)

  to_csv.set(hash, header, value)
end

#object_set(csv_object, hash) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/bumblebee/column.rb', line 46

def object_set(csv_object, hash)
  value = csv_object[header]

  to_object.set(hash, full_property, value)

  hash
end