Module: ImmutableStructEx

Defined in:
lib/immutable_struct_ex.rb,
lib/immutable_struct_ex/version.rb

Overview

Defines the methods used to create/manage the ImmutableStructEx struct.

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.evaluate(struct) ⇒ Object



30
31
32
# File 'lib/immutable_struct_ex.rb', line 30

def evaluate(struct)
  struct.instance_eval yield
end

.new(**hash, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/immutable_struct_ex.rb', line 8

def new(**hash, &block)
  options_struct = Struct.new(*hash.keys, keyword_init: true, &block)
  options_struct.new(**hash).tap do |struct|
    [:[], *struct.members].each do |method|
      evaluate(struct) do
        <<~RUBY
          undef :"#{method}="
        RUBY
      end
    end
    evaluate(struct) do
      <<~RUBY
        def ==(object)
          return false unless object.respond_to? :to_h

          to_h == object.to_h
        end
      RUBY
    end
  end
end