Module: Looksist::Hashed::ClassMethods

Defined in:
lib/looksist/hashed.rb

Instance Method Summary collapse

Instance Method Details

#inject(opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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
    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 = update_using_json_path(hash, opts).to_hash.deep_symbolize_keys
        else
          inject_attributes_at(hash[opts[:at]], opts)
        end
      end
      hash
    end
    alias_method_chain after, :inject
  end
end