Class: Genome

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

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Genome

Returns a new instance of Genome.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kittyverse/genome.rb', line 6

def initialize( arg )
  if arg.is_a? Integer   ## use Integer (Fixnum+Bignum??) - why? why not?
    num = arg
    kai = Kai.encode( num )
  else
    if arg.downcase.start_with?( '0x' )  ## assume hexstring( base16 )
      kai = Kai.encode( arg.to_i(16) )
    else # else assume string in kai/base32 format
      kai = arg
      kai = kai.gsub( ' ', '' )   ## allow spaces (strip/remove)
    end
  end
  ## puts "Genome.initialize #{kai}"

  @kai   = kai    ## note: store/save kai without any spaces ("compact" format)
  @genes = build_genes( kai )  ## array of (sliced) genes (block of four genes)
end

Instance Method Details

#[](key) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kittyverse/genome.rb', line 165

def [](key)
  if key.is_a? Integer   ## assume 0,1,2,3,.. index
    q , r = key.divmod(4)   ## q=quotient, r=rest/modulus
    ##  e.g.   3.divmod(4) => [0,3]
    ##         4.divmod(4) => [1,0]
    ##         5.divmod(4) => [1,1]  etc.
    @genes[q][r]
  else   ## assume trait type key / symbol
    @genes[index(key)]
  end
end

#binaryObject Also known as: bin



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kittyverse/genome.rb', line 34

def binary
  @kai.chars.each_slice(4).map do |slice|
    buf = ""
    buf << Kai::BINARY[slice[0]]
    buf << "-"
    buf << Kai::BINARY[slice[1]]
    buf << "-"
    buf << Kai::BINARY[slice[2]]
    buf << "-"
    buf << Kai::BINARY[slice[3]]
    buf
  end.join( " " )
end

#bodyObject Also known as: fu

Fur (FU)



75
# File 'lib/kittyverse/genome.rb', line 75

def body()           @genes[0]; end

#build_genes(kai) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kittyverse/genome.rb', line 50

def build_genes( kai )
  kai = kai.reverse    ## note: reserve for easy left-to-right access
  genes = []   ## array of (sliced) genes (block of four genes)
  ## fix/todo: use as_json for "official" api order
  ## note: use insert order from "official" api

##    genes << Gene::Slice.new( :body, kai[0],
##                                     kai[1],
##                                     kai[2],
##                                     kai[3] )
##    genes << Gene::Slice.new( :pattern, kai[4+0],
##                                        kai[4+1],
##                                        kai[4+2],
##                                        kai[4+3]] )

  keys.each_with_index do |key,i|
     genes << Gene::Slice.new( key, kai[4*i+0],
                                    kai[4*i+1],
                                    kai[4*i+2],
                                    kai[4*i+3])
  end
  genes
end

#build_tablesObject



178
# File 'lib/kittyverse/genome.rb', line 178

def build_tables()   GenomeTables.new( self ).build;  end

#bytesObject



26
# File 'lib/kittyverse/genome.rb', line 26

def bytes()  Kai.bytes( @kai ); end

#coloreyesObject Also known as: ec

Eyes Color (EC)



77
# File 'lib/kittyverse/genome.rb', line 77

def coloreyes()      @genes[2]; end

#colorprimaryObject Also known as: color1, bc

Base Color (BC)



79
# File 'lib/kittyverse/genome.rb', line 79

def colorprimary()   @genes[4]; end

#colorsecondaryObject Also known as: color2, hc

Highlight Color (HC)



80
# File 'lib/kittyverse/genome.rb', line 80

def colorsecondary() @genes[5]; end

#colortertiaryObject Also known as: color3, ac

Accent Color (AC)



81
# File 'lib/kittyverse/genome.rb', line 81

def colortertiary()  @genes[6]; end

#eachObject Also known as: each_slice



108
# File 'lib/kittyverse/genome.rb', line 108

def each()            @genes.each { |slice| yield(slice) }; end

#each_geneObject



113
114
115
116
117
118
119
120
# File 'lib/kittyverse/genome.rb', line 113

def each_gene
  @genes.each do |slice|
    yield(slice.p)
    yield(slice.r1)
    yield(slice.r2)
    yield(slice.r3)
  end
end

#each_gene_with_indexObject



121
122
123
124
125
126
127
128
# File 'lib/kittyverse/genome.rb', line 121

def each_gene_with_index
  @genes.each_with_index do |slice,i|
    yield(slice.p,  4*i+0)
    yield(slice.r1, 4*i+1)
    yield(slice.r2, 4*i+2)
    yield(slice.r3, 4*i+3)
  end
end

#each_with_indexObject Also known as: each_slice_with_index



109
# File 'lib/kittyverse/genome.rb', line 109

def each_with_index() @genes.each_with_index { |slice,i| yield(slice,i) }; end

#electrologicaObject Also known as: codes

return formatted in electrologica base32 format



31
# File 'lib/kittyverse/genome.rb', line 31

def electrologica() Electrologica.fmt( Electrologica.encode( num ) ); end

#environmentObject Also known as: en

Environment (EN)



84
# File 'lib/kittyverse/genome.rb', line 84

def environment()    @genes[9]; end

#eyesObject Also known as: es

Eyes Shape (ES)



78
# File 'lib/kittyverse/genome.rb', line 78

def eyes()           @genes[3]; end

#index(key) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kittyverse/genome.rb', line 145

def index( key )
  if key.size == 2 && key =~ /^[A-Za-z]{2}$/ ## check for codes e.g. FU, PA, ... (or fu, pa,...)
    key = key.upcase.to_sym
    @@codes_by_index ||= %w(FU PA EC ES BC HC AC WE MO EN SE PU)
                         .each_with_index.reduce({}) do |h,(code,i)|
                                                       h[code.to_sym]=i; h
                                                     end
    @@codes_by_index[ key ]
  else
    key = key.downcase.to_sym
    key = ALT_TRAIT_TYPE_KEYS[ key ]  if ALT_TRAIT_TYPE_KEYS[ key ]

    @@keys_by_index ||= keys.each_with_index.reduce({}) do |h,(key,i)|
                                                          h[key]=i; h
                                                        end
    @@keys_by_index[ key ]
  end
end

#kaiObject

return formatted in blocks of 4



25
# File 'lib/kittyverse/genome.rb', line 25

def kai()    Kai.fmt( @kai ); end

#keysObject

rename to trait_type_keys - why? why not?



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kittyverse/genome.rb', line 130

def keys  ##  rename to trait_type_keys - why? why not?
  [:body,      ### todo/fix: use TRAITS.keys or something - why? why not?
   :pattern,
   :coloreyes,
   :eyes,
   :colorprimary,
   :colorsecondary,
   :colortertiary,
   :wild,
   :mouth,
   :environment,
   :secret,
   :prestige]
end

#mouthObject Also known as: mo

Mouth (MO)



83
# File 'lib/kittyverse/genome.rb', line 83

def mouth()          @genes[8]; end

#numObject Also known as: to_i



28
# File 'lib/kittyverse/genome.rb', line 28

def num()    Kai.decode( @kai ); end

#patternObject Also known as: pa

Pattern (PA)



76
# File 'lib/kittyverse/genome.rb', line 76

def pattern()        @genes[1]; end

#prestigeObject Also known as: pu

Purrstige (PU)



86
# File 'lib/kittyverse/genome.rb', line 86

def prestige()       @genes[11]; end

#secretObject Also known as: se

Secret (SE)



85
# File 'lib/kittyverse/genome.rb', line 85

def secret()         @genes[10]; end

#wildObject Also known as: we

Wild Element (WE)



82
# File 'lib/kittyverse/genome.rb', line 82

def wild()           @genes[7]; end