Constant spinoso_env::RUBY_API_POLYFILLS[][src]

pub const RUBY_API_POLYFILLS: &str = "# frozen_string_literal: true\n\nENV = Object.new\n\nclass << ENV\n  def [](name)\n    @backend ||= ::Artichoke::Environ.new\n    @backend[name]\n  end\n\n  def []=(name, value)\n    @backend ||= ::Artichoke::Environ.new\n    @backend[name] = value\n  end\n\n  def assoc(name)\n    name = name.to_str unless name.is_a?(String)\n    value = self[name]\n    return nil if value.nil?\n\n    [name, value]\n  end\n\n  def clear\n    to_h.each do |var_name, var_value|\n      self[var_name] = nil unless var_value.nil?\n    end\n\n    to_h\n  end\n\n  def delete(name)\n    value = self[name]\n    if value.nil?\n      yield name if block_given?\n      return nil\n    end\n\n    self[name] = nil\n    yield name if block_given?\n\n    value\n  end\n\n  def delete_if\n    return to_enum(:delete_if) unless block_given?\n\n    to_h.each do |key, value|\n      delete(key) if yield key, value\n    end\n    to_h\n  end\n\n  def each(&blk)\n    return to_enum(:each) unless block_given?\n\n    to_h.each(&blk)\n  end\n\n  def each_key(&blk)\n    return to_enum(:each_key) unless block_given?\n\n    to_h.each_key(&blk)\n  end\n\n  def each_pair(&blk)\n    return to_enum(:each_pair) unless block_given?\n\n    to_h.each_pair(&blk)\n  end\n\n  def each_value(&blk)\n    return to_enum(:each_value) unless block_given?\n\n    to_h.each_value(&blk)\n  end\n\n  def empty?\n    to_h.empty?\n  end\n\n  def fetch(name, default = (not_set = true))\n    warn \'warning: block supersedes default value argument\' if !not_set && block_given?\n\n    name =\n      if name.is_a?(String)\n        name\n      elsif name.respond_to?(:to_str)\n        converted = name.to_str\n        unless converted.is_a?(String)\n          raise TypeError, \"can\'t convert #{name.class} to String (#{name.class}#to_str gives #{converted.class})\"\n        end\n\n        converted\n      else\n        cls = name.class\n        cls = \'nil\' if name.nil?\n\n        raise TypeError, \"no implicit conversion of #{cls} into String\"\n      end\n\n    value = self[name]\n    return value unless value.nil?\n    return yield name if block_given?\n    return default unless not_set\n\n    raise KeyError.new(\"key not found: #{name.inspect}\", receiver: self, key: name)\n  end\n\n  def filter(&blk)\n    return to_enum(:filter) unless block_given?\n\n    to_h.select(&blk)\n  end\n\n  def filter!(&blk)\n    return to_enum(:filter!) unless block_given?\n\n    select!(&blk)\n  end\n\n  def has_key?(name) # rubocop:disable Naming/PredicateName\n    to_h.key?(name)\n  end\n\n  def has_value?(value) # rubocop:disable Naming/PredicateName\n    to_h.value?(value)\n  end\n\n  def include?(name)\n    to_h.key?(name)\n  end\n\n  def index(value)\n    to_h.key(value)\n  end\n\n  def inspect\n    to_h.to_s\n  end\n\n  def invert\n    to_h.invert\n  end\n\n  def keep_if\n    return to_enum(:keep_if) unless block_given?\n\n    to_h.each do |key, value|\n      delete(key) unless yield key, value\n    end\n    to_h\n  end\n\n  def key(value)\n    to_h.key(value)\n  end\n\n  def key?(name)\n    !self[name].nil?\n  end\n\n  def keys\n    to_h.keys\n  end\n\n  def length\n    to_h.length\n  end\n\n  def member?(name)\n    !self[name].nil?\n  end\n\n  def rassoc(value)\n    value = value.to_str unless value.is_a?(String)\n    to_h.each do |k, v|\n      return [k, v] if v == value\n    end\n    nil\n  end\n\n  def rehash\n    nil\n  end\n\n  def reject(&blk)\n    return to_enum(:reject) unless block_given?\n\n    to_h.delete_if(&blk)\n  end\n\n  def reject!\n    return to_enum(:reject!) unless block_given?\n\n    modified = false\n    to_h.each do |key, value|\n      if yield key, value\n        delete(key)\n        modified = true\n      end\n    end\n\n    return self if modified\n\n    nil\n  end\n\n  def replace(hash)\n    hash.each do |k, v|\n      self[k] = v\n    end\n    select! { |k, _| hash.key?(k) }\n  end\n\n  def select(&blk)\n    return to_enum(:select) unless block_given?\n\n    to_h.select(&blk)\n  end\n\n  def select!(&blk)\n    return to_enum(:select!) unless block_given?\n\n    env = to_h\n    # collect all the keys where the block evaluates to false\n    to_remove = env.reject(&blk)\n\n    # returns nil if no changes were made\n    return nil if to_remove.empty?\n\n    to_remove.each do |key, _|\n      delete(key)\n    end\n    self\n  end\n\n  def shift\n    envs = to_h\n    return nil if envs.nil? || envs.empty?\n\n    name, value = envs.shift\n    self[name] = nil\n    [name, value]\n  end\n\n  def size\n    to_h.size\n  end\n\n  def slice(*keys)\n    to_h.slice(*keys)\n  end\n\n  def store(name, value)\n    self[name] = value\n  end\n\n  def to_a\n    to_h.to_a\n  end\n\n  def to_h\n    @backend ||= ::Artichoke::Environ.new\n    h = @backend.to_h\n    return h unless block_given?\n\n    pairs = h.each_pair.map do |name, value|\n      tx = yield(name, value)\n      if tx.is_a?(Array)\n        raise ArgumentError, \"element has wrong array length (expected 2, was #{tx.length})\" if tx.length != 2\n\n        tx\n      elsif tx.respond_to?(:to_ary)\n        pair = tx.to_ary\n        unless pair.is_a?(Array)\n          raise TypeError, \"can\'t convert #{tx.class} to Array (#{tx.class}#to_ary gives #{pair.class})\"\n        end\n        raise ArgumentError, \"element has wrong array length (expected 2, was #{pair.length})\" if pair.length != 2\n\n        pair\n      else\n        raise TypeError, \"wrong element type #{tx.class} (expected array)\"\n      end\n    end\n    pairs.to_h\n  end\n\n  def to_hash\n    to_h\n  end\n\n  def to_s\n    \'ENV\'\n  end\n\n  def update(hash)\n    hash.each do |key, value|\n      value = yield(key, self[key], value) if block_given? && key?(key)\n      self[key] = value\n    end\n\n    to_h\n  end\n\n  def value?(name)\n    to_h.value?(name)\n  end\n\n  def values\n    to_h.values\n  end\n\n  def values_at(*names)\n    to_h.values_at(*names)\n  end\nend\n";
Expand description

