Class: Wrappix::Templates::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappix/templates/object.rb

Class Method Summary collapse

Class Method Details

.render(module_name, _config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wrappix/templates/object.rb', line 6

def self.render(module_name, _config)
  <<~RUBY
    # frozen_string_literal: true

    require "ostruct"

    module #{module_name}
      class Object < OpenStruct
        def initialize(attributes)
          super(to_ostruct(attributes))
        end

        def to_ostruct(obj)
          if obj.is_a?(Hash)
            OpenStruct.new(obj.transform_values { |val| to_ostruct(val) })
          elsif obj.is_a?(Array)
            obj.map { |o| to_ostruct(o) }
          else
            obj
          end
        end
      end
    end
  RUBY
end