Class: Mccloud::Mccloudfile
- Inherits:
-
Object
- Object
- Mccloud::Mccloudfile
- Defined in:
- lib/mccloud/mccloudfile.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#sections ⇒ Object
Returns the value of attribute sections.
Instance Method Summary collapse
- #exclude_section?(section) ⇒ Boolean
- #exists? ⇒ Boolean
- #generate(options = {:force => false}) ⇒ Object
-
#initialize(path) ⇒ Mccloudfile
constructor
A new instance of Mccloudfile.
- #to_s ⇒ Object
-
#uncomment(selection) ⇒ Object
Bindings in ERB www.stuartellis.eu/articles/erb/ Links: * Trimming whitespace in ERB blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/242656 * Appending to ERB output blog.jayfields.com/2007/01/appending-to-erb-output-from-block.html.
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
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/mccloud/mccloudfile.rb', line 5 def path @path end |
#sections ⇒ Object
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
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
14 15 16 |
# File 'lib/mccloud/mccloudfile.rb', line 14 def exists? return File.exists?(@path) end |
#generate(options = {:force => false}) ⇒ Object
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(={:force => false}) force=[:force] provider=[: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_s ⇒ Object
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
Bindings in ERB www.stuartellis.eu/articles/erb/ Links:
-
Trimming whitespace in ERB
blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/242656
-
Appending to ERB output
blog.jayfields.com/2007/01/appending-to-erb-output-from-block.html
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 |