Module: ImmutableStructExRedactable::RedactedAccessible::ClassModules

Defined in:
lib/immutable_struct_ex_redactable/redacted_accessible.rb

Instance Method Summary collapse

Instance Method Details

#redacted_accessible_module_for(hash:, config:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/immutable_struct_ex_redactable/redacted_accessible.rb', line 12

def redacted_accessible_module_for(hash:, config:)
  Module.new do
    if config.whitelist.any?
      hash.each_key do |attr|
        next if config.whitelist.include? attr

        unredacted_attr_method = "unredacted_#{attr}"
        code = <<~CODE
          def #{unredacted_attr_method}
            "#{hash[attr]}"
          end
          private :#{unredacted_attr_method}
        CODE
        class_eval code
      end
    else
      config.blacklist.each do |attr|
        unredacted_attr_method = "unredacted_#{attr}"
        code = <<~CODE
          def #{unredacted_attr_method}
            "#{hash[attr]}"
          end
          private :#{unredacted_attr_method}
        CODE
        class_eval code
      end
    end
  end
end