Module: Hash19::Resolvers

Included in:
Core
Defined in:
lib/hash19/resolvers.rb

Instance Method Summary collapse

Instance Method Details

#resolve_aliasesObject



36
37
38
39
40
41
# File 'lib/hash19/resolvers.rb', line 36

def resolve_aliases
  self.class.aliases.each do |key, as|
    @hash19[as] = @hash19[key] if @hash19.has_key?(key)
    @hash19.delete(key) unless self.class.aliases.values.include?(key)
  end
end

#resolve_associations(hash, associations, type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hash19/resolvers.rb', line 12

def resolve_associations(hash, associations, type)
  associations.each do |name, opts|
    class_name = name.to_s.camelize
    association = hash[opts[:key] || name]
    if association.present?
      klass = resolve_class(class_name.singularize)
      @hash19[opts[:alias] || name] = if type == :one
                                        klass.send(:new, association).to_h(true)
                                      elsif type == :many
                                        association.map { |hash| klass.send(:new, hash).to_h(true) }
                                      end
    else
      unless opts[:trigger]
        # puts "warning: Association:<#{name}> is not present in #{self.class.name}. Probably a trigger is missing!" 
        next
      end
      puts "warning: Key:<#{opts[:using]}> not present in #{self.class.name}. Cannot map association:<#{name}>" unless @hash19.has_key? opts[:using]
      if opts[:trigger] and @hash19.has_key? opts[:using]
        @hash19[opts[:alias] || name] = LazyValue.new(-> { opts[:trigger].call(@hash19.delete(opts[:using])) })
      end
    end
  end
end

#resolve_class(assoc_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hash19/resolvers.rb', line 62

def resolve_class(assoc_name)
  full_class_name = self.class.name
  new_class = construct_association_class full_class_name, assoc_name
  new_class.split('::').inject(Object) do |mod, class_name|
    begin
      mod.const_get(class_name)
    rescue NameError
      raise("Class:<#{new_class}> not defined! Unable to resolve association:<#{assoc_name.downcase}>")
    end
  end
end

#resolve_has_many(hash) ⇒ Object



8
9
10
# File 'lib/hash19/resolvers.rb', line 8

def resolve_has_many(hash)
  resolve_associations(hash, self.class.many_assocs, :many)
end

#resolve_has_one(hash) ⇒ Object



4
5
6
# File 'lib/hash19/resolvers.rb', line 4

def resolve_has_one(hash)
  resolve_associations(hash, self.class.one_assocs, :one)
end

#resolve_injections(hash) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/hash19/resolvers.rb', line 53

def resolve_injections(hash)
  together do
    async_injections.each do |opts|
      async { call_and_inject(opts, hash) }
    end
  end
  synchronous_injections.each { |opts| call_and_inject(opts, hash) }
end

#resolve_transformersObject



43
44
45
46
47
48
49
50
51
# File 'lib/hash19/resolvers.rb', line 43

def resolve_transformers
  self.class.transformers.each do |attribute, proc|
    if proc.is_a?(Proc)
      @hash19[attribute] = proc.call(@hash19[attribute])
    else
      raise "[Attribute: #{attribute}] The transform function should be a lambda!"
    end
  end
end