lib/active_data.rb in active_data-1.0.0 vs lib/active_data.rb in active_data-1.1.0

- old
+ new

@@ -8,12 +8,16 @@ require 'active_model' require 'active_data/version' require 'active_data/errors' require 'active_data/extensions' +require 'active_data/undefined_class' require 'active_data/config' require 'active_data/railtie' if defined? Rails +require 'active_data/model' +require 'active_data/model/associations/persistence_adapters/base' +require 'active_data/model/associations/persistence_adapters/active_record' module ActiveData BOOLEAN_MAPPING = { 1 => true, 0 => false, @@ -30,37 +34,33 @@ 'TRUE' => true, 'FALSE' => false, 'y' => true, 'n' => false, 'yes' => true, - 'no' => false, - } + 'no' => false + }.freeze def self.config ActiveData::Config.instance end - singleton_class.delegate *ActiveData::Config.delegated, to: :config + singleton_class.delegate(*ActiveData::Config.delegated, to: :config) typecaster('Object') { |value, attribute| value if value.class < attribute.type } - typecaster('String') { |value| value.to_s } + typecaster('String') { |value, _| value.to_s } typecaster('Array') do |value| case value when ::Array then value when ::String then value.split(',').map(&:strip) - else - nil end end typecaster('Hash') do |value| case value when ::Hash then value - else - nil end end typecaster('Date') do |value| begin value.to_date @@ -87,19 +87,40 @@ when ActiveSupport::TimeZone value when ::TZInfo::Timezone ActiveSupport::TimeZone[value.name] when String, Numeric, ActiveSupport::Duration - value = Float(value) rescue value + value = begin + Float(value) + rescue ArgumentError, TypeError + value + end ActiveSupport::TimeZone[value] - else + end + end + typecaster('BigDecimal') do |value| + next unless value + begin + ::BigDecimal.new Float(value).to_s + rescue ArgumentError, TypeError nil end end - typecaster('BigDecimal') { |value| ::BigDecimal.new Float(value).to_s rescue nil if value } - typecaster('Float') { |value| Float(value) rescue nil } - typecaster('Integer') { |value| Float(value).to_i rescue nil } + typecaster('Float') do |value| + begin + Float(value) + rescue ArgumentError, TypeError + nil + end + end + typecaster('Integer') do |value| + begin + Float(value).to_i + rescue ArgumentError, TypeError + nil + end + end typecaster('Boolean') { |value| BOOLEAN_MAPPING[value] } typecaster('ActiveData::UUID') do |value| case value when UUIDTools::UUID ActiveData::UUID.parse_raw value.raw @@ -107,20 +128,22 @@ value when String ActiveData::UUID.parse_string value when Integer ActiveData::UUID.parse_int value - else - nil end end end -require 'active_data/model' +require 'active_data/base' ActiveSupport.on_load :active_record do require 'active_data/active_record/associations' require 'active_data/active_record/nested_attributes' include ActiveData::ActiveRecord::Associations - include ActiveData::ActiveRecord::NestedAttributes + singleton_class.prepend ActiveData::ActiveRecord::NestedAttributes + + def self.active_data_persistence_adapter + ActiveData::Model::Associations::PersistenceAdapters::ActiveRecord + end end