Class: DataHolder

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/data_holder.rb

Instance Method Summary collapse

Constructor Details

#initializeDataHolder

Returns a new instance of DataHolder.



5
6
7
8
# File 'lib/chef/knife/data_holder.rb', line 5

def initialize
  @holder = {}
  @path = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



10
11
12
# File 'lib/chef/knife/data_holder.rb', line 10

def method_missing(name, *args)
  @holder[name] = args
end

Instance Method Details

#==(holder) ⇒ Object

Raises:

  • (TypeError)


14
15
16
17
# File 'lib/chef/knife/data_holder.rb', line 14

def ==(holder)
  raise TypeError.new('Comparisons only allowed between DataHolder instances')
  self._holder == holder._holder
end

#_holderObject



19
20
21
# File 'lib/chef/knife/data_holder.rb', line 19

def _holder
  @holder
end

#_load(path) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/chef/knife/data_holder.rb', line 23

def _load(path)
  @path = path
  self.instance_eval(
    File.read(path)
  )
  self
end

#_outputObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef/knife/data_holder.rb', line 31

def _output
  output = ''
  @holder.each_pair do |k,v|
    output << "#{k}(\n"
    inards = []
    v.each do |item|
      s = ''
      PP.pp(item, s)
      inards << s
    end
    output << inards.join(",\n")
    output << ")\n"
  end
  output
  File.open(@path, 'w') do |file|
    file.write(output)
  end
  output
end

#_pathObject



51
52
53
# File 'lib/chef/knife/data_holder.rb', line 51

def _path
  @path
end