lib/cascade/row_processor.rb in cascade-rb-0.2.3 vs lib/cascade/row_processor.rb in cascade-rb-0.3.0
- old
+ new
@@ -1,29 +1,29 @@
-require "cascade/complex_fields"
-require "cascade/exceptions"
-require "cascade/helpers/configuration"
-require "cascade/helpers/hash"
+# frozen_string_literal: true
+require 'cascade/complex_fields'
+require 'cascade/exceptions'
+require 'cascade/helpers/hash'
+
module Cascade
class RowProcessor
- extend Configuration
using HashRefinements
- DEFAULT_PROCESSOR = ->(value) { value && value.to_s.strip }
+ DEFAULT_PROCESSOR = ->(value) { value&.to_s&.strip }
- define_setting :use_default_presenter, false
- define_setting :deafult_presenter, -> { DEFAULT_PROCESSOR }
-
def initialize(options = {})
- @options = options
- @columns_matching = options[:columns_matching] || ColumnsMatching.new
+ @options = options
+ @ext_presenters = options[:ext_presenters].to_h
+ @columns_matching = options.fetch(:columns_matching)
+ @use_default_presenter = options.fetch(:use_default_presenter, false)
+ @deafult_presenter = options.fetch(:deafult_presenter, DEFAULT_PROCESSOR)
end
# Iterates through object using columns values supported keys as keys for
- # iterating, then parse it by curresponding parser.
+ # iterating, then parse it by the corresponding parser.
#
- # @param row [Hash] the object retrieved from CSV
+ # @param row [Hash] the object retrieved from data parser
# @return [Hash] the object with parsed columns
def call(row)
@columns_matching.supported_keys.inject({}) do |result, key|
raw_value = row.fetch(@columns_matching.index(key))
value = receive_presenter(key).call(raw_value)
@@ -35,30 +35,31 @@
attr_reader :options
def receive_presenter(column_name)
presenter = presenters[@columns_matching.column_type(column_name)]
- if presenter.nil? && !self.class.use_default_presenter
- raise Cascade::UnknownPresenterType.new
+ if presenter.nil? && !@use_default_presenter
+ raise Cascade::UnknownPresenterType
end
- presenter || self.class.deafult_presenter
+
+ presenter || @deafult_presenter
end
def presenters
- @presenters ||= options.reverse_merge(defined_presenters)
+ @presenters ||= @ext_presenters.reverse_merge(predefined_presenters)
end
def self_copy
self.class.new(options)
end
- def defined_presenters
+ def predefined_presenters
{
- string: DEFAULT_PROCESSOR,
- currency: ComplexFields::Currency.new,
- boolean: ComplexFields::Boolean.new,
+ string: DEFAULT_PROCESSOR,
+ currency: ComplexFields::Currency.new,
+ boolean: ComplexFields::Boolean.new,
iterable_recursive: ComplexFields::ArrayProcessor.new(self_copy),
- recursive: self_copy
+ recursive: self_copy
}
end
end
end