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

#description(*args) ⇒ Object

Sets or returns the description of this plan When called without arguments, it returns the description When called with arguments, the description is set to the first argument provided.



85
86
87
88
89
90
91
# File 'lib/planify/plan.rb', line 85

def description(*args)
  unless args.empty?
    @description = args.first
  end

  @description ||= nil
end

#disable_feature(feature_name) ⇒ Object

Disables a feature on this plan.

Parameters:

  • feature_name (String, Symbol)

    The name of the feature to disable



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

def disable_feature(feature_name)
  feature(feature_name, false)
end

#dupPlanify::Plan

Returns a duplicate instance of this plan

Returns:



95
96
97
98
99
100
# File 'lib/planify/plan.rb', line 95

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

  duplicate
end

#enable_feature(feature_name) ⇒ Object

Enables a feature on this plan.

Parameters:

  • feature_name (String, Symbol)

    The name of the feature to enable



36
37
38
# File 'lib/planify/plan.rb', line 36

def enable_feature(feature_name)
  feature(feature_name, true)
end

#feature(feature_name, enabled = true) ⇒ Object

Defines a feature of this plan

Parameters:

  • feature_name (String, Symbol)

    The name of the feature

  • enabled (Boolean) (defaults to: true)

    Sets availability of the feature for this plan



30
31
32
# File 'lib/planify/plan.rb', line 30

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:

  • feature (String, Symbol)

    the feature to check

Returns:

  • (Boolean)

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



56
57
58
# File 'lib/planify/plan.rb', line 56

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:

  • feature (String, Symbol)

    the feature to check

Returns:

  • (Boolean)

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



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

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:

  • limitable (String, Symbol, Class, Object)

    The class to get the limit for

Returns:

  • (Integer, Float::INFINITY)

    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:

  • limitable (String, Symbol, Class, Object)

    The class to set the limit of

  • limit (Integer)

    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:

Returns:

  • (nil)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/planify/plan.rb', line 105

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

  name other_plan.name
  description other_plan.description
  price other_plan.price

  nil
end

#name(*args) ⇒ Object

Sets or returns the name of this plan When called without arguments, it returns the name When called with arguments, the name is set to the first argument provided



74
75
76
77
78
79
80
# File 'lib/planify/plan.rb', line 74

def name(*args)
  unless args.empty?
    @name = args.first
  end

  @name ||= nil
end

#price(*args) ⇒ Object

Sets or returns the price of this plan. When called without arguments, it returns the price. When called with arguments, the price is set to the first argument provided.



63
64
65
66
67
68
69
# File 'lib/planify/plan.rb', line 63

def price(*args)
  unless args.empty?
    @price = args.first
  end
  
  @price ||= 0.00
end