lib/csv2hash.rb in csv2hash-0.6.4 vs lib/csv2hash.rb in csv2hash-0.6.5
- old
+ new
@@ -1,6 +1,7 @@
require_relative 'csv2hash/version'
+require_relative 'csv2hash/configuration'
require_relative 'csv2hash/registry'
require_relative 'csv2hash/cell'
require_relative 'csv2hash/definition'
require_relative 'csv2hash/validator'
require_relative 'csv2hash/validator/mapping'
@@ -15,21 +16,41 @@
require_relative 'csv2hash/data_wrapper'
require_relative 'csv2hash/notifier'
require_relative 'csv2hash/extra_validator'
require_relative 'csv2hash/adapters/base'
require_relative 'csv2hash/yaml_loader'
+require_relative 'csv2hash/coercers/type_coercer'
+require 'csv2hash/railtie' if defined?(Rails)
+
require 'active_support/core_ext/array/extract_options'
begin
require 'pry'
rescue LoadError
end
module Csv2hash
+
+ class << self
+ attr_writer :configuration
+ end
+
+ def self.configuration
+ @configuration ||= Configuration.new
+ end
+
+ def self.reset
+ @configuration = Configuration.new
+ end
+
+ def self.configure
+ yield(configuration)
+ end
+
class Main
- include Csv2hash::StructureValidator
+ include StructureValidator
class << self
def generate_definition name, &block
definition = Definition.new name, &block
@@ -87,13 +108,14 @@
definition.validate!
definition.default!
validate_structure!
validate_data!
- Csv2hash::DataWrapper.new.tap do |response|
+ DataWrapper.new.tap do |response|
if valid?
fill!
+ TypeCoercer.new(data[:data]).deserialize! if Csv2hash.configuration.convert
response.data = data[:data]
else
response.valid = false
response.errors = csv_with_errors
notifier.notify response
@@ -113,9 +135,10 @@
# protected
def data_source
@data_source ||= begin
+ self.file_path_or_data = Pathname(self.file_path_or_data) if self.file_path_or_data.is_a?(String)
adapter_name = self.file_path_or_data.respond_to?(:to_path) ? :csv : :memory
adapter = Adapter::Base.create(adapter_name, self.file_path_or_data)
adapter.source
end
end