Sha256: 8036cfdc440ee576c6bba2a4648b2d8c4b224a4b1956c9eb58db845dc3a1e2f4

Contents?: true

Size: 979 Bytes

Versions: 2

Compression:

Stored size: 979 Bytes

Contents

module PageEz
  class Parameters
    def self.build(block)
      if block&.respond_to?(:parameters) && block&.respond_to?(:arity)
        new(block)
      else
        NullParameters.new
      end
    end

    def keyword_args
      @block.parameters.filter_map do |type, name|
        param = Parameter.new(type, name)
        param.name if param.kwarg?
      end
    end

    def non_keyword_args
      @block.parameters.filter_map do |type, name|
        param = Parameter.new(type, name)
        param.name if !param.kwarg?
      end
    end

    private_class_method :new

    def initialize(block)
      @block = block
    end

    class NullParameters
      def keyword_args
        []
      end

      def non_keyword_args
        []
      end
    end

    class Parameter
      attr_reader :name

      def initialize(type, name)
        @type = type
        @name = name
      end

      def kwarg?
        @type == :keyreq || @type == :key
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_ez-0.2.0 lib/page_ez/parameters.rb
page_ez-0.1.0 lib/page_ez/parameters.rb