lib/chronicle/etl/configurable.rb in chronicle-etl-0.4.4 vs lib/chronicle/etl/configurable.rb in chronicle-etl-0.5.0
- old
+ new
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require "ostruct"
+require "chronic_duration"
module Chronicle
module ETL
# A mixin that gives a class
# a {Chronicle::ETL::Configurable::ClassMethods#setting} macro to define
@@ -55,12 +56,14 @@
def apply_options(options)
options.transform_keys!(&:to_sym)
options.each do |name, value|
setting = self.class.all_settings[name]
- raise(Chronicle::ETL::ConnectorConfigurationError, "Unrecognized setting: #{name}") unless setting
+ # Do nothing with a given option if it's not a connector setting
+ next unless setting
+
@config[name] = coerced_value(setting, value)
end
validate_config
options
end
@@ -81,10 +84,12 @@
raise Chronicle::ETL::ConnectorConfigurationError, "Missing options: #{missing}" if missing.count.positive?
end
def coerced_value(setting, value)
setting.type ? __send__("coerce_#{setting.type}", value) : value
+ rescue StandardError
+ raise(Chronicle::ETL::ConnectorConfigurationError, "Could not coerce #{value} into a #{setting.type}")
end
def coerce_string(value)
value.to_s
end
@@ -101,14 +106,18 @@
value
end
end
def coerce_time(value)
- # TODO: handle durations like '3h'
- if value.is_a?(String)
- Time.parse(value)
+ return value unless value.is_a?(String)
+
+ # Hacky check for duration strings like "60m"
+ if value.match(/[a-z]+/)
+ ChronicDuration.raise_exceptions = true
+ duration_ago = ChronicDuration.parse(value)
+ Time.now - duration_ago
else
- value
+ Time.parse(value)
end
end
end
# Class methods for classes that have Configurable mixed in