Class: Brewer::Kitchen

Inherits:
Object
  • Object
show all
Defined in:
lib/brewer/kitchen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKitchen

Returns a new instance of Kitchen.



8
9
10
11
# File 'lib/brewer/kitchen.rb', line 8

def initialize
  @url = "http://github.com/llamicron/kitchen.git"
  @recipe = Recipe.new(blank: true)
end

Instance Attribute Details

#recipeObject

Returns the value of attribute recipe.



6
7
8
# File 'lib/brewer/kitchen.rb', line 6

def recipe
  @recipe
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/brewer/kitchen.rb', line 6

def url
  @url
end

Instance Method Details

#clearObject



13
14
15
16
17
18
19
# File 'lib/brewer/kitchen.rb', line 13

def clear
  if present?
    FileUtils.rm_rf(kitchen_dir)
    return true
  end
  false
end

#cloneObject



21
22
23
24
25
26
27
# File 'lib/brewer/kitchen.rb', line 21

def clone
  if !present?
    Git.clone(@url, "kitchen", :path => brewer_dir)
    return true
  end
  false
end

#delete_recipe(recipe) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/brewer/kitchen.rb', line 67

def delete_recipe(recipe)
  if recipe_exists?(recipe)
    File.delete(kitchen_dir(recipe + ".yml"))
    return true
  end
  false
end

#list_recipesObject



75
76
77
78
79
# File 'lib/brewer/kitchen.rb', line 75

def list_recipes
  list = Dir.entries(kitchen_dir).select {|file| file.match(/\.yml/) }
  list.each {|recipe_name| recipe_name.slice!(".yml") }
  return list
end

#list_recipes_as_tableObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/brewer/kitchen.rb', line 81

def list_recipes_as_table
  if list_recipes.empty?
    return "No Saved Recipes"
  else
    recipes_table_rows = list_recipes.each_slice(5).to_a
    recipes_table = Terminal::Table.new :title => "All Recipes", :rows => recipes_table_rows
    puts recipes_table
    return true
  end
end

#load(recipe_name) ⇒ Object



44
45
46
47
48
49
# File 'lib/brewer/kitchen.rb', line 44

def load(recipe_name)
  raise "Recipe name must be a string" unless recipe_name.is_a? String
  raise "Recipe does not exist" unless recipe_exists?(recipe_name)
  @recipe = Recipe.new(recipe_name)
  true
end

#loaded_recipe?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/brewer/kitchen.rb', line 100

def loaded_recipe?
  if @recipe
    if @recipe.name
      return true
    end
  end
  false
end

#new_recipeObject



63
64
65
# File 'lib/brewer/kitchen.rb', line 63

def new_recipe
  @recipe = Recipe.new
end

#present?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/brewer/kitchen.rb', line 35

def present?
  if Dir.exists?(kitchen_dir)
    if Dir.entries(kitchen_dir).length > 2
      return true
    end
  end
  false
end

#recipe_exists?(recipe) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/brewer/kitchen.rb', line 92

def recipe_exists?(recipe)
  raise "Recipe name must be a string" unless recipe.is_a? String
  if list_recipes.include?(recipe)
    return true
  end
  false
end

#refreshObject



29
30
31
32
33
# File 'lib/brewer/kitchen.rb', line 29

def refresh
  clear
  clone
  true
end

#storeObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brewer/kitchen.rb', line 51

def store
  raise "Recipe not loaded. Load a recipe or create a new one" unless !@recipe.vars.empty?
  store = YAML::Store.new kitchen_dir(@recipe.vars['name'] + ".yml")
  store.transaction do
    @recipe.vars.each do |k, v|
      store[k] = v
    end
    store['created_on'] = Time.now
  end
  true
end