Sha256: 4b69819b8af42bbbce3eb776b02d4e4223f5be4afb8f488738cae7f5f05ba4e7

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: false

if Lite::Ruby.configuration.monkey_patches.include?('open_struct')
  require 'ostruct'

  class OpenStruct

    def initialize(hash = nil, &block)
      @table = if block && block.arity == 2
                 Hash.new(&block)
               else
                 {}
               end

      hash&.each do |key, val|
        @table[key.to_sym] = val
        new_ostruct_member!(key)
      end

      yield self if block && block.arity == 1
    end

    def [](key)
      key = key.to_sym unless key.is_a?(Symbol)
      @table[key]
    end

    def []=(key, val)
      raise TypeError, "can't modify frozen #{self.class}", caller(1) if frozen?

      key = key.to_sym unless key.is_a?(Symbol)
      @table[key] = val
    end

    def attributes
      @table
    end

    def replace(args)
      args.each_pair { |key, val| send("#{key}=", val) }
    end

    def to_hash(table: true)
      return attributes unless table

      { table: attributes }
    end

    def to_json(table: true)
      to_hash(table: table).to_json
    end

    alias as_json to_json
    alias to_h to_hash

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lite-ruby-1.1.9 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.8 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.7 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.6 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.5 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.4 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.3 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.2 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.1 lib/lite/ruby/open_struct.rb
lite-ruby-1.1.0 lib/lite/ruby/open_struct.rb