Class: TraitType

Inherits:
Object
  • Object
show all
Defined in:
lib/kittyverse/traits.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ TraitType

Returns a new instance of TraitType.



184
185
186
# File 'lib/kittyverse/traits.rb', line 184

def initialize( **kwargs )
  update( kwargs )
end

Instance Attribute Details

#cattributesObject

Returns the value of attribute cattributes.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def cattributes
  @cattributes
end

#codeObject

Returns the value of attribute code.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def code
  @code
end

#genesObject

Returns the value of attribute genes.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def genes
  @genes
end

#keyObject

Returns the value of attribute key.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def key
  @key
end

#nameObject

Returns the value of attribute name.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def name
  @name
end

#traitsObject

Returns the value of attribute traits.



177
178
179
# File 'lib/kittyverse/traits.rb', line 177

def traits
  @traits
end

Class Method Details

.[](key_or_index_or_offset, length = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kittyverse/traits.rb', line 140

def self.[]( key_or_index_or_offset, length=nil )
  if length    ## returns a slice of trait types
    offset = key_or_index_or_offset
    @@trait_types_by_key.values[offset, length]
  else
    if key_or_index_or_offset.is_a? Integer
      index = key_or_index_or_offset
      @@trait_types_by_key.values[index]
    else
      key = key_or_index_or_offset
      if key.size == 2 && key =~ /^[A-Za-z]{2}$/ ## check for codes e.g. FU, PA, ... (or fu, pa,...)
        TraitType.find_by_code( key )
      else
        if key.is_a? Symbol    ## e.g. :body, :pattern, etc.
          TraitType.find_by_key( key )
        else ## assume string
          TraitType.find_by_name( key )
        end
      end
    end
  end
end

.eachObject



163
164
165
166
167
# File 'lib/kittyverse/traits.rb', line 163

def self.each
  @@trait_types_by_key.each do |(key,type)|
    yield( type )
  end
end

.each_with_indexObject



169
170
171
172
173
# File 'lib/kittyverse/traits.rb', line 169

def self.each_with_index
  @@trait_types_by_key.each_with_index do |(key,type),i|
    yield( type,i )
  end
end

.find_by(**kwargs) ⇒ Object

add “generic” convenience find helper



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kittyverse/traits.rb', line 127

def self.find_by( **kwargs )
  if kwargs[ :key ]
     find_by_key( kwargs[ :key ] )
  elsif kwargs[ :code ]
     find_by_code( kwargs[ :code ] )
   elsif kwargs[ :name ]
      find_by_name( kwargs[ :name ] )
  else
    ## todo/fix: throw argument except!!!
    nil
  end
end

.find_by_code(code) ⇒ Object



109
110
111
112
113
114
# File 'lib/kittyverse/traits.rb', line 109

def self.find_by_code( code )
  ## note: allow string AND symbols (thus, use .to_s)
  ##  e.g. allow 'FU', 'fu', :fu, :FU, etc.
  code = code.to_s.upcase
  @@trait_types_by_code[ code ]
end

.find_by_key(key) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/kittyverse/traits.rb', line 100

def self.find_by_key( key )
  ## note: allow string AND symbols (thus, use .to_sym !!!)
  ## note: allow  known alternative mappings/key (e.g. "internal" cryptokitties keys if different)
  key = key.to_sym
  key = ALT_TRAIT_TYPE_KEYS[ key ]  if ALT_TRAIT_TYPE_KEYS[ key ]

  @@trait_types_by_key[ key]
end

.find_by_name(name) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/kittyverse/traits.rb', line 116

def self.find_by_name( name )
  ## note: downcase name e.g. allow fur too (not just Fur)
  ## note: allow  known alternative mappings/key (e.g. "internal" cryptokitties keys if different)
  name = name.to_s.downcase
  name = ALT_TRAIT_TYPE_NAMES[ name ]  if ALT_TRAIT_TYPE_NAMES[ name ]

  @@trait_types_by_name[ name ]
end

.sizeObject

todo: add length alias too? why? why not?



175
# File 'lib/kittyverse/traits.rb', line 175

def self.size() @@trait_types_by_key.size; end

.trait_types_by_codeObject



96
# File 'lib/kittyverse/traits.rb', line 96

def self.trait_types_by_code() @@trait_types_by_code ||= {}; end

.trait_types_by_keyObject



95
# File 'lib/kittyverse/traits.rb', line 95

def self.trait_types_by_key()  @@trait_types_by_key ||= {}; end

.trait_types_by_nameObject



97
# File 'lib/kittyverse/traits.rb', line 97

def self.trait_types_by_name() @@trait_types_by_name ||= {}; end

Instance Method Details

#[](key) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/kittyverse/traits.rb', line 195

def [](key)
  if key.is_a? Integer   ## assume 0,1,2,3,.. index
    traits[ key ]
  elsif key.size == 2 && key =~ /^[0-9]{2}$/   ## assume code e.g. '00', '01', .. etc.
    traits[ key.to_i(10) ]
  else   ## assume kai char
    traits[ Kai::NUMBER[key] ]
  end
end

#update(**kwargs) ⇒ Object



188
189
190
191
192
193
# File 'lib/kittyverse/traits.rb', line 188

def update( **kwargs )
  kwargs.each do |name,value|
    send( "#{name}=", value ) ## use "regular" plain/classic attribute setter
  end
  self   ## return self for chaining
end