Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 23, 2019 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/ad3c014f383d122190f32f2a3fdc577b to your computer and use it in GitHub Desktop.
Save havenwood/ad3c014f383d122190f32f2a3fdc577b to your computer and use it in GitHub Desktop.
refactoring suggestions for yoshie902a from irc
# 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