Sha256: 03a25167ecd0720275fb6819e47b911e74ef283610943843e268d0d9d34c340e

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'tide/api/util'

module Tide
  module API
    # Transforms arrays of hashes into concrete instances of classes.
    #
    # @api private
    #
    class Mapper
      # 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

3 entries across 3 versions & 1 rubygems

Version Path
tide-api-0.4.0 lib/tide/api/mapper.rb
tide-api-0.3.0 lib/tide/api/mapper.rb
tide-api-0.2.0 lib/tide/api/mapper.rb