Sha256: 1b78c951f7d6374c4f2ee63aa99fc136cf44f0e850c418c3dbd78c468724a079

Contents?: true

Size: 1.23 KB

Versions: 272

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/module/delegation'

module EacRubyUtils
  class CommonConstructor
    class SuperArgs
      attr_reader :class_initialize, :args, :object
      delegate :common_constructor, to: :class_initialize

      def initialize(class_initialize, args, object)
        @class_initialize = class_initialize
        @args = args
        @object = object
      end

      def auto_result
        r = []
        sub_args.each do |name, value|
          i = super_arg_index(name)
          r[i] = value if i
        end
        r
      end

      def result
        result_from_options || auto_result
      end

      def result_from_options
        return unless common_constructor.super_args

        object.instance_exec(&common_constructor.super_args)
      end

      def sub_args
        common_constructor.args.each_with_index.map do |name, index|
          [name, args[index]]
        end.to_h
      end

      def super_arg_index(name)
        super_method.parameters.each_with_index do |arg, index|
          return index if arg[1] == name
        end
        nil
      end

      def super_method
        class_initialize.klass.superclass&.instance_method(:initialize)
      end
    end
  end
end

Version data entries

272 entries across 272 versions & 4 rubygems

Version Path
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_ruby_utils-0.117.1 lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.69.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb
eac_tools-0.68.0 sub/eac_ruby_utils/lib/eac_ruby_utils/common_constructor/super_args.rb