Class: RStore::Data

Inherits:
Object show all
Defined in:
lib/rstore/data.rb

Constant Summary collapse

KnownStates =
[:raw, :parsed, :converted, :error]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, content, state, options) ⇒ Data

Returns a new instance of Data.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/rstore/data.rb', line 21

def initialize path, content, state, options
  error_message = "#{path}: The following options are not valid as an argument to #{self.class}:\n#{options}"
  raise ArgumentError, error_message  unless options.is_a?(Hash)
  @path      = path
  @content   = content
  self.state = state
  @options   = options
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/rstore/data.rb', line 13

def content
  @content
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/rstore/data.rb', line 15

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/rstore/data.rb', line 12

def path
  @path
end

#stateObject

Returns the value of attribute state.



14
15
16
# File 'lib/rstore/data.rb', line 14

def state
  @state
end

Instance Method Details

#convert_fields(database, table_name) ⇒ Object



55
56
57
58
# File 'lib/rstore/data.rb', line 55

def convert_fields database, table_name
  converter = Converter.new(self, database, table_name)
  converter.convert
end

#into_db(database, table_name) ⇒ Object



61
62
63
# File 'lib/rstore/data.rb', line 61

def into_db database, table_name
  Storage.new(self, database, table_name).insert
end

#parse_csvObject

def extract_type path

path, filename = File.split(path)
filename.match(/\.(?<type>.*)$/)[:type].to_sym

end

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rstore/data.rb', line 36

def parse_csv
  raise InvalidStateError, "#{state.inspect} is not a valid Data state for method 'to_csv'"  unless state == :raw

  file_options  = @options[:file_options]
  parse_options = @options[:parse_options]

  begin
    csv = CSVWrapper.parse(@content, parse_options)
    csv = csv.drop(1)  if file_options[:has_headers] == true  # drop the first row if it is a header
  rescue => e
    Logger.new(@options).print(@path, :parse, e)
  end

  @state = :parsed
    Data.new(@path, csv, @state, @options)
end

Helper methods ——————————–



74
75
76
# File 'lib/rstore/data.rb', line 74

def print_valid_states
  KnownStates.map { |s| s.inspect }.join(', ')
end