Class: Mattock::ConfigurationStore

Inherits:
Object
  • Object
show all
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

Examples:

How to Use:

module CoolTasks
  def config
    @config ||= Mattock::ConfigurationStore.new("cool_tasks")
  end

  def preferences
    config.loaded["preferences.yaml"]
  end

  class AwesomeLib < Mattock::Tasklib
    setting :level, CoolTasks.preferences[:level]
  end
end

In Rakefile

CoolTasks.config.register_search_path(__FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, library_default_dir = nil) ⇒ ConfigurationStore

Returns a new instance of ConfigurationStore.

Parameters:

  • app_name (String)

    The path component to represent this gem - consider downcasing your root module

  • library_default_dir (String) (defaults to: nil)

    If present, a directory containing default files that come in the gem



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

#loadedObject (readonly)

Returns the value of attribute loaded.



74
75
76
# File 'lib/mattock/configuration-store.rb', line 74

def loaded
  @loaded
end

#valiseObject (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::expand_path("../.#{@app_name}", from_file)
  @valise.prepend_search_root(Valise::SearchRoot.new(directory))
  loaded.clear
end

#user_preferencesObject



88
89
90
# File 'lib/mattock/configuration-store.rb', line 88

def user_preferences
  loaded["preferences.yaml"]
end