Class: ConservationGuardian::Budget

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "default", limits: {}) ⇒ Budget

limits: Hash of { category_name => max_value }



8
9
10
11
12
# File 'lib/conservation_guardian/budget.rb', line 8

def initialize(name: "default", limits: {})
  @name   = name
  @limits = limits.transform_keys(&:to_sym)
  freeze
end

Instance Attribute Details

#limitsObject (readonly)

Returns the value of attribute limits.



5
6
7
# File 'lib/conservation_guardian/budget.rb', line 5

def limits
  @limits
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/conservation_guardian/budget.rb', line 5

def name
  @name
end

Instance Method Details

#categoriesObject



25
26
27
# File 'lib/conservation_guardian/budget.rb', line 25

def categories
  @limits.keys
end

#exceeded?(category, value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/conservation_guardian/budget.rb', line 18

def exceeded?(category, value)
  max = limit_for(category)
  return false if max.nil?

  value > max
end

#limit_for(category) ⇒ Object



14
15
16
# File 'lib/conservation_guardian/budget.rb', line 14

def limit_for(category)
  @limits[category.to_sym]
end

#to_hObject



29
30
31
# File 'lib/conservation_guardian/budget.rb', line 29

def to_h
  { name: @name, limits: @limits.dup }
end