Class: Glitch::Type

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Type

Returns a new instance of Type.



5
6
7
8
9
10
11
12
13
# File 'lib/type.rb', line 5

def initialize(name, options = {})
  @name = name
  @initial_price = options[:initial_price]
  @multiplier = options[:multiplier]
  @count_available = options[:count_available] || :infinite
  @price_calc = options[:price_calc]
  @description = options[:description]
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/type.rb', line 3

def count
  @count
end

#initial_priceObject (readonly)

Returns the value of attribute initial_price.



3
4
5
# File 'lib/type.rb', line 3

def initial_price
  @initial_price
end

#multiplierObject (readonly)

Returns the value of attribute multiplier.



3
4
5
# File 'lib/type.rb', line 3

def multiplier
  @multiplier
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/type.rb', line 3

def name
  @name
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/type.rb', line 41

def available?
  infinite? || @count_available > 0
end

#descriptionObject



62
63
64
# File 'lib/type.rb', line 62

def description
  @description || '??'
end

#incrementObject



34
35
36
37
38
39
# File 'lib/type.rb', line 34

def increment
  @count = @count + 1
  if @count_available.is_a? Integer
    @count_available = @count_available - 1
  end
end

#info_stringObject



53
54
55
56
57
58
59
60
# File 'lib/type.rb', line 53

def info_string
  string = []
  string << name_with_shortcut
  string << "[#{price} bits]"
  string << "(#{@count}/#{total_available})" if @count > 0
  string << "*#{@multiplier}"
  string.join ' '
end

#name_with_shortcutObject



19
20
21
# File 'lib/type.rb', line 19

def name_with_shortcut
  @name.sub(shortcut, "[#{shortcut}]")
end

#priceObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/type.rb', line 23

def price
  if @price_calc
    @price_calc.call(self)
  else
    [
      @initial_price,
      @initial_price * @count * (@count >= 10 ? 10 : 1) + @count
    ].max
  end
end

#shortcutObject



15
16
17
# File 'lib/type.rb', line 15

def shortcut
  @name[0]
end

#total_availableObject



45
46
47
48
49
50
51
# File 'lib/type.rb', line 45

def total_available
  if infinite?
    '??'
  else
    @count + @count_available
  end
end