Module: Hash19::Resolvers
- Included in:
- Core
- Defined in:
- lib/hash19/resolvers.rb
Instance Method Summary collapse
- #resolve_aliases ⇒ Object
- #resolve_associations(hash, associations, type) ⇒ Object
- #resolve_class(assoc_name) ⇒ Object
- #resolve_has_many(hash) ⇒ Object
- #resolve_has_one(hash) ⇒ Object
- #resolve_injections(hash) ⇒ Object
Instance Method Details
#resolve_aliases ⇒ Object
36 37 38 39 40 |
# File 'lib/hash19/resolvers.rb', line 36 def resolve_aliases self.class.aliases.each do |key, as| @hash19[as] = @hash19.delete(key) if @hash19.has_key?(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(lazy: true) elsif type == :many association.map { |hash| klass.send(:new, hash).to_h(lazy: true) } end else unless opts[:trigger] puts "warning: Association:<#{name}> is not present in #{self.class.name}. Possible specify a trigger" 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 = full_class_name.gsub(full_class_name.demodulize, 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hash19/resolvers.rb', line 42 def resolve_injections(hash) together do self.class.injections.each do |opts| async do entries = JsonPath.new(opts[:at]).on(hash).flatten ids = entries.map { |el| el[opts[:using]] }.compact next unless ids.present? to_inject = opts[:trigger].call(ids).map(&:with_indifferent_access) key = opts[:as] || opts[:using].to_s.gsub(/_id$|Id$/, '') entries.each do |entry| id = entry.delete(opts[:using]) next unless id.present? target = to_inject.find { |el| el[opts[:reference] || opts[:using]] == id } entry[key] = target end end end end end |