ricardovaleriano/hashing

View on GitHub
lib/hashing/macros.rb

Summary

Maintainability
A
0 mins
Test Coverage
require "hashing/hasher"

module Hashing
  # Define the class methods that should be available in a 'hasherized ®' class
  # (a class that include {Hashing}).
  module Macros
    # Configures which instance variables will be used to compose the `Hash`
    # generated by `#to_h`
    #
    # @api
    # @param ivars [Array<Symbol>]
    def hasherize(*ivars)
      __hasher.add ivars
    end

    # those methods are private but part of the class api (macros).
    # #TODO: there is a way to document the 'macros' for a class in YARD?
    private :hasherize

    # Receives a `Hash` and uses the strategy configured by `.loading` to
    # (re)create an instance of the 'hasherized ®' class.
    #
    # @param hash [Hash] in a valid form defined by `.hasherize`
    # @return new object
    def from_hash(hash)
      __hasher.load hash
    end

    # Provides the entry point to the object that has the actual logic of
    # serialization/unserialization for {Hashing} instances.
    # The ideia here is to not polute the host class with a bunch of methods
    # included by the {Hashing}. Instead, we just inject the api method
    # {#hasherize}, {#from_hash} and the internally used {#__hasher} method.
    def __hasher
      @__hasher ||= Hasher.new self
    end
  end
end