Class: BuckKnife::Project

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, DataAccessor
Defined in:
lib/buckknife/project.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataAccessor

#_read_data, #_write_data, included

Constructor Details

#initialize(data) ⇒ Project

Returns a new instance of Project.



48
49
50
# File 'lib/buckknife/project.rb', line 48

def initialize(data)
  @data = data
end

Class Method Details

.allObject



20
21
22
# File 'lib/buckknife/project.rb', line 20

def all
  Dir["#{root}/*.json"].map { |f| from_file(f) }
end

.all_namesObject



16
17
18
# File 'lib/buckknife/project.rb', line 16

def all_names
  Dir["#{root}/*.json"].map { |f| File.basename(f).split(".").first }
end

.find(name) ⇒ Object



24
25
26
# File 'lib/buckknife/project.rb', line 24

def find(name)
  from_file root.join("#{name}.json")
end

.from_file(path) ⇒ Object



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

def from_file(path)
  raise UnknownProjectError unless File.exist?(path)
  new( JSON.parse( File.read(path) ) )
end

.rootObject



33
34
35
36
37
38
39
40
41
# File 'lib/buckknife/project.rb', line 33

def root
  @root ||= begin
    unless path = Chef::Config[:knife][:project_dir]
      raise UnknownProjectRootError, "You must define knife[:project_dir] in your knife.rb"
    end

    Pathname.new path
  end
end

.root=(path) ⇒ Object



43
44
45
# File 'lib/buckknife/project.rb', line 43

def root=(path)
  @root = Pathname.new(path)
end

Instance Method Details

#environment_namesObject



68
69
70
# File 'lib/buckknife/project.rb', line 68

def environment_names
  @data["environments"].keys.sort
end

#environmentsObject



72
73
74
75
76
# File 'lib/buckknife/project.rb', line 72

def environments
  @data["environments"].map do |name, data|
    Environment.new( name, data, nodes.select { |n| n.environment == name } )
  end
end

#nameObject



52
53
54
# File 'lib/buckknife/project.rb', line 52

def name
  @data["id"]
end

#node(name) ⇒ Object



64
65
66
# File 'lib/buckknife/project.rb', line 64

def node(name)
  nodes.find { |n| n.name == name }
end

#node_namesObject



56
57
58
# File 'lib/buckknife/project.rb', line 56

def node_names
  @data["nodes"].keys.sort
end

#nodesObject



60
61
62
# File 'lib/buckknife/project.rb', line 60

def nodes
  @data["nodes"].map { |name, data| Node.new(name, data) }
end