Class: Gold::Tier
- Inherits:
-
Object
- Object
- Gold::Tier
- 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
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#monthly_price ⇒ Object
readonly
Returns the value of attribute monthly_price.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#trial_days ⇒ Object
readonly
Returns the value of attribute trial_days.
Class Method Summary collapse
-
.all ⇒ Object
Returns the defined tiers.
-
.find(id) ⇒ Object
Returns the tier by ID or nil if that tier cannot be found.
- .load_from_yaml ⇒ Object
-
.reset! ⇒ Object
Exposed for testing to allow clearing the cached tiers.
-
.visible ⇒ Object
Returns only the visible tiers (ones that customers should see on a pricing comparision page).
Instance Method Summary collapse
- #features ⇒ Object
-
#free? ⇒ Boolean
Whether the tier is completely free for shops to use.
-
#initialize(options = {}) ⇒ Tier
constructor
A new instance of Tier.
-
#locked? ⇒ Boolean
Whether the tier has been locked and new shops can no longer user it without an admin assigning it to their shop.
- #parent ⇒ Object
- #qualifications ⇒ Object
- #top_id ⇒ Object
-
#visible? ⇒ Boolean
Whether the tier should show up in a list of available options.
Constructor Details
#initialize(options = {}) ⇒ Tier
Returns a new instance of Tier.
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( = {}) raise ArgumentError, "Cannot parse tier: #{}" unless .is_a?(Hash) raise ArgumentError, "ID must be provided for a tier" unless ["id"] @id = ["id"].to_sym @parent_id = ["parent_id"] @name = ["name"] @description = ["description"] @locked = .fetch("locked", false) @visible = .fetch("visible", true) @trial_days = .dig("pricing", "trial_days") || 0 @monthly_price = BigDecimal(.dig("pricing", "monthly") || 0) @free = .dig("pricing", "free") || false @qualifications = (["qualifications"] || {}).with_indifferent_access @features = (["features"] || {}).with_indifferent_access end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
41 42 43 |
# File 'app/models/gold/tier.rb', line 41 def description @description end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
41 42 43 |
# File 'app/models/gold/tier.rb', line 41 def id @id end |
#monthly_price ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
41 42 43 |
# File 'app/models/gold/tier.rb', line 41 def name @name end |
#trial_days ⇒ Object (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
.all ⇒ Object
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_yaml ⇒ Object
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 |
.visible ⇒ Object
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
#features ⇒ Object
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.
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.
66 67 68 |
# File 'app/models/gold/tier.rb', line 66 def locked? @locked end |
#parent ⇒ Object
89 90 91 |
# File 'app/models/gold/tier.rb', line 89 def parent @parent_id ? self.class.find(@parent_id.to_sym) : nil end |
#qualifications ⇒ Object
81 82 83 |
# File 'app/models/gold/tier.rb', line 81 def qualifications parent ? parent.qualifications : @qualifications end |
#top_id ⇒ Object
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.
71 72 73 |
# File 'app/models/gold/tier.rb', line 71 def visible? @visible end |