class Chef::Part

Attributes

from[R]
location[RW]
logger[R]
resource[RW]
to[R]

Public Class Methods

new(resource,path) click to toggle source
# File lib/chef/knife/chop/chef_part.rb, line 96
def initialize(resource,path)
  self.resource = resource
  self.location = path
end

Public Instance Methods

hash_to_rb(hash) click to toggle source

# 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') click to toggle source

# 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) click to toggle source

# 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) click to toggle source
# 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_json() click to toggle source

# 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_rb() click to toggle source

# 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