Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
Allow indifferent hash access for Environment#config.
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jun 11, 2014
1 parent 2c497f6 commit 5cab904
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
70 changes: 36 additions & 34 deletions lib/palimpsest/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_support/core_ext/hash'
require 'tmpdir'

module Palimpsest
Expand All @@ -17,94 +18,94 @@ module Palimpsest
# # example of palimpsest.yml
#
# # component settings
# :components:
# components:
# # all component paths are relative to the base
# :base: _components
# base: _components
#
# # list of components
# :paths:
# paths:
# #- [ components_path, install_path ]
# - [ my_app/templates, apps/my_app/templates ]
# - [ my_app/extra, apps/my_app ]
#
# # externals settings
# :externals:
# externals:
# # server or local path that repositories are under
# :server: "https://github.com/razor-x"
# server: "https://github.com/razor-x"
#
# # list of external repositories
# :repositories:
# repositories:
# # uses repository at https://github.com/razor-x/my_app
# # and installs to apps/my_app
# - :name: my_app
# :path: apps/my_app
# - name: my_app
# path: apps/my_app
#
# # uses repository at https://bitbucket.org/razorx/sub_app
# # and installs to apps/my_app from git reference v1.0.0
# - :name: sub_app
# :path: apps/my_app/sub_app
# :reference: v1.0.0
# :server: apps/my_app
# - name: sub_app
# path: apps/my_app/sub_app
# reference: v1.0.0
# server: apps/my_app
#
# # list of excludes
# # matching paths are removed with {#remove_excludes}.
# :excludes:
# excludes:
# - _assets
# - apps/*/.gitignore
#
# # asset settings
# :assets:
# assets:
# # all options are passed to Assets#options
# # options will use defaults set in Palimpsest::Asset::DEFAULT_OPTIONS if unset here
# # unless otherwise mentioned, options can be set or overridden per asset type
# :options:
# options:
# # opening and closing brackets for asset source tags
# # global option only: cannot be overridden per asset type
# :src_pre: "[%"
# :src_post: "%]"
# src_pre: "[%"
# src_post: "%]"
#
# # relative directory to save compiled assets
# :output: compiled
# output: compiled
#
# # assume assets will be served under here
# :cdn: https://cdn.example.com/
# cdn: https://cdn.example.com/
#
# # compiled asset names include a uniqe hash by default
# # this can be toggled off
# :hash: false
# hash: false
#
# # directories to scan for files with asset tags
# :sources:
# sources:
# # putting assets/stylesheets first would allow asset tags,
# # e.g. for images, to be used in your stylesheets
# - assets/stylesheets
# - public
# - app/src
#
# # all other keys are asset types
# :javascripts:
# :options:
# :js_compressor: :uglifier
# javascripts:
# options:
# js_compressor: :uglifier
# # these paths are loaded into the sprockets environment
# :paths:
# paths:
# - assets/javascripts
# - other/javascripts
#
# # this is another asset type which will have it's own namespace
# :stylesheets:
# :options:
# :css_compressor: :sass
# stylesheets:
# options:
# css_compressor: :sass
#
# :paths:
# paths:
# - assets/stylesheets
# # images can be part of the asset pipeline
# :images:
# :options:
# images:
# options:
# # requires the sprockets-image_compressor gem
# :image_compression: true
# image_compression: true
# # options can be overridden per type
# :output: images
# :paths:
# output: images
# paths:
# - assets/images
# ````
class Environment
Expand Down Expand Up @@ -249,6 +250,7 @@ def config settings = {}
populate unless populated
file = File.join(directory, options[:config_file])
@config = YAML.load_file(file) if File.exists? file
@config = ActiveSupport::HashWithIndifferentAccess.new @config
validate_config if @config
end

Expand Down
16 changes: 10 additions & 6 deletions spec/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,31 @@

context "settings given" do

let(:hash) { ActiveSupport::HashWithIndifferentAccess.new(conf_1: :val_1, conf_2: :val_2, conf_3: :val_3) }

before :each do
allow(YAML).to receive(:load_file).with("#{environment.directory}/palimpsest.yml")
.and_return( { conf_1: :val_1, conf_2: :val_2 } )
.and_return(conf_1: :val_1, conf_2: :val_2)
end

it "merges new settings on first call" do
expect( environment.config({ conf_3: :val_3 }) ).to eq({ conf_1: :val_1, conf_2: :val_2, conf_3: :val_3 })
expect(environment.config conf_3: :val_3).to eq hash
end

it "merges new settings on subsequent call" do
environment.config
expect( environment.config({ conf_3: :val_3 }) ).to eq({ conf_1: :val_1, conf_2: :val_2, conf_3: :val_3 })
expect(environment.config conf_3: :val_3).to eq hash
end

it "remembers new settings" do
environment.config({ conf_3: :val_3 })
expect(environment.config).to eq({ conf_1: :val_1, conf_2: :val_2, conf_3: :val_3 })
hash = ActiveSupport::HashWithIndifferentAccess.new(conf_1: :val_1, conf_2: :val_2, conf_3: :val_3)
environment.config(conf_3: :val_3)
expect(environment.config).to eq hash
end

it "overwrites current settings" do
expect( environment.config({ conf_2: :new_val_2 }) ).to eq({ conf_1: :val_1, conf_2: :new_val_2 })
hash = ActiveSupport::HashWithIndifferentAccess.new(conf_1: :val_1, conf_2: :new_val_2)
expect(environment.config conf_2: :new_val_2).to eq hash
end
end
end
Expand Down

0 comments on commit 5cab904

Please sign in to comment.