Class: RC::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/rc/properties.rb

Overview

Project properties make it easy for configurations to utilize information about a project for config settings. Properties are stored in a global variables called ‘$properties`.

$properties.name #=> 'rc'

Properties derive from a project’s ‘.index` file and `var/` files. This may expand in the future to include a project’s gemspec.

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Properties

Initialize new Properties instance.



17
18
19
20
21
# File 'lib/rc/properties.rb', line 17

def initialize(path=nil)
  @root  = find_root(path || Dir.pwd)
  @index = load_index
  @var   = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s) ⇒ Object

Route missing method to index lookup and failing that var file lookup.



35
36
37
38
# File 'lib/rc/properties.rb', line 35

def method_missing(s)
  name = s.to_s.downcase
  index(name) || var(name)
end

Instance Method Details

#find_root(path) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rc/properties.rb', line 45

def find_root(path)
  pwd  = File.expand_path(path)
  home = File.expand_path('~')

  while pwd != '/' && pwd != home
    return pwd if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) }
    pwd = File.dirname(pwd)
  end

  nil
end

#index(name) ⇒ Object (private)



60
61
62
# File 'lib/rc/properties.rb', line 60

def index(name)
  @index[name]
end

#load_gemspecObject (private)

TODO:

Support gemspec as properties source ?



102
103
104
105
# File 'lib/rc/properties.rb', line 102

def load_gemspec
  file = Dir['{*,,pkg/*}.gemspec'].first
  # ...
end

#load_indexObject (private)



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rc/properties.rb', line 86

def load_index
  index = {}
  file = File.join(root, '.index')
  if File.exist?(file)
    begin
      index = YAML.load_file(file)
    rescue SyntaxError => error
      warn error.to_s
    end
  end
  index
end

#rootString

Root directory of project.

Returns:

  • (String)

    Full path to project’s root directory.



28
29
30
# File 'lib/rc/properties.rb', line 28

def root
  @root
end

#var(name) ⇒ Object (private)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rc/properties.rb', line 67

def var(name)
  return @var[name] if @var.key?(name)

  glob = File.join(root, 'var', name)
  file = Dir[glob].first
  if file
    data = File.read(file)
    if data =~ /\A(---|%YAML)/
      data = YAML.load(data)
    end
    @var[name] = data
  else
    nil
  end
end