Module: Baml::Sorbet::Struct

Defined in:
lib/struct.rb

Overview

Provides dynamic properties on statically defined classes

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol) ⇒ Object

Needed to allow accessing dynamic types



11
12
13
# File 'lib/struct.rb', line 11

def method_missing(symbol)
  @props[symbol]
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
# File 'lib/struct.rb', line 24

def [](key)
  key = key.to_sym if key.is_a?(String)
  @props[key]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/struct.rb', line 15

def eql?(other)
  self.class == other.class &&
    @props.eql?(other.instance_variable_get(:@args))
end

#hashObject



20
21
22
# File 'lib/struct.rb', line 20

def hash
  [self.class, @props].hash
end

#inspectObject



29
30
31
# File 'lib/struct.rb', line 29

def inspect
  PP.pp(self, +"", 79)
end

#pretty_print(pp) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/struct.rb', line 34

def pretty_print(pp)
  pp.object_group(self) do
    pp.breakable
    @props.each do |key, value|
      pp.text "#{key}="
      pp.pp value
      pp.comma_breakable unless key == @props.keys.last
    end
  end
end

#to_h(&block) ⇒ Object

From the ostruct implementation



46
47
48
49
50
51
52
# File 'lib/struct.rb', line 46

def to_h(&block)
  if block
    @props.map(&block).to_h
  else
    @props.dup
  end
end

#to_json(*args) ⇒ Object



54
55
56
# File 'lib/struct.rb', line 54

def to_json(*args)
  @props.to_json(*args)
end