Class: FileProcessor
- Inherits:
-
Object
- Object
- FileProcessor
- Defined in:
- lib/rboss/file_processor.rb
Instance Method Summary collapse
- #copy_to(file) ⇒ Object
-
#initialize(opts = {}) ⇒ FileProcessor
constructor
A new instance of FileProcessor.
- #process ⇒ Object
- #register(type, actions) ⇒ Object
- #return ⇒ Object
- #to_load(&block) ⇒ Object
- #to_process(&block) ⇒ Object
- #to_store(&block) ⇒ Object
- #with(file, type = :plain) {|_self| ... } ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ FileProcessor
Returns a new instance of FileProcessor.
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/rboss/file_processor.rb', line 30 def initialize opts = {} @logger = opts[:logger] @var = opts[:var] @logger ||= Logger::new(STDOUT) @handlers = { :plain => { :load => lambda do |file| @logger.info "Loading file #{file}" File.read(file) end, :store => lambda do |file, content| @logger.info "Saving file #{file}" File.open(file, 'w+') { |f| f.write (content) } end }, :xml => { :load => lambda do |file| @logger.info "Parsing file #{file}" REXML::Document::new File::new(file) end, :store => lambda do |file, xml| @logger.info "Saving file #{file}" content = '' formatter = REXML::Formatters::Pretty::new 2 formatter.write xml, content File.open(file, 'w+') { |f| f.write content } end } } @actions = {} end |
Instance Method Details
#copy_to(file) ⇒ Object
76 77 78 |
# File 'lib/rboss/file_processor.rb', line 76 def copy_to file @actions[@current_file][:copy] = file.to_s end |
#process ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rboss/file_processor.rb', line 96 def process @actions.each do |file, action| action[:obj] = action[:to_load].call file @logger.info "Processing file #{file}" action[:obj] = action[:to_process].call(action[:obj], @var) if action[:obj] file = action[:copy] if action[:copy] return action[:obj] if action[:return] action[:to_store].call file, action[:obj] end end |
#register(type, actions) ⇒ Object
64 65 66 |
# File 'lib/rboss/file_processor.rb', line 64 def register type, actions @handlers[type] = actions end |
#return ⇒ Object
80 81 82 |
# File 'lib/rboss/file_processor.rb', line 80 def return @actions[@current_file][:return] = true end |
#to_load(&block) ⇒ Object
84 85 86 |
# File 'lib/rboss/file_processor.rb', line 84 def to_load &block @actions[@current_file][:to_load] = block end |
#to_process(&block) ⇒ Object
92 93 94 |
# File 'lib/rboss/file_processor.rb', line 92 def to_process &block @actions[@current_file][:to_process] = block end |
#to_store(&block) ⇒ Object
88 89 90 |
# File 'lib/rboss/file_processor.rb', line 88 def to_store &block @actions[@current_file][:to_store] = block end |
#with(file, type = :plain) {|_self| ... } ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/rboss/file_processor.rb', line 68 def with file, type = :plain @current_file = file.to_s @actions[@current_file] = {} to_load &@handlers[type][:load] to_store &@handlers[type][:store] yield self end |