Sha256: b067e546a83464a04e8783acd53e4570cd3677cfb9eae7b650dd669b709dd4e7

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Vedeu

  module Repositories

    # Some classes expect a certain set of attributes when
    # initialized, this module uses the #defaults method of the class
    # to provide missing attribute keys (and values).
    #
    # @api private
    #
    module Defaults

      # Returns a new instance of the class including this module.
      #
      # @note
      #   If a particular key is missing from the attributes
      #   parameter, then it is added with the respective value from
      #   #defaults.
      #
      # @param attributes [Hash]
      # @return [void] A new instance of the class including this
      #   module.
      def initialize(attributes = {})
        fail Vedeu::Error::InvalidSyntax,
             'Argument :attributes is not a Hash.' unless attributes.is_a?(Hash)

        attrs = attributes.keep_if { |key, _| defaults.key?(key) }

        defaults.merge!(attrs).each do |key, value|
          instance_variable_set("@#{key}", value || defaults.fetch(key))
        end
      end

      private

      # @return [Hash<void>]
      def defaults
        {}
      end

    end # Defaults

  end # Repositories

end # Vedeu

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.8.0 lib/vedeu/repositories/defaults.rb