Module: Looksist::Hashed::ClassMethods
- Defined in:
- lib/looksist/hashed.rb
Instance Method Summary collapse
- #__entity__(entity) ⇒ Object
- #do_populate(elt, values, using, as, populate) ⇒ Object
- #extract_values(array_of_hashes, using) ⇒ Object
- #inject(opts) ⇒ Object
- #inject_attributes_at(hash_offset, opts) ⇒ Object
- #inject_attributes_for(array_of_hashes, at, opts) ⇒ Object
- #inject_attributes_for_array(array_of_hashes, at, opts) ⇒ Object
- #inject_class_methods(after) ⇒ Object
- #inject_instance_methods(after) ⇒ Object
- #smart_lookup(array_of_hashes, opts, all_values, at) ⇒ Object
- #update_using_json_path(hash, at, opts) ⇒ Object
Instance Method Details
#__entity__(entity) ⇒ Object
174 175 176 |
# File 'lib/looksist/hashed.rb', line 174 def __entity__(entity) entity.to_s.gsub('_id', '') end |
#do_populate(elt, values, using, as, populate) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/looksist/hashed.rb', line 155 def do_populate(elt, values, using, as, populate) if populate.is_a? Array populate.collect do |_key| alias_method = find_alias(as, _key) parsed_key = JSON.parse(values[elt.with_indifferent_access[using]] || '{}').deep_symbolize_keys elt[alias_method] = parsed_key[_key] end else alias_method = find_alias(as, populate) begin json = JSON.parse(values[elt.with_indifferent_access[using]]) elt[alias_method] = json.with_indifferent_access[populate] rescue elt[alias_method] = values[elt.with_indifferent_access[using]] end end end |
#extract_values(array_of_hashes, using) ⇒ Object
120 121 122 123 |
# File 'lib/looksist/hashed.rb', line 120 def extract_values(array_of_hashes, using) hash = array_of_hashes.is_a?(Array) ? {:root => array_of_hashes} : array_of_hashes hash.find_all_values_for(using) end |
#inject(opts) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/looksist/hashed.rb', line 10 def inject(opts) raise 'Incorrect usage' unless [:after, :using, :populate].all? { |e| opts.keys.include? e } after = opts[:after] @rules ||= {} (@rules[after] ||= []) << opts unless @rules[after].length > 1 if self.singleton_methods.include?(after) inject_class_methods(after) else inject_instance_methods(after) end end end |
#inject_attributes_at(hash_offset, opts) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/looksist/hashed.rb', line 58 def inject_attributes_at(hash_offset, opts) return hash_offset if hash_offset.nil? or hash_offset.empty? opts.each do |opt| keys = hash_offset[opt[:using]] entity_name = __entity__(opt[:bucket_name] || opt[:using]) values = Looksist.redis_service.send("#{entity_name}_for", keys) if opt[:populate].is_a? Array opt[:populate].each do |elt| if values.is_a?(Array) value_hash = values.each_with_object([]) do |i, acc| acc << JSON.parse(i || '{}').deep_symbolize_keys[elt] end else value_hash = JSON.parse(values || '{}').deep_symbolize_keys[elt] end alias_method = find_alias(opt[:as], elt) hash_offset[alias_method] = value_hash end else alias_method = find_alias(opt[:as], opt[:populate]) hash_offset[alias_method] = values end end hash_offset end |
#inject_attributes_for(array_of_hashes, at, opts) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/looksist/hashed.rb', line 99 def inject_attributes_for(array_of_hashes, at, opts) all_values = opts.each_with_object({}) do |opt, acc| entity_name = __entity__(opt[:bucket_name] || opt[:using]) keys = (array_of_hashes.collect { |i| i[opt[:using]] }).compact.uniq values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))] acc[opt[:using]] = values end smart_lookup(array_of_hashes, opts, all_values, nil) end |
#inject_attributes_for_array(array_of_hashes, at, opts) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/looksist/hashed.rb', line 109 def inject_attributes_for_array(array_of_hashes, at, opts) all_values = opts.each_with_object({}) do |opt, acc| entity_name = __entity__(opt[:bucket_name] || opt[:using]) modified_array = extract_values(array_of_hashes, opt[:using]) keys = modified_array.flatten.compact.uniq values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))] acc[opt[:using]] = values end smart_lookup(array_of_hashes, opts, all_values, at) end |
#inject_class_methods(after) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/looksist/hashed.rb', line 42 def inject_class_methods(after) define_singleton_method("#{after}_with_inject") do |*args| hash = send("#{after}_without_inject".to_sym, *args) rules_group_by_at = @rules[after].group_by { |e| e[:at] } rules_group_by_at.each do |at, opts| if at.nil? or at.is_a? String hash = update_using_json_path(hash, at, opts) else inject_attributes_at(hash[at], opts) end end hash end self.singleton_class.send(:alias_method_chain, after, :inject) end |
#inject_instance_methods(after) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/looksist/hashed.rb', line 26 def inject_instance_methods(after) define_method("#{after}_with_inject") do |*args| hash = send("#{after}_without_inject".to_sym, *args) rules_group_by_at = self.class.instance_variable_get(:@rules)[after].group_by { |e| e[:at] } rules_group_by_at.each do |at, opts| if at.nil? or at.is_a? String hash = self.class.update_using_json_path(hash, at, opts) else self.class.inject_attributes_at(hash[at], opts) end end hash end alias_method_chain after, :inject end |
#smart_lookup(array_of_hashes, opts, all_values, at) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/looksist/hashed.rb', line 125 def smart_lookup(array_of_hashes, opts, all_values, at) ## populate is not a array array_of_hashes.collect do |elt| if at.present? JsonPath.for(elt.with_indifferent_access).gsub!(at) do |node| if node.is_a? Array node.each do |x| opts.each do |opt| values = all_values[opt[:using]] do_populate(x, values, opt[:using], opt[:as], opt[:populate]) end end else opts.each do |opt| values = all_values[opt[:using]] do_populate(node, values, opt[:using], opt[:as], opt[:populate]) end end node end.to_hash.deep_symbolize_keys else opts.each do |opt| values = all_values[opt[:using]] do_populate(elt, values, opt[:using], opt[:as], opt[:populate]) end elt end end end |
#update_using_json_path(hash, at, opts) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/looksist/hashed.rb', line 84 def update_using_json_path(hash, at, opts) if hash.is_a?(Hash) if at.present? JsonPath.for(hash.with_indifferent_access).gsub!(at) do |i| i.is_a?(Array) ? inject_attributes_for(i, at, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?) i end else inject_attributes_at(hash, opts) end.to_hash.deep_symbolize_keys else inject_attributes_for_array(hash, at, opts) end end |