Class: TraitType
- Inherits:
-
Object
- Object
- TraitType
- Defined in:
- lib/kittyverse/traits.rb
Instance Attribute Summary collapse
-
#cattributes ⇒ Object
Returns the value of attribute cattributes.
-
#code ⇒ Object
Returns the value of attribute code.
-
#genes ⇒ Object
Returns the value of attribute genes.
-
#key ⇒ Object
Returns the value of attribute key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#traits ⇒ Object
Returns the value of attribute traits.
Class Method Summary collapse
- .[](key_or_index_or_offset, length = nil) ⇒ Object
- .each ⇒ Object
- .each_with_index ⇒ Object
-
.find_by(**kwargs) ⇒ Object
add “generic” convenience find helper.
- .find_by_code(code) ⇒ Object
- .find_by_key(key) ⇒ Object
- .find_by_name(name) ⇒ Object
-
.size ⇒ Object
todo: add length alias too? why? why not?.
- .trait_types_by_code ⇒ Object
- .trait_types_by_key ⇒ Object
- .trait_types_by_name ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(**kwargs) ⇒ TraitType
constructor
A new instance of TraitType.
- #update(**kwargs) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ TraitType
Returns a new instance of TraitType.
186 187 188 |
# File 'lib/kittyverse/traits.rb', line 186 def initialize( **kwargs ) update( kwargs ) end |
Instance Attribute Details
#cattributes ⇒ Object
Returns the value of attribute cattributes.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def cattributes @cattributes end |
#code ⇒ Object
Returns the value of attribute code.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def code @code end |
#genes ⇒ Object
Returns the value of attribute genes.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def genes @genes end |
#key ⇒ Object
Returns the value of attribute key.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def name @name end |
#traits ⇒ Object
Returns the value of attribute traits.
179 180 181 |
# File 'lib/kittyverse/traits.rb', line 179 def traits @traits end |
Class Method Details
.[](key_or_index_or_offset, length = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/kittyverse/traits.rb', line 142 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 ⇒ Object
165 166 167 168 169 |
# File 'lib/kittyverse/traits.rb', line 165 def self.each @@trait_types_by_key.each do |(key,type)| yield( type ) end end |
.each_with_index ⇒ Object
171 172 173 174 175 |
# File 'lib/kittyverse/traits.rb', line 171 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
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/kittyverse/traits.rb', line 129 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
111 112 113 114 115 116 |
# File 'lib/kittyverse/traits.rb', line 111 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
102 103 104 105 106 107 108 109 |
# File 'lib/kittyverse/traits.rb', line 102 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
118 119 120 121 122 123 124 125 |
# File 'lib/kittyverse/traits.rb', line 118 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 |
.size ⇒ Object
todo: add length alias too? why? why not?
177 |
# File 'lib/kittyverse/traits.rb', line 177 def self.size() @@trait_types_by_key.size; end |
.trait_types_by_code ⇒ Object
98 |
# File 'lib/kittyverse/traits.rb', line 98 def self.trait_types_by_code() @@trait_types_by_code ||= {}; end |
.trait_types_by_key ⇒ Object
97 |
# File 'lib/kittyverse/traits.rb', line 97 def self.trait_types_by_key() @@trait_types_by_key ||= {}; end |
.trait_types_by_name ⇒ Object
99 |
# File 'lib/kittyverse/traits.rb', line 99 def self.trait_types_by_name() @@trait_types_by_name ||= {}; end |
Instance Method Details
#[](key) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/kittyverse/traits.rb', line 197 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
190 191 192 193 194 195 |
# File 'lib/kittyverse/traits.rb', line 190 def update( **kwargs ) kwargs.each do |name,value| send( "#{name}=", value ) ## use "regular" plain/classic attribute setter end self ## return self for chaining end |