class TraitType

Attributes

cattributes[RW]
code[RW]
genes[RW]
key[RW]
name[RW]
traits[RW]

Public Class Methods

[]( key_or_index_or_offset, length=nil ) click to toggle source
# 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
each() { |type| ... } click to toggle source
# File lib/kittyverse/traits.rb, line 163
def self.each
  @@trait_types_by_key.each do |(key,type)|
    yield( type )
  end
end
each_with_index() { |type,i| ... } click to toggle source
# 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 ) click to toggle source

add “generic” convenience find helper

# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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
new( **kwargs ) click to toggle source
# File lib/kittyverse/traits.rb, line 184
def initialize( **kwargs )
  update( kwargs )
end
size() click to toggle source
# File lib/kittyverse/traits.rb, line 175
def self.size() @@trait_types_by_key.size; end
trait_types_by_code() click to toggle source
# File lib/kittyverse/traits.rb, line 96
def self.trait_types_by_code() @@trait_types_by_code ||= {}; end
trait_types_by_key() click to toggle source
# File lib/kittyverse/traits.rb, line 95
def self.trait_types_by_key()  @@trait_types_by_key ||= {}; end
trait_types_by_name() click to toggle source
# File lib/kittyverse/traits.rb, line 97
def self.trait_types_by_name() @@trait_types_by_name ||= {}; end

Public Instance Methods

[](key) click to toggle source
# 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 ) click to toggle source
# 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