Class: Gold::Tier

Inherits:
Object
  • Object
show all
Defined in:
app/models/gold/tier.rb

Overview

A tier is level of service that this app offer merchants. Tiers are named, have a series of available features, and have pricing data. Tiers are configured in an app’s config/tiers.yml file.

Constant Summary collapse

CONFIG_FILE =
Rails.root.join("config", "tiers.yml")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tier

Returns a new instance of Tier.

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/gold/tier.rb', line 47

def initialize(options = {})
  raise ArgumentError, "Cannot parse tier: #{options}" unless options.is_a?(Hash)
  raise ArgumentError, "ID must be provided for a tier" unless options["id"]

  @id = options["id"].to_sym
  @parent_id = options["parent_id"]
  @name = options["name"]
  @description = options["description"]
  @locked = options.fetch("locked", false)
  @visible = options.fetch("visible", true)
  @trial_days = options.dig("pricing", "trial_days") || 0
  @monthly_price = BigDecimal(options.dig("pricing", "monthly") || 0)
  @free = options.dig("pricing", "free") || false
  @qualifications = (options["qualifications"] || {}).with_indifferent_access
  @features = (options["features"] || {}).with_indifferent_access
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



41
42
43
# File 'app/models/gold/tier.rb', line 41

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



41
42
43
# File 'app/models/gold/tier.rb', line 41

def id
  @id
end

#monthly_priceObject (readonly)

Returns the value of attribute monthly_price.



41
42
43
# File 'app/models/gold/tier.rb', line 41

def monthly_price
  @monthly_price
end

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'app/models/gold/tier.rb', line 41

def name
  @name
end

#trial_daysObject (readonly)

Returns the value of attribute trial_days.



41
42
43
# File 'app/models/gold/tier.rb', line 41

def trial_days
  @trial_days
end

Class Method Details

.allObject

Returns the defined tiers.



17
18
19
# File 'app/models/gold/tier.rb', line 17

def all
  @all ||= (load_from_yaml || []).map { |tier| Tier.new(tier) }
end

.find(id) ⇒ Object

Returns the tier by ID or nil if that tier cannot be found.



28
29
30
31
32
# File 'app/models/gold/tier.rb', line 28

def find(id)
  return nil if id.nil?

  all.find { |tier| tier.id == id.to_sym }
end

.load_from_yamlObject



12
13
14
# File 'app/models/gold/tier.rb', line 12

def load_from_yaml
  YAML.load_file(CONFIG_FILE)
end

.reset!Object

Exposed for testing to allow clearing the cached tiers. Not necessary for normal application use.



36
37
38
# File 'app/models/gold/tier.rb', line 36

def reset!
  @all = nil
end

.visibleObject

Returns only the visible tiers (ones that customers should see on a pricing comparision page).



23
24
25
# File 'app/models/gold/tier.rb', line 23

def visible
  all.find_all(&:visible?)
end

Instance Method Details

#featuresObject



85
86
87
# File 'app/models/gold/tier.rb', line 85

def features
  parent ? parent.features : @features
end

#free?Boolean

Whether the tier is completely free for shops to use. This is different than a tier that has a zero monthly price but usage-based charges.

Returns:

  • (Boolean)


77
78
79
# File 'app/models/gold/tier.rb', line 77

def free?
  @free
end

#locked?Boolean

Whether the tier has been locked and new shops can no longer user it without an admin assigning it to their shop.

Returns:

  • (Boolean)


66
67
68
# File 'app/models/gold/tier.rb', line 66

def locked?
  @locked
end

#parentObject



89
90
91
# File 'app/models/gold/tier.rb', line 89

def parent
  @parent_id ? self.class.find(@parent_id.to_sym) : nil
end

#qualificationsObject



81
82
83
# File 'app/models/gold/tier.rb', line 81

def qualifications
  parent ? parent.qualifications : @qualifications
end

#top_idObject



93
94
95
# File 'app/models/gold/tier.rb', line 93

def top_id
  parent ? parent.id : id
end

#visible?Boolean

Whether the tier should show up in a list of available options.

Returns:

  • (Boolean)


71
72
73
# File 'app/models/gold/tier.rb', line 71

def visible?
  @visible
end