Class: DxfIO::Writer
- Inherits:
-
Object
- Object
- DxfIO::Writer
- Defined in:
- lib/dxf_io/writer.rb
Constant Summary collapse
- SECTIONS_LIST =
DxfIO::Constants::SECTIONS_LIST
- HEADER_NAME =
DxfIO::Constants::HEADER_NAME
- STRATEGY =
DxfIO::Constants::WRITER_STRATEGY
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Writer
constructor
A new instance of Writer.
- #run(dxf_hash = @dxf_hash) ⇒ Object (also: #write_hash)
-
#write_through_disk(dxf_hash = @dxf_hash) ⇒ Object
write dxf file directly on disk without temporary usage of memory for content.
-
#write_through_memory(dxf_hash = @dxf_hash) ⇒ Object
construct dxf content in memory and write all in file at once.
Constructor Details
#initialize(options) ⇒ Writer
Returns a new instance of Writer.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dxf_io/writer.rb', line 9 def initialize() # TODO: replace instance variables to hash with options # default options @encoding = 'Windows-1251' @delimiter = "\r\n" @strategy = STRATEGY.first @dxf_hash = {} if .is_a? String @filename = elsif .is_a? Hash @dxf_hash = [:dxf_hash] if .has_key? :dxf_hash @filename = [:path] if .has_key? :path @encoding = [:encoding] if .has_key? :encoding @delimiter = [:delimiter] if .has_key? :delimiter @strategy = [:strategy] if .has_key? :strategy && STRATEGY.include?([:strategy]) else raise ArgumentError, 'options must be String or Hash with :path key' end end |
Class Method Details
.open(options) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/dxf_io/writer.rb', line 31 def open() self.new().tap do |writer_instance| if block_given? yield writer_instance end end end |
Instance Method Details
#run(dxf_hash = @dxf_hash) ⇒ Object Also known as: write_hash
76 77 78 79 80 81 82 83 84 |
# File 'lib/dxf_io/writer.rb', line 76 def run(dxf_hash = @dxf_hash) if @strategy == :memory write_through_memory(dxf_hash) elsif @strategy == :disk write_through_disk(dxf_hash) else raise ArgumentError, ':strategy has invalid value; allowed only [:memory, :disk]' end end |
#write_through_disk(dxf_hash = @dxf_hash) ⇒ Object
write dxf file directly on disk without temporary usage of memory for content
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dxf_io/writer.rb', line 60 def write_through_disk(dxf_hash = @dxf_hash) file_stream do |fp| file_wrap(fp) do dxf_hash.each_pair do |section_name, section_content| section_wrap(fp, section_name) do if header_section?(section_name) header_wrap(fp, section_content) else other_section_wrap(fp, section_content) end end end end end end |
#write_through_memory(dxf_hash = @dxf_hash) ⇒ Object
construct dxf content in memory and write all in file at once
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dxf_io/writer.rb', line 41 def write_through_memory(dxf_hash = @dxf_hash) file_stream do |fp| fp.write( file_content do dxf_hash.inject('') do |sections_content, (section_name, section_content)| sections_content << section_wrapper_content(section_name) do if header_section?(section_name) header_content(section_content) else other_section_content(section_content) end end end end ) end end |