Class: Mattock::ConfigurationStore
- Inherits:
-
Object
- Object
- Mattock::ConfigurationStore
- Defined in:
- lib/mattock/configuration-store.rb
Overview
Configuration for the set of Tasklibs - useful for when there are settings that shouldn’t be part of the Rakefile, or are specific to a particular environment rather than a set of tasks - e.g. specific to each developer’s laptop or the server.
Having done that, any preferences.yaml file in any of several directories will get merged into a single hash and be available as CoolTasks.preferences
The search path will look like:
-
/etc/mattock
-
/etc/cool_tasks
-
/usr/share/mattock
-
/usr/share/cool_tasks
-
~/.mattock
-
~/.cool_tasks
-
<Rakefile_dir>/.cool_tasks
Each file found will be merged into the running hash, so preferences.yaml in the project dir will be able to override everything.
Last bonus: any file can be added into that search path, and the ‘closest’ one will be found and returned by config.loaded
Instance Attribute Summary collapse
-
#loaded ⇒ Object
readonly
Returns the value of attribute loaded.
-
#valise ⇒ Object
readonly
Returns the value of attribute valise.
Instance Method Summary collapse
-
#initialize(app_name, library_default_dir = nil) ⇒ ConfigurationStore
constructor
A new instance of ConfigurationStore.
-
#register_file(name, type, merge) ⇒ Object
Add special file handling for a particular file.
-
#register_search_path(from_file) ⇒ Object
Add a search path to look for configuration files.
- #user_preferences ⇒ Object
Constructor Details
#initialize(app_name, library_default_dir = nil) ⇒ ConfigurationStore
Returns a new instance of ConfigurationStore.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mattock/configuration-store.rb', line 50 def initialize(app_name, library_default_dir = nil) @app_name = app_name @valise = Valise::Set.define do rw "~/.#{app_name}" rw "~/.mattock/#{app_name}" rw "~/.mattock" rw "/usr/share/#{app_name}" rw "/usr/share/mattock/#{app_name}" rw "/usr/share/mattock" rw "/etc/#{app_name}" rw "/etc/mattock/#{app_name}" rw "/etc/mattock" ro library_default_dir unless library_default_dir.nil? ro from_here("default_configuration") handle "preferences.yaml", :yaml, :hash_merge end @loaded ||= Hash.new{|h,k| h[k] = @valise.find(k).contents} end |
Instance Attribute Details
#loaded ⇒ Object (readonly)
Returns the value of attribute loaded.
74 75 76 |
# File 'lib/mattock/configuration-store.rb', line 74 def loaded @loaded end |
#valise ⇒ Object (readonly)
Returns the value of attribute valise.
74 75 76 |
# File 'lib/mattock/configuration-store.rb', line 74 def valise @valise end |
Instance Method Details
#register_file(name, type, merge) ⇒ Object
Add special file handling for a particular file
77 78 79 |
# File 'lib/mattock/configuration-store.rb', line 77 def register_file(name, type, merge) @valise.add_handler(name, type, merge) end |
#register_search_path(from_file) ⇒ Object
Add a search path to look for configuration files
82 83 84 85 86 |
# File 'lib/mattock/configuration-store.rb', line 82 def register_search_path(from_file) directory = File::("../.#{@app_name}", from_file) @valise.prepend_search_root(Valise::SearchRoot.new(directory)) loaded.clear end |
#user_preferences ⇒ Object
88 89 90 |
# File 'lib/mattock/configuration-store.rb', line 88 def user_preferences loaded["preferences.yaml"] end |