Class: Chef::Part

Inherits:
Object show all
Defined in:
lib/chef/knife/chop/chef_part.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, path) ⇒ Part

Returns a new instance of Part.



96
97
98
99
# File 'lib/chef/knife/chop/chef_part.rb', line 96

def initialize(resource,path)
  self.resource = resource
  self.location = path
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



93
94
95
# File 'lib/chef/knife/chop/chef_part.rb', line 93

def from
  @from
end

#locationObject

Returns the value of attribute location.



91
92
93
# File 'lib/chef/knife/chop/chef_part.rb', line 91

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



92
93
94
# File 'lib/chef/knife/chop/chef_part.rb', line 92

def logger
  @logger
end

#resourceObject

Returns the value of attribute resource.



90
91
92
# File 'lib/chef/knife/chop/chef_part.rb', line 90

def resource
  @resource
end

#toObject (readonly)

Returns the value of attribute to.



94
95
96
# File 'lib/chef/knife/chop/chef_part.rb', line 94

def to
  @to
end

Instance Method Details

#hash_to_rb(hash) ⇒ Object




122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/knife/chop/chef_part.rb', line 122

def hash_to_rb(hash)
  line = ["# #{hash}()"]
  if self.resource.send(hash).size > 0
    line =  ["#{hash}( "]
    self.resource.send(hash).each{|k,v|
      s = v.to_rb
      #s = s.gsub(%r/"'/, %(``')).gsub(%r/'"/, %('``)).gsub(%r/"/, "'").gsub(%r/``'/, %("')).gsub(%r/'``/, %('"))
      unless k.match(%r([\.\-]))
        line << "#{k}: #{s},"
      else
        line << "'#{k}' => #{s},"
      end
    }
    #line[-1].gsub!(%r(,\s*$), "")
    line << ")"
  end
  line.join("\n")
end

#run_lists_to_rb(run_lists, name = 'run_lists') ⇒ Object




156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef/knife/chop/chef_part.rb', line 156

def run_lists_to_rb(run_lists,name='run_lists')
  line = ["# #{name}()"]
  if run_lists.size > 0
    line =  ["#{name}( "]
    run_lists.map{|k,v|
      line << "'#{k}' => #{v.to_rb},"
    }
    line[-1] = line[-1].gsub(%r(\n), "<nl>").gsub(%r(<nl>$), "").gsub(%r(,\s*$), "").gsub(%r(<nl>), "\n")
    line << ")"
  end
  line.join("\n")
end

#save_source(source, ext) ⇒ Object




180
181
182
183
184
185
186
187
188
# File 'lib/chef/knife/chop/chef_part.rb', line 180

def save_source(source,ext)
  @logger.info "Saving '#{ext}'"
  location = self.location.gsub(%r(\.#{@from}$), ".#{@to}")
  @logger.debug "Location: #{location}"
  #@logger.debug source
  File.open(location, 'w') do |f|
    f.write source
  end
end

#translate(config) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/chop/chef_part.rb', line 101

def translate(config)
  @config = config
  @logger = @config[:logger]
  @from,@to = @config[:translate]
  @logger.debug "#{@resource.class.name} To #{@to}"
  unless self.resource.respond_to?("generate_#{@to}")
    raise ChopInternalError.new("Unable to support translation '#{@from}' --> '#{@to}' "+
                                    "because #{self.resource.class.name} CANNOT 'generate_#{@to}'")
  end

  str = self.send("translate_to_#{@to}")
end

#translate_to_jsonObject




115
116
117
118
119
# File 'lib/chef/knife/chop/chef_part.rb', line 115

def translate_to_json()
  obj = JSON.parse(self.resource.generate_json(self))
  json = JSON.pretty_generate(obj)
  save_source(json,"json")
end

#translate_to_rbObject




170
171
172
173
174
175
176
177
# File 'lib/chef/knife/chop/chef_part.rb', line 170

def translate_to_rb()
  #rb = Eden::Formatter.format_source(to_rb)
  rb = ::RBeautify.beautify_string :ruby, @resource.generate_rb(self)
  #rb = @resource.generate_rb(self)
  #sexp = Ripper::SexpBuilder.new(rb).parse
  #puts Sorcerer.source(sexp, multiline: true, indent: true)
  save_source(rb, "rb")
end