Sha256: cc2af2c5fd9c2ea77c721cd994b9682a504b2a022cf76199a7b878e9f9bdd82e
Contents?: true
Size: 1.86 KB
Versions: 3
Compression:
Stored size: 1.86 KB
Contents
require 'jsonpath' require 'json' module Looksist module Hashed extend ActiveSupport::Concern include Looksist::Common module ClassMethods def inject(opts) raise 'Incorrect usage' unless [:after, :using, :populate].all? { |e| opts.keys.include? e } after = opts[:after] @rules ||= {} (@rules[after] ||= []) << opts return if @rules[after].length > 1 define_method("#{after}_with_inject") do |*args| hash = send("#{after}_without_inject".to_sym, *args) self.class.instance_variable_get(:@rules)[after].each do |opts| if opts[:at].is_a? String hash = JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i| i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless i.empty? i end.to_hash.deep_symbolize_keys else inject_attributes_at(hash[opts[:at]], opts) end end hash end alias_method_chain after, :inject end end included do |base| base.class_attribute :rules end private def inject_attributes_at(hash_offset, opts) return hash_offset if hash_offset.nil? or hash_offset.empty? keys = hash_offset[opts[:using]] entity_name = __entity__(opts[:using]) values = Looksist.redis_service.send("#{entity_name}_for", keys) hash_offset[opts[:populate]] = values hash_offset end def inject_attributes_for(arry_of_hashes, opts) entity_name = __entity__(opts[:using]) keys = (arry_of_hashes.collect { |i| i[opts[:using]] }).compact.uniq values = keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys)).to_h arry_of_hashes.each do |elt| elt[opts[:populate]] = values[elt[opts[:using]]] end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
looksist-0.1.8 | lib/looksist/hashed.rb |
looksist-0.1.7 | lib/looksist/hashed.rb |
looksist-0.1.6 | lib/looksist/hashed.rb |