Class: Floorplanner::DesignDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/floorplanner/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(fml) ⇒ DesignDocument

Returns a new instance of DesignDocument.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/floorplanner/document.rb', line 10

def initialize fml
  if fml.kind_of? String # filename
    @xml = Nokogiri::XML.parse(fml)
  elsif fml.kind_of? Nokogiri::XML::Document
    @xml = fml
  elsif fml.respond_to?(:read) # IO
    @xml = Nokogiri::XML.parse(fml)
  else
    raise ArgumentError.new("values must be one of: filename, IO, LibXML::XML::Document")
  end
end

Instance Method Details

#save(path) ⇒ Object



56
57
58
# File 'lib/floorplanner/document.rb', line 56

def save path
  @xml.write_to open(path, 'w')
end

#to_sObject



64
65
66
# File 'lib/floorplanner/document.rb', line 64

def to_s
  @xml.to_s
end

#to_xmlObject



60
61
62
# File 'lib/floorplanner/document.rb', line 60

def to_xml
  @xml
end

#update_heights(new_height) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/floorplanner/document.rb', line 22

def update_heights new_height
  lines = @xml.xpath("/design/lines/line[type='default_wall' or type='normal_wall' or contains(type,'hedge') or contains(type,'fence')]")
  lines.each do |line|
    begin
      points = line.xpath("points").first
      next unless points.content.include? ","

      coords = points.content.strip.split(",")
      top_coords = coords[1].strip.split(/\s/).map(&:to_f)

      top_coords[2] = new_height
      top_coords[5] = new_height
      if top_coords.length > 6
        top_coords[8] = new_height
      end

      coords[1] = top_coords.join(" ")
      points.content = coords.join(",")
    rescue; end
  end
end

#update_thumb_2d_url(thumb_2d_url) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/floorplanner/document.rb', line 44

def update_thumb_2d_url thumb_2d_url
  if thumb_node = @xml.xpath('/design/thumb-2d-url').first
    thumb_node.content = thumb_2d_url
  elsif design_node = @xml.xpath('/design').first
    thumb_node = @xml.create_element('thumb-2d-url')
    thumb_node.content = thumb_2d_url
    design_node << thumb_node
  else
    raise "Cannot update the 2D thumb URL!"
  end
end