Module: Blocks::HashWithCaller

Defined in:
lib/blocks/utilities/hash_with_caller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callersObject

Returns the value of attribute callers.



5
6
7
# File 'lib/blocks/utilities/hash_with_caller.rb', line 5

def callers
  @callers
end

Instance Method Details

#initialize(*args) ⇒ Object



7
8
9
10
# File 'lib/blocks/utilities/hash_with_caller.rb', line 7

def initialize(*args)
  self.callers = {}
  super
end

#initialize_copy(original) ⇒ Object



12
13
14
15
# File 'lib/blocks/utilities/hash_with_caller.rb', line 12

def initialize_copy(original)
  super
  self.callers = original.callers.clone
end

#reverse_merge!(*args) ⇒ Object

TODO: fix and test this implementation def to_s

description = []

description << "{"
description << map do |key, value|
  value_display = case value
  when Symbol
    ":#{value}"
  when String
    "\"#{value}\""
  when Proc
    "Proc"
  else
    value
  end
  "\"#{key}\" => #{value_display}, # [#{callers[key]}]"
end.join(",\n")
description << "}"
description.join("\n")

end



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/blocks/utilities/hash_with_caller.rb', line 41

def reverse_merge!(*args)
  options = args.extract_options!

  caller_id = args.first.to_s.presence || ""

  if !options.is_a?(HashWithCaller) && Blocks.lookup_caller_location
    caller_location = caller.detect do |c|
      !c.include?("/lib/blocks") &&
      !c.include?("/lib/ruby") &&
      !c.include?("patch")
    end.try(:split, ":in").try(:[], 0)

    caller_id += " from #{caller_location}" if caller_location
  end

  options.each do |key, value|
    current_value = self[key]

    if options.is_a?(HashWithCaller)
      setter = options.callers[key]
    else
      setter = "set by #{caller_id}"
    end

    if !self.key?(key)
      callers[key] = setter

    elsif current_value.is_a?(Hash) && value.is_a?(Hash)
      # self[key] = value.deep_merge(current_value)
      callers[key] = "#{callers[key]}, #{setter}"
    end
  end

  super options
end