Module: Clavatar

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

Overview

Clavatar module

Constant Summary collapse

VERSION =
'0.0.4'

Class Method Summary collapse

Class Method Details

.cast(hash, klass, example) ⇒ Object



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
32
33
34
35
# File 'lib/clavatar.rb', line 5

def self.cast(hash, klass, example)
  accessible_attr = []
  example.instance_variables.each do |attr|
    attr = attr.to_s.sub('@', '').to_sym
    if example.respond_to? ("#{attr}=")
      accessible_attr << attr
    end
  end

  args = {}
  construct_params = example.method(:initialize).parameters
  construct_params.each do |param|
    arg_required = param.first
    arg_name = param.last

    if arg_required == :keyreq
      raise "parameter #{arg_name} is mandatory but missing in #{hash}" unless hash.key? arg_name
    elsif arg_required == :keyrest
      args.merge! hash
      break
    end

    args[arg_name] = hash[arg_name]
  end

  instance = klass.new args
  accessible_attr.each do |attr|
    instance.send("#{attr}=", hash[attr])
  end
  return instance
end