Module: CSVMachine::ClassMethods
- Defined in:
- lib/csv-machine.rb
Overview
TODO: def self.map(klass, &block) end
Instance Attribute Summary collapse
-
#csv_fields ⇒ Object
readonly
Returns the value of attribute csv_fields.
-
#csv_options ⇒ Object
readonly
Returns the value of attribute csv_options.
Instance Method Summary collapse
- #create_attribute(name) ⇒ Object
- #field(name, options = {}) ⇒ Object
- #from_csv(row) ⇒ Object
- #parse(data) ⇒ Object
- #set_csv_option(name, value) ⇒ Object
Instance Attribute Details
#csv_fields ⇒ Object (readonly)
Returns the value of attribute csv_fields.
14 15 16 |
# File 'lib/csv-machine.rb', line 14 def csv_fields @csv_fields end |
#csv_options ⇒ Object (readonly)
Returns the value of attribute csv_options.
15 16 17 |
# File 'lib/csv-machine.rb', line 15 def @csv_options end |
Instance Method Details
#create_attribute(name) ⇒ Object
54 55 56 57 |
# File 'lib/csv-machine.rb', line 54 def create_attribute(name) attr_reader name unless method_defined?(name) attr_writer name unless method_defined?(:"#{name}=") end |
#field(name, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/csv-machine.rb', line 33 def field(name, ={}) #options.clone do |hash| #hash[(key.to_sym rescue key) || key] = options.delete(key) #end [:column] ||= begin csv_fields.empty? ? 0 : ( csv_fields.values.last.[:column] + 1 ) end _field = Field.new(name, ) create_attribute(_field.name) csv_fields[_field.name] = _field end |
#from_csv(row) ⇒ Object
48 49 50 51 52 |
# File 'lib/csv-machine.rb', line 48 def from_csv(row) new.tap do |obj| csv_fields.each { |n, f| obj.__send__(:"#{n}=", row[f.column]) } end end |
#parse(data) ⇒ Object
59 60 61 |
# File 'lib/csv-machine.rb', line 59 def parse(data) CSV.new(data, ).collect { |row| from_csv(row) } end |
#set_csv_option(name, value) ⇒ Object
27 28 29 |
# File 'lib/csv-machine.rb', line 27 def set_csv_option(name, value) raise "Unknown csv option `#{name}'" unless .key?(name) end |