-
-
Save havenwood/ad3c014f383d122190f32f2a3fdc577b to your computer and use it in GitHub Desktop.
refactoring suggestions for yoshie902a from irc
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
# frozen_string_literal: true | |
require 'psych' | |
require 'singleton' | |
class Product | |
include Singleton | |
PRODUCTS_CONFIG = Rails.root.join 'config', 'products.yml' | |
INSTANCE_VALUES = ->(id, attrs) { [id.to_sym, new(id: id, **attrs.symbolize_keys)] } | |
attr_reader :all | |
def initialize | |
@all = Psych.load_file(PRODUCTS_CONFIG) | |
.fetch(Rails.env) | |
.to_h(&INSTANCE_VALUES) | |
end | |
def [](id, &block) | |
@all.fetch id&.to_sym, &block | |
end | |
def member | |
self[:member] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment