Class: Genome

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Genome

Returns a new instance of Genome.



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

def initialize( arg )
  if arg.is_a? Hash
    hash = arg       ## assumes (pre-built) hash with genes
    @genes = hash
  else
    if arg.is_a? Integer   ## use Integer (Fixnum+Bignum??) - why? why not?
      num = arg
      kai = Kai.encode( num )
    else              # else assume string in kai/base32 format
      kai = arg.dup  # just in case; make a clean (fresh) copy
      kai = kai.gsub( ' ', '' )   ## allow spaces (strip/remove)
    end
    ## puts "Genome.initialize #{kai}"
    build_genes( kai )
  end
end

Instance Attribute Details

#genesObject (readonly)

hash of genes (key is gene type)



5
6
7
# File 'lib/copycats/genome.rb', line 5

def genes
  @genes
end

Instance Method Details

#bodyObject



40
# File 'lib/copycats/genome.rb', line 40

def body()      TRAITS[:body][:kai][ @genes[:body].d ]; end

#build_genes(kai) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/copycats/genome.rb', line 24

def build_genes( kai )
  kai = kai.reverse    ## note: reserve for easy left-to-right access
  @genes = {}   ## hash of genes (key is gene type)
  ## fix/todo: use as_json for "official" api order
  ## note: use insert order from "official" api
  @genes[:body]      = Gene.new( kai[0,4].reverse )
  @genes[:pattern]   = Gene.new( kai[4,4].reverse )
  @genes[:coloreyes] = Gene.new( kai[8,4].reverse )
  @genes[:eyes]      = Gene.new( kai[12,4].reverse )
  @genes[:color1]    = Gene.new( kai[16,4].reverse )  ## colorprimary / body color / base color
  @genes[:color2]    = Gene.new( kai[20,4].reverse )  ## colorsecondary / sec color / pattern color /  hi(light) color
  @genes[:color3]    = Gene.new( kai[24,4].reverse )  ## colortertiary / acc(ent) color
  @genes[:wild]      = Gene.new( kai[28,4].reverse )
  @genes[:mouth]     = Gene.new( kai[32,4].reverse )
end

#build_mix_tables(other) ⇒ Object



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

def build_mix_tables( other )  GenomeMixTables.new( self, other ).build; end

#build_tablesObject



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

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

#color1Object



45
# File 'lib/copycats/genome.rb', line 45

def color1()    TRAITS[:color1][:kai][ @genes[:color1].d ]; end

#color2Object



46
# File 'lib/copycats/genome.rb', line 46

def color2()    TRAITS[:color2][:kai][ @genes[:color2].d ]; end

#color3Object



47
# File 'lib/copycats/genome.rb', line 47

def color3()    TRAITS[:color3][:kai][ @genes[:color3].d ]; end

#coloreyesObject



41
# File 'lib/copycats/genome.rb', line 41

def coloreyes() TRAITS[:coloreyes][:kai][ @genes[:coloreyes].d ]; end

#eyesObject



42
# File 'lib/copycats/genome.rb', line 42

def eyes()      TRAITS[:eyes][:kai][ @genes[:eyes].d ]; end

#genes_color1Object

rename to color1_genes instead - why? why not?



51
# File 'lib/copycats/genome.rb', line 51

def genes_color1() @genes[:color1]; end

#genes_eyesObject



52
# File 'lib/copycats/genome.rb', line 52

def genes_eyes()   @genes[:eyes];   end

#mix(other) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/copycats/genome.rb', line 58

def mix( other )
  mgenes = genes          ## matron genes
  sgenes = other.genes    ## sire genes
  new_genes  = {}

  [:body,
   :pattern,
   :coloreyes,
   :eyes,
   :color1,
   :color2,
   :color3,
   :wild,
   :mouth].each do |key|
    mgene = mgenes[key]
    sgene = sgenes[key]

    new_gene = mgene.mix( sgene )
    new_genes[key] = new_gene
  end

  Genome.new( new_genes )   ## return new genome from (pre-built) hash (with genes)
end

#mouthObject



44
# File 'lib/copycats/genome.rb', line 44

def mouth()     TRAITS[:mouth][:kai][ @genes[:mouth].d ]; end

#patternObject



43
# File 'lib/copycats/genome.rb', line 43

def pattern()   TRAITS[:pattern][:kai][ @genes[:pattern].d ]; end