Class: RecordRoo::DataSource
- Inherits:
-
Object
- Object
- RecordRoo::DataSource
- Defined in:
- lib/record_roo/data_source.rb
Overview
This class wraps a spreadsheet file. It provides two methods:
-
parse for reading the file content as an array of arrays,
-
model_generator for create a collection of class instances based on a given class.
Instance Method Summary collapse
-
#initialize(filepath) ⇒ DataSource
constructor
A new instance of DataSource.
-
#model_generator(model_with_attributes) ⇒ Object
Given a class and an attribute list, it produces an array of class instances.
-
#parse ⇒ Object
Returns an array of rows.
Constructor Details
#initialize(filepath) ⇒ DataSource
Returns a new instance of DataSource.
6 7 8 |
# File 'lib/record_roo/data_source.rb', line 6 def initialize filepath @filepath = filepath end |
Instance Method Details
#model_generator(model_with_attributes) ⇒ Object
Given a class and an attribute list, it produces an array of class instances.
17 18 19 20 21 22 23 24 25 |
# File 'lib/record_roo/data_source.rb', line 17 def model_generator model_with_attributes parse.drop(1).map do |row| model = model_with_attributes[:model] object = Hash[model_with_attributes[:attributes].map.with_index(0) do |attr, index| [attr, row[index]] end] model.new(object) end end |
#parse ⇒ Object
Returns an array of rows
11 12 13 14 |
# File 'lib/record_roo/data_source.rb', line 11 def parse @adapter = Class.instance_eval "RecordRoo::#{adapter}" @adapter.parse @filepath end |