Class: Mccloud::Mccloudfile

Inherits:
Object
  • Object
show all
Defined in:
lib/mccloud/mccloudfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Mccloudfile

Returns a new instance of Mccloudfile.



8
9
10
11
12
# File 'lib/mccloud/mccloudfile.rb', line 8

def initialize(path)
  # Path to the file
  @path=path

end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/mccloud/mccloudfile.rb', line 5

def path
  @path
end

#sectionsObject

Returns the value of attribute sections.



6
7
8
# File 'lib/mccloud/mccloudfile.rb', line 6

def sections
  @sections
end

Instance Method Details

#exclude_section?(section) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/mccloud/mccloudfile.rb', line 45

def exclude_section?(section)
  section.each do |s|
    return false if @sections.include?(s)
  end
  return true
end

#exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mccloud/mccloudfile.rb', line 14

def exists?
  return File.exists?(@path)
end

#generate(options = {:force => false}) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mccloud/mccloudfile.rb', line 52

def generate(options={:force => false})
  force=options[:force]
  provider=options[:provider]
  raise Mccloud::Error, "You need to specify a provider to generate a Mccloudfile" if provider.nil?

  @sections=[provider.to_sym]
  # We need at least one provider

  if exists? && force==false
      raise Mccloud::Error, "Error Mccloudfile already exists."
  else
   begin
    File.open(@path,'w'){ |f| f.write(self.to_s)}
   rescue Exception => ex
      raise Mccloud::Error, "Error saving Mccloudfile: #{ex}"
   end
  end
end

#to_sObject



71
72
73
74
75
# File 'lib/mccloud/mccloudfile.rb', line 71

def to_s
  template=File.new(File.join(File.dirname(__FILE__),"templates","Mccloudfile.erb")).read
  result=::ERB.new(template,nil,"-","@output").result(binding)
  return result
end

#uncomment(selection) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mccloud/mccloudfile.rb', line 24

def uncomment(selection)

  cur_pos=@output.length
  yield
  new_pos=@output.length

  if exclude_section?(selection)
    # Extract the block
    block_text=@output[cur_pos..new_pos]

    # Remove the block
    @output[cur_pos..new_pos]=''

    # Comment the block, with leading spaces into account
    block_text.gsub!(/^(\s)*/,'\1# ')

    # Re-insert the block
    @output=@output.insert cur_pos, block_text
  end
end