Class: MaimaiNet::Model::Either

Inherits:
Object
  • Object
show all
Includes:
Variant, MaimaiNet::ModuleExt::MethodCache
Defined in:
lib/maimai_net/model-typing.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MaimaiNet::ModuleExt::MethodCache

included, #singleton_method_added

Methods included from MaimaiNet::ModuleExt::HaveClassMethods

#class_method, #included

Methods included from Variant

included

Constructor Details

#initialize(variants) ⇒ Either

Returns a new instance of Either.



127
128
129
130
# File 'lib/maimai_net/model-typing.rb', line 127

def initialize(variants)
  @variants = variants.freeze
  freeze
end

Class Method Details

.[](*variants) ⇒ Either

defines an either class statement

Returns:



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/maimai_net/model-typing.rb', line 166

def [](*variants)
  variants.uniq!
  return variants.first if variants.size == 1

  @_list ||= {}
  sorted_hash = variants.sort_by(&:object_id).hash
  gen = @_list.fetch(sorted_hash, nil)
  return gen unless gen.nil?

  gen = new(variants)
  @_list[sorted_hash] ||= gen
  gen
end

Instance Method Details

#===(obj) ⇒ Object



138
139
140
# File 'lib/maimai_net/model-typing.rb', line 138

def ===(obj)
  variants.any? do |variant| variant === obj end
end

#to_sObject Also known as: inspect



142
143
144
145
146
147
148
149
150
# File 'lib/maimai_net/model-typing.rb', line 142

def to_s
  prefix = nil
  prefix = 'Optional' if variants.include? NilClass

  "%s(%s)" % [
    prefix,
    clean_variants.join('|'),
  ]
end

#variantsObject



153
154
155
# File 'lib/maimai_net/model-typing.rb', line 153

def variants
  @variants
end