Class: Product

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/galaxy/models/product.rb

Instance Method Summary collapse

Constructor Details

#initialize(match, state) ⇒ Product

Returns a new instance of Product.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/galaxy/models/product.rb', line 20

def initialize match, state
  case match.size
    when 7 then # This is a ship design
    super({:name=>match[0], :drive=>match[1].to_f, :guns=>match[2].to_i, :weapons=>match[3].to_f, 
      :shields=>match[4].to_f, :cargo=>match[5].to_f, :mass=>match[6].to_f, :prod_type=>'ship'})
    
    when 5 then # This is a science
    super({:name=>match[0], :drive=>match[1].to_f, :weapons=>match[2].to_f, :shields=>match[3].to_f, 
      :cargo=>match[4].to_f, :prod_type=>'research'})
    
    when 0 then # This is Cap/Mat/Col/Drive/Weapon/Shields/Cargo init, all init data given in a hash
    super state # :name=>state[:name], :prod_type=>state[:prod_type]
  end
  # Add this Product to appropriate Race collections it belongs to
  # auto-delete from previous collection should be handled by Race class through collection callbacks
  state[:race].products << self if state[:race]
  add if self.class.dataset # Add instantiated model to dataset if it is defined
end

Instance Method Details

#<=>(other) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/galaxy/models/product.rb', line 63

def <=>(other)
  case other
    when nil then 1
    when Race then race ? race <=> other : -1
    when Product then (ship? and mass != other.mass) ? mass <=> other.mass : key <=> other.key
    when Bombing then bombings.any? {|r| r == other} ? 0 : 1 #Product is bigger than Bombing
    when Planet then planets.any? {|r| r == other} ? 0 : 1 #Product is bigger than Planet
    when Integer then ship? ? mass <=> other : 1 #Science is bigger than anything
    when Float then ship? ? mass <=> other : 1 #Science is bigger than anything
    when String then self <=> other.downcase.to_sym
    when Symbol then 
    return drone? ? 0 : 1 if other == :drone or other == :dron
    return research? ? 0 : 1 if other == :research or other == :science or other == :tech 
    return moving? ? 0 : 1 if other == :move or other == :moving 
    return terraforming? ? 0 : 1 if other == :terraform or other == :terraforming
    return 0 if prod_type.downcase.to_sym == other
    return 0 if key.downcase.include? other.to_s
    key <=> other.to_s
  else raise ArgumentError, 'Comparison with a wrong type'
  end
end

#cap?Boolean

Boolean tests on Products

Returns:

  • (Boolean)


46
# File 'lib/galaxy/models/product.rb', line 46

def cap? ; prod_type == 'cap' end

#drone?Boolean

Returns:

  • (Boolean)


56
# File 'lib/galaxy/models/product.rb', line 56

def drone? ; drive == 1 and mass == 1 end

#in_use?Boolean

Returns:

  • (Boolean)


60
# File 'lib/galaxy/models/product.rb', line 60

def in_use? ; !bombings.empty? or !planets.empty? or !groups.empty?  end

#keyObject



39
40
41
42
43
# File 'lib/galaxy/models/product.rb', line 39

def key
  full_name = research? ? name + '_Research' : name
  full_name = race.name + '.' + full_name if race 
  full_name
end

#mat?Boolean

Returns:

  • (Boolean)


48
# File 'lib/galaxy/models/product.rb', line 48

def mat? ; prod_type == 'mat' end

#moving?Boolean

Returns:

  • (Boolean)


52
# File 'lib/galaxy/models/product.rb', line 52

def moving? ; name[0..3].downcase == 'move' end

#research?Boolean Also known as: science?

Returns:

  • (Boolean)


58
# File 'lib/galaxy/models/product.rb', line 58

def research? ; prod_type == 'research' end

#ship?Boolean

Returns:

  • (Boolean)


54
# File 'lib/galaxy/models/product.rb', line 54

def ship? ; mass != nil end

#terraforming?Boolean

Returns:

  • (Boolean)


50
# File 'lib/galaxy/models/product.rb', line 50

def terraforming? ; research? and name == '_TerraForming' end