Sha256: 218470350f48461bad0f0a7d065ab776b0840d37abc740808496df9ca8b8af45

Contents?: true

Size: 922 Bytes

Versions: 1

Compression:

Stored size: 922 Bytes

Contents

Method = Struct.new :method do
  def invoke receiver = method.owner, hsh_args
    unless receiver == method.owner or receiver.kind_of? method.owner
      raise TypeError, "no implicit conversion of #{receiver.class} into #{method.owner.name}"
    end
    Invokr.invoke method: method.name, on: receiver, with: hsh_args
  end

  def trim_args hsh_args
    hsh_args.select { |key, _| dependencies.include? key }
  end

  def dependencies
    map_identifiers parameters
  end

  def optional_dependencies
    map_identifiers select_parameters_by_type [:opt, :key]
  end

  def required_dependencies
    map_identifiers select_parameters_by_type [:req, :keyreq]
  end

  def parameters
    method.parameters
  end

  private

  def select_parameters_by_type types
    parameters.select do |type, _| types.include? type end
  end

  def map_identifiers parameters
    parameters.map do |_, identifier| identifier end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
invokr-0.0.4 lib/invokr/method.rb