Class: DxfIO::Writer

Inherits:
Object
  • Object
show all
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

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(options)
  # TODO: replace instance variables to hash with options
  # default options
  @encoding = 'Windows-1251'
  @delimiter = "\r\n"
  @strategy = STRATEGY.first
  @dxf_hash = {}

  if options.is_a? String
    @filename = options
  elsif options.is_a? Hash
    @dxf_hash = options[:dxf_hash] if options.has_key? :dxf_hash
    @filename = options[:path] if options.has_key? :path
    @encoding = options[:encoding] if options.has_key? :encoding
    @delimiter = options[:delimiter] if options.has_key? :delimiter
    @strategy = options[:strategy] if options.has_key? :strategy && STRATEGY.include?(options[: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(options)
  self.new(options).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