Class: Confection::Project

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/confection/project.rb

Overview

TODO:

Rename to ProjectConfig or ?

Project configuration.

Constant Summary collapse

PATTERN =

Configuration file pattern. The standard configuration file name is Config.rb, and that name should be used in most cases. However, .config.rb can also be use and will take precedence if found. Conversely, config.rb (lowercase form) can also be used but has the least precedence.

Config files looked for in the order or precedence:

* `.config.rb`
* `Config.rb`
* `config.rb`
'{.,}config{.rb,}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Project

Initialize new ProjectConfig.

Parameters:

  • root (String)

    Project root directory.



82
83
84
# File 'lib/confection/project.rb', line 82

def initialize(root)
  @root  = root
end

Instance Attribute Details

#rootString (readonly) Also known as: directory

Project root directory.

Returns:

  • (String)

    project’s root directory



91
92
93
# File 'lib/confection/project.rb', line 91

def root
  @root
end

Class Method Details

.cacheObject

Per library cache.



29
30
31
# File 'lib/confection/project.rb', line 29

def self.cache
  @cache ||= {}
end

.load(lib = nil) ⇒ Project?

Get project configuration from another library.

This method uses the Finder gem.

Parameters:

  • lib (String) (defaults to: nil)

    Library name.

Returns:

  • (Project, nil)

    Located project.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/confection/project.rb', line 43

def self.load(lib=nil)
  if lib
    lib = lib.to_s
    return cache[lib] if cache.key?(lib)
    cache[lib] ||= (
      config_path = Find.path(PATTERN, :from=>lib).first
      config_path ? new(File.dirname(config_path)) : nil
    )
  else
    lookup
  end
end

.lookup(dir = nil) ⇒ String

Lookup configuation file.

Parameters:

  • dir (String) (defaults to: nil)

    Optional directory to begin search.

Returns:

  • (String)

    file path



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/confection/project.rb', line 64

def self.lookup(dir=nil)
  dir = dir || Dir.pwd
  home = File.expand_path('~')
  while dir != '/' && dir != home
    if file = Dir.glob(File.join(dir, PATTERN), File::FNM_CASEFOLD).first
      return new(File.dirname(file))
    end
    dir = File.dirname(dir)
  end
  return nil
end

Instance Method Details

#controller(scope, tool, options = {}) ⇒ Object

Create a configuration controller.

Parameters:

  • scope (Object)

    Context for which controller is being created.

  • tool (Symbol)

    The tool of the configuration to select.



147
148
149
150
151
# File 'lib/confection/project.rb', line 147

def controller(scope, tool, options={})
  profile = options[:profile]
  configs = store.lookup(tool, profile)
  Controller.new(scope, *configs)
end

#each(&block) ⇒ Object

Iterate over each configurations.



156
157
158
# File 'lib/confection/project.rb', line 156

def each(&block)
  store.each(&block)
end

#profiles(tool) ⇒ Array

List of configuration profiles.

Returns:

  • (Array)

    profile names



119
120
121
# File 'lib/confection/project.rb', line 119

def profiles(tool)
  store.profiles(tool)
end

#propertiesObject

TODO:

Use cascading class, e.g. Confstruct.

Project properties.



128
129
130
131
132
133
134
135
136
# File 'lib/confection/project.rb', line 128

def properties
  dotruby = File.join(directory,'.ruby')
  if File.exist?(dotruby)
    data = YAML.load_file(dotruby)
    OpenStruct.new(data)
  else
    OpenStruct.new
  end
end

#sizeFixnum

The number of configurations.

Returns:

  • (Fixnum)

    config count



165
166
167
# File 'lib/confection/project.rb', line 165

def size
  store.size
end

#sourceString

The file path of the project’s configuration file.

Returns:

  • (String)

    path to configuration file



110
111
112
# File 'lib/confection/project.rb', line 110

def source
  Dir.glob(File.join(root, PATTERN), File::FNM_CASEFOLD)
end

#storeObject

Configuration store tracks a project’s confirguration entries.



101
102
103
# File 'lib/confection/project.rb', line 101

def store
  @store ||= Store.new(*source)
end