Class: Planify::Plan

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

Overview

The Planify::Plan class provides functionality for storing class creation limits and available features. It also embodies a simple DSL for defining these attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limits = {}, features = {}) ⇒ Plan

Returns a new instance of Plan.



8
9
10
11
# File 'lib/planify/plan.rb', line 8

def initialize(limits = {}, features = {})
  @limits = Limitations.new(limits)
  @features = features
end

Instance Attribute Details

#featuresObject (readonly)

Returns the value of attribute features.



6
7
8
# File 'lib/planify/plan.rb', line 6

def features
  @features
end

#limitsObject (readonly)

Returns the value of attribute limits.



6
7
8
# File 'lib/planify/plan.rb', line 6

def limits
  @limits
end

Instance Method Details

#dupPlanify::Plan

Returns a duplicate instance of this plan

Returns:

  • an exact copy of this plan



47
48
49
50
51
52
# File 'lib/planify/plan.rb', line 47

def dup
  duplicate = Plan.new
  duplicate.merge! self

  duplicate
end

#feature(feature_name, enabled = true) ⇒ Object



27
28
29
# File 'lib/planify/plan.rb', line 27

def feature(feature_name, enabled = true) 
  @features[feature_name.to_sym] = enabled
end

#feature_disabled?(feature) ⇒ Boolean

Boolean method for determining if a certain feature is disabled in this plan

Parameters:

  • the feature to check

Returns:

  • true if feature is disabled or undefined, false if feature is enabled.



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

def feature_disabled?(feature)
  !feature_enabled?(feature)
end

#feature_enabled?(feature) ⇒ Boolean

Boolean method for determining if a certain feature is enabled in this plan

Parameters:

  • the feature to check

Returns:

  • true if feature is enabled, false if feature is disabled or undefined.



34
35
36
# File 'lib/planify/plan.rb', line 34

def feature_enabled?(feature)
  @features[feature.to_sym] || false
end

#limit(limitable) ⇒ Integer, Float::INFINITY

Gets the plan’s limit for a given class constant

Parameters:

  • The class to get the limit for

Returns:

  • The plan limit for limitable, if one exists. Otherwise Float::INFINITY



23
24
25
# File 'lib/planify/plan.rb', line 23

def limit(limitable)
  @limits.get(limitable)
end

#max(limitable, limit) ⇒ Object

Sets the maximum number of a Planify::Limitable that a user can create on this plan

Parameters:

  • The class to set the limit of

  • The maxiumum number of limitable that can be created



16
17
18
# File 'lib/planify/plan.rb', line 16

def max(limitable, limit)
  @limits.set(limitable, limit)
end

#merge!(other_plan) ⇒ nil

Merges limits and features from other_plan into self.

Parameters:

  • the plan to merge with

Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/planify/plan.rb', line 57

def merge!(other_plan)
  other_plan.features.each do |f, enabled|
    feature f, enabled
  end

  other_plan.limits.all.each do |klass, lim|
    max klass, lim
  end

  nil
end