Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/wunder/parser.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#products ⇒ Object
Returns the value of attribute products.
Instance Method Summary collapse
-
#initialize(path = "", no_validate = false, use_mock_file = false) ⇒ Parser
constructor
initialize a new conference settings.
-
#mock_file_path ⇒ Object
incase file is not provided.
- #process_file ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(path = "", no_validate = false, use_mock_file = false) ⇒ Parser
initialize a new conference settings
10 11 12 13 |
# File 'lib/wunder/parser.rb', line 10 def initialize(path = "", no_validate = false, use_mock_file = false) @path = (use_mock_file == true ? mock_file_path : path) validate if no_validate == false end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/wunder/parser.rb', line 7 def path @path end |
#products ⇒ Object
Returns the value of attribute products.
7 8 9 |
# File 'lib/wunder/parser.rb', line 7 def products @products end |
Instance Method Details
#mock_file_path ⇒ Object
incase file is not provided
33 34 35 36 |
# File 'lib/wunder/parser.rb', line 33 def mock_file_path source = __dir__ File.join(source, "input/input.csv") end |
#process_file ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wunder/parser.rb', line 20 def process_file @products = ProductCollection.new CSV.foreach(path, col_sep: ",", row_sep: :auto, headers: true) do |row| product = Product.new(row[0], row[1], row[2]) next unless @products.validate_product_code_is_uniq(product.product_code) @products << product end @products end |
#validate ⇒ Object
15 16 17 18 |
# File 'lib/wunder/parser.rb', line 15 def validate err_msg = "No such file found" raise err_msg.to_s if path == "" || path.nil? || File.exist?(path) == false end |