Class: MaimaiNet::Model::Generic
- Inherits:
-
Object
- Object
- MaimaiNet::Model::Generic
- Includes:
- Variant, MaimaiNet::ModuleExt::MethodCache
- Defined in:
- lib/maimai_net/model-typing.rb
Overview
defines a generic typing
Class Method Summary collapse
-
.[](cls, *variants) ⇒ Generic
defines a generic class statement.
Instance Method Summary collapse
- #===(obj) ⇒ Object
-
#initialize(cls, variants) ⇒ Generic
constructor
A new instance of Generic.
- #to_class ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #variants ⇒ Object
Methods included from MaimaiNet::ModuleExt::MethodCache
included, #singleton_method_added
Methods included from MaimaiNet::ModuleExt::HaveClassMethods
Methods included from Variant
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
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_class ⇒ Object
99 100 101 |
# File 'lib/maimai_net/model-typing.rb', line 99 def to_class @class end |
#to_s ⇒ Object 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 |
#variants ⇒ Object
102 103 104 |
# File 'lib/maimai_net/model-typing.rb', line 102 def variants @variants end |