Sha256: 29482f0ace5ac9bbc8e911a6832a3f8e89152c60997a12ee15a89e9d7949ffb5

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

# frozen_string_literal: true

module Sourcescrub
  # Utilities
  module Utils
    # Parse the response. build with object
    module Response
      module_function

      def dynamic_attributes(object, attribute_names, response)
        attribute_names.each do |attr_name|
          attr_value = response.dig(attr_name)

          dynamic_define_method(object, attr_name, attr_value)
        end

        object
      end

      private

      def dynamic_define_method(object, attr_name, attr_value)
        # Manually creates methods for both getter and setter and then
        #   sends a message to the new setter with the attr_value
        object.class.send(:define_method, "#{attr_name}=".to_sym) do |value|
          instance_variable_set('@' + attr_name, value)
        end

        object.class.send(:define_method, attr_name.to_sym) do
          instance_variable_get('@' + attr_name.to_s)
        end

        object.send("#{attr_name}=".to_sym, attr_value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sourcescrub-0.0.2 lib/sourcescrub/utils/response.rb