Ruby implementations of the full ENV API.

This crate only implements enough of the ENV API in native code to glue the system APIs to the interpreter. ENV offers a rich, Hash-like API on top of the raw names and values.

This constant contains Ruby source code that implements the remainder of these APIs on top of the core accessors and mutators in this crate.

Ruby Source


ENV = Object.new

class << ENV
  def [](name)
    @backend ||= ::Artichoke::Environ.new
    @backend[name]
  end

  def []=(name, value)
    @backend ||= ::Artichoke::Environ.new
    @backend[name] = value
  end

  def assoc(name)
    name = name.to_str unless name.is_a?(String)
    value = self[name]
    return nil if value.nil?

    [name, value]
  end

  def clear
    to_h.each do |var_name, var_value|
      self[var_name] = nil unless var_value.nil?
    end

    to_h
  end

  def delete(name)
    value = self[name]
    if value.nil?
      yield name if block_given?
      return nil
    end

    self[name] = nil
    yield name if block_given?

    value
  end

  def delete_if
    return to_enum(:delete_if) unless block_given?

    to_h.each do |key, value|
      delete(key) if yield key, value
    end
    to_h
  end

  def each(&blk)
    return to_enum(:each) unless block_given?

    to_h.each(&blk)
  end

  def each_key(&blk)
    return to_enum(:each_key) unless block_given?

    to_h.each_key(&blk)
  end

  def each_pair(&blk)
    return to_enum(:each_pair) unless block_given?

    to_h.each_pair(&blk)
  end

  def each_value(&blk)
    return to_enum(:each_value) unless block_given?

    to_h.each_value(&blk)
  end

  def empty?
    to_h.empty?
  end

  def fetch(name, default = (not_set = true))
    warn 'warning: block supersedes default value argument' if !not_set && block_given?

    name =
      if name.is_a?(String)
        name
      elsif name.respond_to?(:to_str)
        converted = name.to_str
        unless converted.is_a?(String)
          raise TypeError, "can't convert #{name.class} to String (#{name.class}#to_str gives #{converted.class})"
        end

        converted
      else
        cls = name.class
        cls = 'nil' if name.nil?

        raise TypeError, "no implicit conversion of #{cls} into String"
      end

    value = self[name]
    return value unless value.nil?
    return yield name if block_given?
    return default unless not_set

    raise KeyError.new("key not found: #{name.inspect}", receiver: self, key: name)
  end

  def filter(&blk)
    return to_enum(:filter) unless block_given?

    to_h.select(&blk)
  end

  def filter!(&blk)
    return to_enum(:filter!) unless block_given?

    select!(&blk)
  end

  def has_key?(name) # rubocop:disable Naming/PredicateName
    to_h.key?(name)
  end

  def has_value?(value) # rubocop:disable Naming/PredicateName
    to_h.value?(value)
  end

  def include?(name)
    to_h.key?(name)
  end

  def index(value)
    to_h.key(value)
  end

  def inspect
    to_h.to_s
  end

  def invert
    to_h.invert
  end

  def keep_if
    return to_enum(:keep_if) unless block_given?

    to_h.each do |key, value|
      delete(key) unless yield key, value
    end
    to_h
  end

  def key(value)
    to_h.key(value)
  end

  def key?(name)
    !self[name].nil?
  end

  def keys
    to_h.keys
  end

  def length
    to_h.length
  end

  def member?(name)
    !self[name].nil?
  end

  def rassoc(value)
    value = value.to_str unless value.is_a?(String)
    to_h.each do |k, v|
      return [k, v] if v == value
    end
    nil
  end

  def rehash
    nil
  end

  def reject(&blk)
    return to_enum(:reject) unless block_given?

    to_h.delete_if(&blk)
  end

  def reject!
    return to_enum(:reject!) unless block_given?

    modified = false
    to_h.each do |key, value|
      if yield key, value
        delete(key)
        modified = true
      end
    end

    return self if modified

    nil
  end

  def replace(hash)
    hash.each do |k, v|
      self[k] = v
    end
    select! { |k, _| hash.key?(k) }
  end

  def select(&blk)
    return to_enum(:select) unless block_given?

    to_h.select(&blk)
  end

  def select!(&blk)
    return to_enum(:select!) unless block_given?

    env = to_h
    to_remove = env.reject(&blk)

    return nil if to_remove.empty?

    to_remove.each do |key, _|
      delete(key)
    end
    self
  end

  def shift
    envs = to_h
    return nil if envs.nil? || envs.empty?

    name, value = envs.shift
    self[name] = nil
    [name, value]
  end

  def size
    to_h.size
  end

  def slice(*keys)
    to_h.slice(*keys)
  end

  def store(name, value)
    self[name] = value
  end

  def to_a
    to_h.to_a
  end

  def to_h
    @backend ||= ::Artichoke::Environ.new
    h = @backend.to_h
    return h unless block_given?

    pairs = h.each_pair.map do |name, value|
      tx = yield(name, value)
      if tx.is_a?(Array)
        raise ArgumentError, "element has wrong array length (expected 2, was #{tx.length})" if tx.length != 2

        tx
      elsif tx.respond_to?(:to_ary)
        pair = tx.to_ary
        unless pair.is_a?(Array)
          raise TypeError, "can't convert #{tx.class} to Array (#{tx.class}#to_ary gives #{pair.class})"
        end
        raise ArgumentError, "element has wrong array length (expected 2, was #{pair.length})" if pair.length != 2

        pair
      else
        raise TypeError, "wrong element type #{tx.class} (expected array)"
      end
    end
    pairs.to_h
  end

  def to_hash
    to_h
  end

  def to_s
    'ENV'
  end

  def update(hash)
    hash.each do |key, value|
      value = yield(key, self[key], value) if block_given? && key?(key)
      self[key] = value
    end

    to_h
  end

  def value?(name)
    to_h.value?(name)
  end

  def values
    to_h.values
  end

  def values_at(*names)
    to_h.values_at(*names)
  end
end