Class: Bumblebee::Column
- Inherits:
-
Object
- Object
- Bumblebee::Column
- 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
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#property ⇒ Object
readonly
Returns the value of attribute property.
-
#through ⇒ Object
readonly
Returns the value of attribute through.
-
#to_csv ⇒ Object
readonly
Returns the value of attribute to_csv.
-
#to_object ⇒ Object
readonly
Returns the value of attribute to_object.
Instance Method Summary collapse
-
#csv_set(data_object, hash) ⇒ Object
Extract from object and set on hash.
-
#initialize(header, property: nil, through: [], to_csv: nil, to_object: nil) ⇒ Column
constructor
A new instance of Column.
- #object_set(csv_object, hash) ⇒ Object
Constructor Details
#initialize(header, property: nil, through: [], to_csv: nil, to_object: nil) ⇒ Column
Returns a new instance of Column.
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
#header ⇒ Object (readonly)
Returns the value of attribute header.
14 15 16 |
# File 'lib/bumblebee/column.rb', line 14 def header @header end |
#property ⇒ Object (readonly)
Returns the value of attribute property.
14 15 16 |
# File 'lib/bumblebee/column.rb', line 14 def property @property end |
#through ⇒ Object (readonly)
Returns the value of attribute through.
14 15 16 |
# File 'lib/bumblebee/column.rb', line 14 def through @through end |
#to_csv ⇒ Object (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_object ⇒ Object (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 |