Sha256: 19e7790375bb37902bbe6aab5b2ea8c0d398c7f82a132c60cbcfd32a803245bd

Contents?: true

Size: 1.41 KB

Versions: 65

Compression:

Stored size: 1.41 KB

Contents

module Hydra::GlobalConfigurable
  
  # The config environment name used by the #config method
  #
  # Example:
  #   class MyThing
  #     extend Hydra::GlobalConfigurable
  #   end
  # 
  # Now MyThing.config will be the result of:
  #   MyThing.configure(:production) {|config|}
  #
  # You set shared attributes by leaving the first argument blank or passing the :shared value:
  #   MyThing.configure {|config|} 
  # or
  #   MyThing.configure(:shared) {|config|}
  
  # sets the @configs variable to a new Hash with empty Hash for :shared key and @config to nil
  def reset_configs!
    @config = nil
    @configs = {:shared=>{}}
  end
  
  # A hash of all environment configs
  # The key is the environment name, the value a Hash
  def configs
    @configs ? @configs : (reset_configs! and @configs)
  end
  
  # The main config accessor. It merges the current configs[::Rails.env] 
  # with configs[:shared] and lazy-loads @config to the result.
  def config
    @config ||= configs[:shared].merge(configs[::Rails.env] ||= {})
  end
  alias_method :blacklight_config, :config
  
  # Accepts a value for the environment to configure and a block
  # A hash is yielded to the block
  # If the "env" != :shared,
  # the hash is created by deep cloning the :shared environment config.
  # This makes it possible to create defaults in the :shared config
  def configure(env = :shared, &blk)
    configs[env] = {}
    yield configs[env]
  end
end

Version data entries

65 entries across 65 versions & 2 rubygems

Version Path
hydra-core-5.0.0.pre3 lib/hydra/global_configurable.rb
hydra-core-5.0.0.pre2 lib/hydra/global_configurable.rb
hydra-core-5.0.0.pre1 lib/hydra/global_configurable.rb
hydra-head-4.1.1 lib/hydra/global_configurable.rb
hydra-head-4.1.0 lib/hydra/global_configurable.rb