Class: Planify::Limitations

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector, ClassHelper
Defined in:
lib/planify/limitations.rb

Overview

Planify::Limitations is a simple container for Class constants and their associated limits

Instance Method Summary collapse

Methods included from ClassHelper

#normalize_class

Constructor Details

#initialize(limits = {}) ⇒ Limitations



10
11
12
# File 'lib/planify/limitations.rb', line 10

def initialize(limits = {})
  @limits = limits
end

Instance Method Details

#allHash(String,Integer)

Returns all limits defined in this instance



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

def all
  @limits
end

#get(klass, default_limit = Float::INFINITY) ⇒ Integer

Fetches the limit for the given class constant



29
30
31
32
33
34
35
36
37
# File 'lib/planify/limitations.rb', line 29

def get(klass, default_limit = Float::INFINITY)
  key = normalize_class(klass)

  begin
    @limits.fetch(key)
  rescue
    return default_limit
  end
end

#set(klass, limit) ⇒ Integer

Sets the limit for the given class constant

Raises:

  • (ArgumentError)

    if klass does not include module Planify::Limitable



19
20
21
22
23
# File 'lib/planify/limitations.rb', line 19

def set(klass, limit)
  key = normalize_class(klass)
  raise ArgumentError unless constantize(key).include?(Planify::Limitable)
  @limits[key] = limit
end