-
-
Save yoshie902a/627845570d0ea88fe0911a3d619b0c58 to your computer and use it in GitHub Desktop.
YAML Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
class Product | |
def self.all | |
@all ||= YAML | |
.load_file(Rails.root/"config"/"products.yml") | |
.fetch(Rails.env) | |
.map { |id, attrs| [id.intern, new(id: id, **attrs.symbolize_keys)] } | |
.to_h | |
end | |
def self.[](id, &if_dne) | |
all.fetch id&.intern, &if_dne | |
end | |
def self.one_membership | |
self[__method__] | |
end | |
def self.two_membership | |
self[__method__] | |
end | |
def self.three_membership | |
self[__method__] | |
end | |
def initialize(id:, name:, amount:, stripe_plan_id:) | |
@id = id.intern | |
@name = name.freeze | |
@amount = amount | |
@square_plan_id = square_plan_id&.freeze | |
end | |
attr_reader :id, :name, :amount, :square_plan_id | |
def subscription? | |
!!square_plan_id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment