Class: Erector::Erect
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
-
#superklass ⇒ Object
readonly
Returns the value of attribute superklass.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
stolen from activesuppport/lib/inflector.rb.
- #constantize(camel_cased_word) ⇒ Object
- #explode(dir) ⇒ Object
-
#explode_dirs ⇒ Object
todo: unit test.
-
#initialize(args) ⇒ Erect
constructor
A new instance of Erect.
- #run ⇒ Object
- #say(msg) ⇒ Object
- #to_erector ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(args) ⇒ Erect
Returns a new instance of Erect.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/erector/erect/erect.rb', line 8 def initialize(args) @verbose = true @mode = :to_erector @output_dir = nil @superklass = 'Erector::Widget' @method_name = 'content' opts = OptionParser.new do |opts| opts. = "Usage: erector [options] [file|dir]*" opts.separator "Converts from html/rhtml files to erector widgets, or from erector widgets to html files" opts.separator "" opts.separator "Options:" opts.on("-q", "--quiet", "Operate silently except in case of error") do |quiet| @verbose = !quiet end opts.on("--to-erector", "(default) Convert from html/rhtml to erector classes") do @mode = :to_erector end opts.on("--to-html", "Convert from erector to html") do @mode = :to_html end opts.on("--superclass SUPERCLASS", "Superclass for new widget (default Erector::Widget)") do |superklass| @superklass = superklass end opts.on("--method METHOD", "Method containing content for widget (default 'content')") do |method_name| @method_name = method_name end opts.on("-o", "--output-dir DIRECTORY", "Output files to DIRECTORY (default: output files go next to input files)") do |dir| @output_dir = dir end opts.on_tail("-h", "--help", "Show this message") do @mode = :help puts opts exit end opts.on_tail("-v", "--version", "Show version") do puts Erector::VERSION exit end end opts.parse!(args) @files = args explode_dirs end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def files @files end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def method_name @method_name end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def mode @mode end |
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def output_dir @output_dir end |
#superklass ⇒ Object (readonly)
Returns the value of attribute superklass.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def superklass @superklass end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
7 8 9 |
# File 'lib/erector/erect/erect.rb', line 7 def verbose @verbose end |
Instance Method Details
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
stolen from activesuppport/lib/inflector.rb
144 145 146 147 148 149 150 |
# File 'lib/erector/erect/erect.rb', line 144 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } else lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize(camel_cased_word) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/erector/erect/erect.rb', line 151 def constantize(camel_cased_word) unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end Object.module_eval("::#{$1}", __FILE__, __LINE__) end |
#explode(dir) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/erector/erect/erect.rb', line 81 def explode(dir) case mode when :to_erector FileList["#{dir}/**/*.rhtml", "#{dir}/**/*.html", "#{dir}/**/*.html.erb"] when :to_html FileList["#{dir}/**/*.rb"] end end |
#explode_dirs ⇒ Object
todo: unit test
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/erector/erect/erect.rb', line 69 def explode_dirs exploded_files = FileList.new files.each do |file| if File.directory?(file) exploded_files.add(explode(file)) else exploded_files.add(file) end end @files = exploded_files end |
#run ⇒ Object
90 91 92 93 94 |
# File 'lib/erector/erect/erect.rb', line 90 def run @success = true self.send(mode) @success end |
#say(msg) ⇒ Object
64 65 66 |
# File 'lib/erector/erect/erect.rb', line 64 def say(msg) print msg if verbose end |
#to_erector ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/erector/erect/erect.rb', line 96 def to_erector files.each do |file| say "Erecting #{file}... " begin e = Erector::Erected.new(file, @superklass, @method_name) e.convert say " --> #{e.filename}\n" rescue => e puts e puts e.backtrace.join("\n\t") @success = false end end end |
#to_html ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/erector/erect/erect.rb', line 111 def to_html files.each do |file| say "Erecting #{file}... " #todo: move this into Erected with better tests for the naming methods begin #todo: fail if file isn't a .rb file require file filename = file.split('/').last.gsub(/\.rb$/, '') = camelize(filename) = constantize() if < Erector::Widget = .new #todo: skip if it's missing a no-arg constructor dir = output_dir || File.dirname(file) FileUtils.mkdir_p(dir) output_file = "#{dir}/#{filename}.html" File.open(output_file, "w") do |f| f.puts .to_html end say " --> #{output_file}\n" else say " -- not a widget, skipping\n" end rescue => e puts e puts e.backtrace.join("\n\t") @success = false end end end |