Class: StrongStruct

Inherits:
SafeStruct show all
Defined in:
lib/r_kit/struct/strong_struct.rb

Class Method Summary collapse

Class Method Details

.new(allowed:, defaults: {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/r_kit/struct/strong_struct.rb', line 3

def self.new allowed:, defaults: {}
  super.tap do |klass|

    klass.send :define_method, :instance_variable_set, ->(name, value) do
      if __class__.allowed.include? name
        super(name, value)
      else
        raise NameError.new("unsafe key `#{ name }' for #{ __class__ }")
      end
    end

  end
end