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
# File 'lib/immutable_struct_ex_redactable/redacted_accessible.rb', line 12

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

        class_eval unredacted_method_proc.call(attr: attr, hash: hash)
      end
    else
      config.blacklist.each do |attr|
        class_eval unredacted_method_proc.call(attr: attr, hash: hash)
      end
    end
  end
end

#unredacted_attr_method(attr:, hash:) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/immutable_struct_ex_redactable/redacted_accessible.rb', line 29

def unredacted_attr_method(attr:, hash:)
  unredacted_attr_method = "unredacted_#{attr}"
  <<~CODE
    def #{unredacted_attr_method}
      "#{hash[attr]}"
    end
    private :#{unredacted_attr_method}
  CODE
end