Class: XTwitterScraper::Internal::Type::ArrayOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter, Util::SorbetRuntimeSupport
Defined in:
lib/x_twitter_scraper/internal/type/array_of.rb,
sig/x_twitter_scraper/internal/type/array_of.rbs

Overview

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

This class is abstract.

Array of items of a given type.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type

Methods included from Converter

coerce, dump, inspect, meta_info, new_coerce_state, type_info

Constructor Details

#initialize(type_info, spec = {}) ⇒ ArrayOf

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.

Returns a new instance of ArrayOf.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, XTwitterScraper::Internal::Type::Converter, Class)
  • spec (Hash{Symbol=>Object}) (defaults to: {})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"



153
154
155
156
157
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 153

def initialize(type_info, spec = {})
  @item_type_fn = XTwitterScraper::Internal::Type::Converter.type_info(type_info || spec)
  @meta = XTwitterScraper::Internal::Type::Converter.meta_info(type_info, spec)
  @nilable = spec.fetch(:nil?, false)
end

Class Method Details

.[](type_info, spec = {}) ⇒ self

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.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, XTwitterScraper::Internal::Type::Converter, Class)
  • spec (Hash{Symbol=>Object})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"

Returns:

  • (self)


38
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 38

def self.[](...) = new(...)

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



52
53
54
55
56
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 52

def ==(other)
  # rubocop:disable Layout/LineLength
  other.is_a?(XTwitterScraper::Internal::Type::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type
  # rubocop:enable Layout/LineLength
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



45
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 45

def ===(other) = other.is_a?(Array) && other.all?(item_type)

#coerce(value, state:) ⇒ Array<Object>, 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.

Parameters:

  • value (Array<Object>, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

Returns:

  • (Array<Object>, Object)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 80

def coerce(value, state:)
  exactness = state.fetch(:exactness)

  unless value.is_a?(Array)
    exactness[:no] += 1
    state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
    return value
  end

  target = item_type
  exactness[:yes] += 1
  value
    .map do |item|
      case [nilable?, item]
      in [true, nil]
        exactness[:yes] += 1
        nil
      else
        XTwitterScraper::Internal::Type::Converter.coerce(target, item, state: state)
      end
    end
end

#dump(value, state:) ⇒ Array<Object>, 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.

Parameters:

  • value (Array<Object>, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Array<Object>, Object)


112
113
114
115
116
117
118
119
120
121
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 112

def dump(value, state:)
  target = item_type
  if value.is_a?(Array)
    value.map do
      XTwitterScraper::Internal::Type::Converter.dump(target, _1, state: state)
    end
  else
    super
  end
end

#hashInteger

Returns:

  • (Integer)


61
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 61

def hash = [self.class, item_type].hash

#inspect(depth: 0) ⇒ String

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.

Parameters:

  • depth (Integer) (defaults to: 0)
  • depth: (Integer) (defaults to: 0)

Returns:

  • (String)


164
165
166
167
168
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 164

def inspect(depth: 0)
  items = XTwitterScraper::Internal::Type::Converter.inspect(item_type, depth: depth.succ)

  "#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]"
end

#item_typegeneric<Elem>

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.

Returns:

  • (generic<Elem>)


133
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 133

protected def item_type = @item_type_fn.call

#nilable?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.

Returns:



138
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 138

protected def nilable? = @nilable

#to_sorbet_typeObject

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.

Returns:

  • (Object)


126
127
128
# File 'lib/x_twitter_scraper/internal/type/array_of.rb', line 126

def to_sorbet_type
  T::Array[XTwitterScraper::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(item_type)]
end