Module: Hashifiable

Defined in:
lib/hashifiable.rb,
lib/hashifiable/version.rb

Defined Under Namespace

Modules: Version

Constant Summary collapse

VERSION =
Version.to_s

Instance Method Summary collapse

Instance Method Details

#hashify(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hashifiable.rb', line 3

def hashify(*args)

  ## Defines to_hash method dinamically with the key/values specified
  ## in *args.
  define_method :to_h do
    hash_representation = {}

    args.each do |argument|
      case argument
      when Symbol, String
        ## Calls the specified method on the object and stores it
        ## under the method name.
        hash_representation[argument] = self.send(argument)
      when Hash
        ## Takes the key of the hash passed as the key in the object's
        ## hash, the Proc/lambda is called in the context of the object
        ## to provide the value in the hash.
        argument.each do |name, function|
          hash_representation[name] = instance_exec(&function)
        end
      else raise ArgumentError
      end
    end

    hash_representation
  end

  alias_method :to_hash, :to_h
end