Class: Hasherize

Inherits:
Module
  • Object
show all
Defined in:
lib/hasherize.rb

Overview

Provides some sugar syntax to declare which ‘ivars` should be used to represent an object as a `Hash`.

It respects all the behavior you will get by including Hashing. In fact, using this constructor is a shortcut to ‘include Hashing`, and call `.hasherize`

Examples:

shortcut to ‘include Hashing` and call `.hasherize`

class File
  include Hasherize.new :path, :commit
end

configuring :to_hash and :from_hash strategies

class File
  include Hasherize.new :content,
    to_hash: ->(content) { Base64.encode64 content },
    from_hash: ->(content_string) { Base64.decode64 content_string }
end

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initialize(*ivars_and_options) ⇒ Hasherize

Stores the ivars and options to be repassed to ‘Hashing.serialize` by the hook #included

Parameters:

  • ivars_and_options (*args)

    ivar names and options (‘:to_hash` and `:from_hash`)

Since:

  • 0.0.1



28
29
30
# File 'lib/hasherize.rb', line 28

def initialize(*ivars_and_options)
  @ivars = ivars_and_options
end

Instance Method Details

#included(serializable_class) ⇒ Object

Includes the ‘Hashing` module and calls Hashing.hasherize, repassing the ivar names an the options received in the constructor

Since:

  • 0.0.1



34
35
36
37
38
39
# File 'lib/hasherize.rb', line 34

def included(serializable_class)
  serializable_class.module_eval do
    include Hashing
  end
  serializable_class.send :hasherize, *@ivars
end