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

Returns a new instance of 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

Returns:

  • (Hash(String,Integer))

    A hash mapping class constant name to it’s limit



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

Parameters:

  • klass (String, Symbol, Class, Object)

    The class to find the limit for

  • default_limit (Integer) (defaults to: Float::INFINITY)

    The default value to return, if one is not defined for klass

Returns:

  • (Integer)

    The limit value for klass, or default_limit if none exists.



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

Parameters:

  • klass (String, Symbol, Class, Object)

    The class to set the limit for

  • limit (Integer)

    The number of instances of klass which can be created

Returns:

  • (Integer)

    The value of limit

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