Class: Vagabond::Vagabondfile

Inherits:
Object
  • Object
show all
Defined in:
lib/vagabond/vagabondfile.rb

Constant Summary collapse

ALIASES =
Mash.new(:boxes => :nodes, :nodes => :boxes)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, *args) ⇒ Vagabondfile

Returns a new instance of Vagabondfile.



11
12
13
14
15
# File 'lib/vagabond/vagabondfile.rb', line 11

def initialize(path=nil, *args)
  path = discover_path(Dir.pwd) unless path
  @path = path
  load_configuration!(args.include?(:allow_missing))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/vagabond/vagabondfile.rb', line 7

def config
  @config
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/vagabond/vagabondfile.rb', line 6

def path
  @path
end

Instance Method Details

#[](k) ⇒ Object



17
18
19
# File 'lib/vagabond/vagabondfile.rb', line 17

def [](k)
  aliased(k) || @config[k]
end

#aliased(k) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagabond/vagabondfile.rb', line 21

def aliased(k)
  if(ALIASES.has_key?(k))
    v = [@config[k], @config[ALIASES[k]]].compact
    if(v.size > 1)
      case v.first.class
      when Array
        m = :|
      when Hash
        m = :merge
      else
        m = :+
      end
      v.inject(&m)
    else
      v.first
    end
  end
end

#discover_path(path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/vagabond/vagabondfile.rb', line 49

def discover_path(path)
  d_path = Dir.glob(File.join(path, 'Vagabondfile')).first
  unless(d_path)
    cut_path = path.split(File::SEPARATOR)
    cut_path.pop
    d_path = discover_path(cut_path.join(File::SEPARATOR)) unless cut_path.empty?
  end
  d_path
end

#load_configuration!(no_raise = false) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/vagabond/vagabondfile.rb', line 40

def load_configuration!(no_raise = false)
  if(@path && File.exists?(@path))
    @config = Mash.new(self.instance_eval(IO.read(@path), @path, 1))
  else
    raise 'No Vagabondfile file found!' unless no_raise
    @config = Mash.new
  end
end