Module: CsvRecord::Writer::ClassMethods

Defined in:
lib/csv_record/csv_writers/class_writer.rb

Instance Method Summary collapse

Instance Method Details

#__create__(attributes = {}) {|instance| ... } ⇒ Object Also known as: create

Yields:

  • (instance)


3
4
5
6
7
8
# File 'lib/csv_record/csv_writers/class_writer.rb', line 3

def __create__(attributes={})
  instance = self.build attributes
  yield instance if block_given?
  result = instance.save
  instance
end

#mapping(config = []) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/csv_record/csv_writers/class_writer.rb', line 32

def mapping(config=[])
  config.each do |field, doppelganger|
    unless self.fields.include? field
      self.fields << (CsvRecord::Field.new field, doppelganger)
    end
  end
end

#redefine_database_locationObject



40
41
42
43
44
45
46
47
48
# File 'lib/csv_record/csv_writers/class_writer.rb', line 40

def redefine_database_location
  if const_defined?('DATABASE_LOCATION') || const_defined?('DATABASE_LOCATION_TMP')
    send :remove_const, 'DATABASE_LOCATION'
    send :remove_const, 'DATABASE_LOCATION_TMP'
  end

  const_set 'DATABASE_LOCATION', "db/#{@table_name}.csv"
  const_set 'DATABASE_LOCATION_TMP', "db/#{@table_name}_tmp.csv"
end

#store_as(name) ⇒ Object



21
22
23
24
25
26
# File 'lib/csv_record/csv_writers/class_writer.rb', line 21

def store_as(name)
  @table_name = name.to_s.underscore.pluralize
  redefine_database_location

  @table_name
end

#table_nameObject



28
29
30
# File 'lib/csv_record/csv_writers/class_writer.rb', line 28

def table_name
  @table_name ||= store_as name
end