Class: Tendersync::Document

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

Defined Under Namespace

Classes: Group, TOCEntry

Constant Summary collapse

Properties =
[:section, :document_id, :title, :permalink, :keywords, :body]
NUM_DASHES =

the number of dashes in keyword fields

28

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Document

Returns a new instance of Document.



54
55
56
57
58
# File 'lib/tendersync/document.rb', line 54

def initialize(values={})
  values.each do | prop, value |
    self.send "#{prop}=", value
  end
end

Class Method Details

.each(section) ⇒ Object



136
137
138
# File 'lib/tendersync/document.rb', line 136

def self.each(section)
  Dir.glob("#{section}/*").each { |f| yield Tendersync::Document.read_from_file(f) }
end

.from_form(section, form) ⇒ Object

Can be scraped from a form



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tendersync/document.rb', line 110

def self.from_form(section,form)
  values = {
    :document_id => form.action[%r{/faqs/(\d+)/edit},1],
    :section => section
  }
  form.fields.each { |tf|
    if field_name = tf.name[/faq\[(.*)\]/,1]
      value = tf.value.map { |line| line.chomp }.join("\n")
      values[field_name.intern] = value
    end
  }
  new(values)
rescue => e
  puts e.backtrace.join("\n  ")
  raise
end

.index_for(section_id, section_name, permalink = nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/tendersync/document.rb', line 140

def self.index_for(section_id, section_name, permalink = nil)
  permalink ||= "#{section_id}-table-of-contents"
  new(:section => section_id,
      :title => "#{section_name} Table of Contents",
      :permalink => permalink,
      :keywords => "toc index")
end

.load(section, io) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tendersync/document.rb', line 81

def self.load(section, io)
  values = { :section => section }
  key = data = nil
  while line = io.gets
    line.chomp!
    if line =~ /^----+ (.+) -----+$/
      values[key] = data.join("\n") if data 
      key = $1.intern
      data = []
    else
      raise "keyword line not recognized: #{line}" unless data
      data << line
    end
  end
  values[key] = data.join("\n") if key
  new values
end

.read_from_file(file_name) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/tendersync/document.rb', line 99

def self.read_from_file(file_name)
  section = file_name.split('/')[-2] 
  if !File.exists? file_name
    raise Tendersync::Runner::Error, "Cannot read #{file_name}"
  end
  File.open(file_name) { |f| self.load(section, f) }
end

Instance Method Details

#refresh_index(groups = [], depth = 2) ⇒ Object

Update this document body with an index of all the documents in this section. Underneath the TOC entry for a document will be sub entries for each named A element.

group_map is an associative array of /regex/ to “Title String” of a group of documents. It is used to divide up documents into groups within the table of contents. A document is placed in a TOC group based on the first regex it matches in the group map.

If group_map is empty then headings will be sorted alphabetically and not grouped.

depth s the number of nested levels to descend into a document.



161
162
163
# File 'lib/tendersync/document.rb', line 161

def refresh_index(groups=[], depth=2)
  generate_index(create_toc(groups), depth)  
end

#saveObject



75
76
77
78
79
# File 'lib/tendersync/document.rb', line 75

def save
  FileUtils.mkdir_p section
  File.open("#{section}/#{permalink}",'w') { |f| f.print self }
  self
end

#to_form(form) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/tendersync/document.rb', line 126

def to_form(form)
  form.fields.each { |tf|
    if field_name = tf.name[/faq\[(.*)\]/,1] and self.send(field_name.intern)
      lines = []
      self.send(field_name.intern).each_line {|line| lines << line.chomp }
      tf.value = lines.join("\r\n")
    end
  }
end

#to_sObject

Documents can be read from / written to a file



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tendersync/document.rb', line 62

def to_s
  io = StringIO.new
  Properties.each do |field|
    next unless value = self.send(field) 
    io.write  "-" * NUM_DASHES
    io.write " #{field} "
    io.write "-" * NUM_DASHES
    io.puts
    io.puts value
  end
  io.string
end