/product.rb Secret
Created
May 23, 2019 19:43
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