Class: MaimaiNet::Model::Generic

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

Overview

defines a generic typing

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(cls, variants) ⇒ Generic

Returns a new instance of Generic.



71
72
73
74
75
# File 'lib/maimai_net/model-typing.rb', line 71

def initialize(cls, variants)
  @class    = cls
  @variants = variants.freeze
  freeze
end

Class Method Details

.[](cls, *variants) ⇒ Generic

defines a generic class statement

Returns:



111
112
113
114
115
116
117
118
119
# File 'lib/maimai_net/model-typing.rb', line 111

def [](cls, *variants)
  @_list ||= {}
  gen = @_list.fetch(cls, []).find do |gen_| gen_.variants.eql?(variants) end
  return gen unless gen.nil?

  gen = new(cls, variants)
  (@_list[cls] ||= []) << gen
  gen
end

Instance Method Details

#===(obj) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/maimai_net/model-typing.rb', line 83

def ===(obj)
  class_match    = @class === obj
  fail NotImplementedError, "#{obj.class} does not support generic" unless obj.respond_to?(:generic_of?)
  internal_match = obj.generic_of?(variants)

  class_match && internal_match
end

#to_classObject



99
100
101
# File 'lib/maimai_net/model-typing.rb', line 99

def to_class
  @class
end

#to_sObject Also known as: inspect



91
92
93
94
95
96
# File 'lib/maimai_net/model-typing.rb', line 91

def to_s
  "%s[%s]" % [
    to_class.name,
    variants.join(', '),
  ]
end

#variantsObject



102
103
104
# File 'lib/maimai_net/model-typing.rb', line 102

def variants
  @variants
end