lib/yap/column_mapper.rb in yap-1.4.2 vs lib/yap/column_mapper.rb in yap-2.0.0

- old
+ new

@@ -1,14 +1,39 @@ module ColumnMapper + attr_reader :_api_aliases + def api_aliases(aliases = nil, &block) + raise "Aliases already defined. Make sure to invoke #{__method__} only once." if defined? @_api_aliases + + api_aliases_hash(aliases) if aliases.present? + + if block_given? + raise ArgumentError, 'Only one of the following allowed: Hash of aliases or block.' if aliases.present? + + @_api_aliases = block + end + + nil + end + + def api_aliases_hash(aliases) + raise ArgumentError, "Expected first argument to be a Hash, got #{aliases.class.name}." unless aliases.is_a? Hash + + @_api_aliases = aliases.symbolize_keys + + nil + end + def map_column(name) actual_column = name if column_names.include?(name) - column_alias = if respond_to? :map_name_to_column - map_name_to_column(name) - elsif actual_column.nil? && !Yap::DEFAULTS.disable_warnings - warn "#{self.name} does not implement map_name_to_column. If you do not need column mapping set " \ - 'disable_warnings=true' + column_alias = if _api_aliases.is_a? Hash + _api_aliases[name.to_sym] + elsif _api_aliases.is_a? Proc + _api_aliases.call(name) end - column_alias || actual_column + result = column_alias || actual_column + return if result.blank? + + result.to_s end end