Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created January 22, 2016 14:47
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 krisleech/e9e9127526b54bf58e4c to your computer and use it in GitHub Desktop.
Save krisleech/e9e9127526b54bf58e4c to your computer and use it in GitHub Desktop.
Guarding configuration "type"
require 'dry/container'
require 'logger'
module Oxygen
class Configuration
extend Dry::Container::Mixin
# default configuration
register('reports', [])
register('utils.logger', Logger.new($stdout))
module Reports
def self.register(configuration)
raise ArgumentError unless %i(name url section).all? { |key| configuration.keys.include?(key) }
Configuration['reports'] << configuration
self
end
def self.all
Configuration['reports']
end
end
end
end
Oxygen::Configuration::Reports.register(name: '...', url: '...', section: '...')
Oxygen::Configuration::Reports.all # => [...]
Oxygen::Configuration['reports'] # => [...]
@kwando
Copy link

kwando commented Jan 22, 2016

class ReportStore
  def initialize
    @reports = []
  end

  def register(configuration)
    raise ArgumentError unless %i(name url section).all? { |key| configuration.keys.include?(key) }
    @reports << configuration
    self
  end

  def all
    @reports
  end
end

Container.register('reports', ReportStore.new)
Container['reports'].register(name: '...', url: '...', section: '...')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment