Sha256: 0a7ff7fcfc10ba081fc1d29397a8e68e730c099ce7309fe83535dd5ad522299c

Contents?: true

Size: 1.08 KB

Versions: 15

Compression:

Stored size: 1.08 KB

Contents

require 'loqate/util'

module Loqate
  module Mappers
    # Transforms arrays of hashes into concrete instances of classes.
    #
    # @api private
    #
    class GenericMapper
      # Creates objects from an API response. The objects will be an instance of +object_class+.
      #
      # @param [Class] object_class Class of the final objects
      # @param [Array<Hash>] items Array of attributes to instantiate the final objects
      #
      # @return An array of objects of the given class
      #
      def map(items, object_class)
        items.map { |item| map_one(item, object_class) }
      end

      # Creates a single object from an API response. The object will be an instance of +object_class+.
      #
      # @param [Class] object_class Class of the final object
      # @param [Hash] item Attributes to instantiate the final object
      #
      # @return An object of the given class
      #
      def map_one(item, object_class)
        attributes = item.transform_keys { |attribute| Util.underscore(attribute) }
        object_class.new(attributes)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
loqate-0.12.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.11.1 lib/loqate/mappers/generic_mapper.rb
loqate-0.11.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.10.4 lib/loqate/mappers/generic_mapper.rb
loqate-0.10.3 lib/loqate/mappers/generic_mapper.rb
loqate-0.10.2 lib/loqate/mappers/generic_mapper.rb
loqate-0.10.1 lib/loqate/mappers/generic_mapper.rb
loqate-0.10.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.9.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.8.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.7.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.6.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.5.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.4.0 lib/loqate/mappers/generic_mapper.rb
loqate-0.3.0 lib/loqate/mappers/generic_mapper.rb