Sha256: cb6c2e75bc57edc7cd34ce222205c27bb806dfa601c371772cc455e484325d62

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop checks for `OpenStruct.new` calls.
      # Instantiation of an `OpenStruct` invalidates
      # Ruby global method cache as it causes dynamic method
      # definition during program runtime.
      # This could have an effect on performance,
      # especially in case of single-threaded
      # applications with multiple `OpenStruct` instantiations.
      #
      # @example
      #   # bad
      #   class MyClass
      #     def my_method
      #       OpenStruct.new(my_key1: 'my_value1', my_key2: 'my_value2')
      #     end
      #   end
      #
      #   # good
      #   class MyClass
      #     MyStruct = Struct.new(:my_key1, :my_key2)
      #     def my_method
      #       MyStruct.new('my_value1', 'my_value2')
      #     end
      #   end
      #
      class OpenStruct < Cop
        MSG = 'Consider using `Struct` over `OpenStruct` ' \
              'to optimize the performance.'

        def_node_matcher :open_struct, <<~PATTERN
          (send (const {nil? cbase} :OpenStruct) :new ...)
        PATTERN

        def on_send(node)
          open_struct(node) do
            add_offense(node, location: :selector)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-performance-1.7.1 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.7.0 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.6.1 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.6.0 lib/rubocop/cop/performance/open_struct.rb