Module: JSI::Util::Private Private

Extended by:
Private
Included in:
JSI::Util, Private
Defined in:
lib/jsi/util/private.rb,
lib/jsi/util/private/memo_map.rb,
lib/jsi/util/private/attr_struct.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

JSI::Util::Private classes, modules, constants, and methods are internal, and will be added and removed without warning.

Defined Under Namespace

Modules: FingerprintHash Classes: AttrStruct, MemoMap

Constant Summary collapse

EMPTY_ARY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[].freeze
EMPTY_HASH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{}.freeze
EMPTY_SET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Set[].freeze
CLASSES_ALWAYS_FROZEN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Set[TrueClass, FalseClass, NilClass, Integer, Float, BigDecimal, Rational, Symbol].freeze
LAST_ARGUMENT_AS_KEYWORD_PARAMETERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

is a hash as the last argument passed to keyword params? (false in ruby 3; true before - generates a warning in 2.7 but no way to make 2.7 behave like 3 so the warning is useless)

TODO remove eventually (keyword argument compatibility)

begin
  if Object.const_defined?(:Warning)
    warn = ::Warning.instance_method(:warn)
    ::Warning.send(:remove_method, :warn)
    ::Warning.send(:define_method, :warn) { |*, **| }
  end

  -> (k: ) { k }[{k: nil}]
  true
rescue ArgumentError
  false
ensure
  if Object.const_defined?(:Warning)
    ::Warning.send(:remove_method, :warn)
    ::Warning.send(:define_method, :warn, warn)
  end
end
USE_TO_JSON_METHOD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

we won't use #to_json on classes where it is defined by JSON::Ext::Generator::GeneratorMethods / JSON::Pure::Generator::GeneratorMethods this is a bit of a kluge and disregards any singleton class to_json, but it will do.

Hash.new do |h, klass|
  h[klass] = klass.method_defined?(:to_json) &&
    klass.instance_method(:to_json).owner.name !~ /\AJSON:.*:GeneratorMethods\b/
end
RUBY_REJECT_NAME_CODEPOINTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  0..31, # C0 control chars
  %q( !"#$%&'()*+,-./:;<=>?@[\\]^`{|}~).each_codepoint, # printable special chars (note: "_" not included)
  127..159, # C1 control chars
].inject(Set[], &:merge).freeze
RUBY_REJECT_NAME_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regexp.new('[' + Regexp.escape(RUBY_REJECT_NAME_CODEPOINTS.to_a.pack('U*')) + ']+').freeze

Instance Method Summary collapse

Instance Method Details

#const_name_from_parts(parts, join: '') ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jsi/util/private.rb', line 71

def const_name_from_parts(parts, join: '')
  parts = parts.map do |part|
    part = part.dup
    part[/\A[^a-zA-Z]*/] = ''
    part[0] = part[0].upcase if part[0]
    part.gsub!(RUBY_REJECT_NAME_RE, '_')
    part
  end
  if !parts.all?(&:empty?)
    parts.reject(&:empty?).join(join).freeze
  else
    nil
  end
end

#ok_ruby_method_name?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

is the given name ok to use as a ruby method name?

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/jsi/util/private.rb', line 60

def ok_ruby_method_name?(name)
  # must be a string
  return false unless name.respond_to?(:to_str)
  # must not begin with a digit
  return false if name =~ /\A[0-9]/
  # must not contain special or control characters
  return false if name =~ RUBY_REJECT_NAME_RE

  return true
end

#require_jmespathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jsi/util/private.rb', line 117

def require_jmespath
  return if instance_variable_defined?(:@jmespath_required)
  begin
    require 'jmespath'
  rescue ::LoadError => e
    # :nocov:
    msg = [
      "please install and/or add to your Gemfile the `jmespath` gem to use this. jmespath is not a dependency of JSI.",
      "original error message:",
      e.message,
    ].join("\n")
    raise(e.class, msg, e.backtrace)
    # :nocov:
  end
  hashlike = JSI::SchemaSet[].new_jsi({'test' => 0})
  unless JMESPath.search('test', hashlike) == 0
    # :nocov:
    raise(::LoadError, [
      "the loaded version of jmespath cannot be used with JSI.",
      "jmespath is compatible with JSI objects as of version 1.5.0",
    ].join("\n"))
    # :nocov:
  end
  @jmespath_required = true
  nil
end

#uri(uri) ⇒ Addressable::URI

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

string or URI → frozen URI

Returns:

  • (Addressable::URI)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jsi/util/private.rb', line 88

def uri(uri)
  if uri.is_a?(Addressable::URI)
    if uri.frozen?
      uri
    else
      uri.dup.freeze
    end
  else
    Addressable::URI.parse(uri).freeze
  end
end

#ycombObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

this is the Y-combinator, which allows anonymous recursive functions. for a simple example, to define a recursive function to return the length of an array:

length = ycomb do |len|
  proc { |list| list == [] ? 0 : 1 + len.call(list[1..-1]) }
end

length.call([0])
# => 1

see https://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator and chapter 9 of the little schemer, available as the sample chapter at https://felleisen.org/matthias/BTLS-index.html



113
114
115
# File 'lib/jsi/util/private.rb', line 113

def ycomb
  proc { |f| f.call(f) }.call(proc { |f| yield proc { |*x| f.call(f).call(*x) } })
end