Sha256: 886cc231b3995bd4745fad6e21859da97188f415eb7d53fd539d9bb5463af196

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Micro::Struct
  class Factory
    class Members
      attr_reader :required_and_optional

      Names = ->(values) { NormalizeNames::AsSymbols.(values, context: 'member') }

      def initialize(required_members, required_option, optional_option)
        @required = Names[required_members] + Names[required_option]
        @optional = Names[optional_option]

        @required_and_optional = @required + @optional
      end

      ToEval = ::Struct.new(:required, :optional, :required_and_optional) do
        def keyword_args
          required_kargs = "#{required.join(':, ')}#{':' unless required.empty?}"
          optional_kargs = "#{optional.join(': nil, ')}#{': nil' unless optional.empty?}"

          [required_kargs, optional_kargs].reject(&:empty?).join(', ')
        end

        def positional_args
          required_and_optional.join(', ')
        end
      end

      def to_eval
        @to_eval ||= ToEval.new(@required, @optional, required_and_optional)
      end
    end

    private_constant :Members
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
u-struct-1.0.0 lib/micro/struct/factory/members.rb
u-struct-0.12.0 lib/micro/struct/factory/members.rb
u-struct-0.11.0 lib/micro/struct/factory/members.rb