Class: Linecook::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/linecook/cookbook.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*project_dirs) ⇒ Cookbook

Returns a new instance of Cookbook.



34
35
36
37
38
39
40
41
42
43
# File 'lib/linecook/cookbook.rb', line 34

def initialize(*project_dirs)
  @registry = {}
  project_dirs.each do |dir|
    case dir
    when String then add(dir)
    when Hash   then add('.', dir)
    else add(*dir)
    end
  end
end

Class Attribute Details

.default_file_nameObject



15
16
17
# File 'lib/linecook/cookbook.rb', line 15

def default_file_name
  @default_file_name ||= 'cookbook.yml'
end

.default_path_mapObject



5
6
7
8
9
10
11
12
# File 'lib/linecook/cookbook.rb', line 5

def default_path_map
  @default_path_map ||= {
    :attributes => ['attributes'],
    :files      => ['files'],
    :templates  => ['templates'],
    :recipes    => ['recipes']
  }
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



32
33
34
# File 'lib/linecook/cookbook.rb', line 32

def registry
  @registry
end

Class Method Details

.gemspecs(latest = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/linecook/cookbook.rb', line 19

def gemspecs(latest=true)
  return [] unless Object.const_defined?(:Gem)

  index = Gem.source_index
  specs = latest ? index.latest_specs : index.gems.values

  specs.select do |spec|
    cookbook_file = File.expand_path(default_file_name, spec.full_gem_path)
    File.exists?(cookbook_file)
  end
end

Instance Method Details

#_find_(type, filename, extnames = nil) ⇒ Object

Same as find but returns nil if no file can be found.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/linecook/cookbook.rb', line 70

def _find_(type, filename, extnames=nil)
  if type.nil? || filename.nil?
    return nil
  end

  if absolute?(filename)
    return File.exists?(filename) ? filename : nil
  end

  path(type).each do |dir|
    each_full_path(dir, filename, extnames) do |full_path|
      if File.exists?(full_path) && subpath?(dir, full_path)
        return full_path
      end
    end
  end

  nil
end

#add(dir, path_map = nil) ⇒ Object



50
51
52
53
54
# File 'lib/linecook/cookbook.rb', line 50

def add(dir, path_map=nil)
  resolve_path_map(dir, path_map).each_pair do |type, paths|
    (registry[type] ||= []).concat(paths)
  end
end

#find(type, source_name, extnames = nil) ⇒ Object

Searches for a file by expanding filename vs each directory in the path for type. The first existing full path is returned. If an array of extnames is provided, then each extname is tried for each directory, much as with Kernal.require. Absolute paths that exists are returned directly. Raises an error if the file cannot be found.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/linecook/cookbook.rb', line 95

def find(type, source_name, extnames=nil)
  _find_(type, source_name, extnames) or begin
    case
    when type.nil?
      raise "could not find file: #{source_name.inspect} (nil type specified)"
    when source_name.nil?
      raise "could not find file: #{source_name.inspect}"
    when absolute?(source_name)
      raise "no such file: #{source_name.inspect}"
    else
      try_string = try_extnames?(source_name, extnames) ? " (tried #{extnames.join(', ')})" : nil
      raise "could not find file: #{source_name.inspect}#{try_string}"
    end
  end
end

#path(type) ⇒ Object

Returns an array of directories comprising the path for type.



46
47
48
# File 'lib/linecook/cookbook.rb', line 46

def path(type)
  registry[type] || []
end

#rm(dir, path_map = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/linecook/cookbook.rb', line 56

def rm(dir, path_map=nil)
  resolve_path_map(dir, path_map).each_pair do |type, paths|
    if current = registry[type]
      current = current - paths
      if current.empty?
        registry.delete(type)
      else
        registry[type] = current
      end
    end
  end
end