Class: RStore::Data
Constant Summary collapse
- KnownStates =
[:raw, :parsed, :converted, :error]
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #convert_fields(database, table_name) ⇒ Object
-
#initialize(path, content, state, options) ⇒ Data
constructor
A new instance of Data.
- #into_db(database, table_name) ⇒ Object
-
#parse_csv ⇒ Object
def extract_type path path, filename = File.split(path) filename.match(/\.(?<type>.*)$/)[:type].to_sym end.
-
#print_valid_states ⇒ Object
Helper methods ——————————–.
Constructor Details
#initialize(path, content, state, options) ⇒ Data
Returns a new instance of Data.
21 22 23 24 25 26 27 28 |
# File 'lib/rstore/data.rb', line 21 def initialize path, content, state, = "#{path}: The following options are not valid as an argument to #{self.class}:\n#{}" raise ArgumentError, unless .is_a?(Hash) @path = path @content = content self.state = state @options = end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
13 14 15 |
# File 'lib/rstore/data.rb', line 13 def content @content end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/rstore/data.rb', line 15 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/rstore/data.rb', line 12 def path @path end |
#state ⇒ Object
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_csv ⇒ Object
def extract_type path
path, filename = File.split(path)
filename.match(/\.(?<type>.*)$/)[:type].to_sym
end
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 = @options[:file_options] = @options[:parse_options] begin csv = CSVWrapper.parse(@content, ) csv = csv.drop(1) if [: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 |
#print_valid_states ⇒ Object
Helper methods ——————————–
74 75 76 |
# File 'lib/rstore/data.rb', line 74 def print_valid_states KnownStates.map { |s| s.inspect }.join(', ') end |