Class: StaticDocs::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/static_docs/importer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Importer

Returns a new instance of Importer.



9
10
11
# File 'lib/static_docs/importer.rb', line 9

def initialize(namespace)
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



3
4
5
# File 'lib/static_docs/importer.rb', line 3

def namespace
  @namespace
end

Class Method Details

.import(namespace) ⇒ Object



5
6
7
# File 'lib/static_docs/importer.rb', line 5

def self.import(namespace)
  new(namespace).import
end

Instance Method Details

#cleanupObject



39
40
41
42
# File 'lib/static_docs/importer.rb', line 39

def cleanup
  actual_page_urls = (config['pages'] + config['special']).map{ |page| page['path'] }
  Page.where(:namespace => namespace).where('path not in (?)', actual_page_urls).destroy_all
end

#configObject



17
18
19
# File 'lib/static_docs/importer.rb', line 17

def config
  @config ||= YAML.load IO.read File.join source, 'index.yml'
end

#importObject



21
22
23
24
25
# File 'lib/static_docs/importer.rb', line 21

def import
  cleanup
  import_collection config['special'], false
  import_collection config['pages']
end

#import_collection(pages, show = true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/static_docs/importer.rb', line 27

def import_collection(pages, show = true)
  return if pages.empty?
  pages.each_with_index do |data, index|
    page = Page.where(:namespace => namespace, :path => data['path']).first_or_initialize
    page.extension = data['file'][/([^\.]+)$/]
    page.position = show ? index : nil if page.attributes.include?('position')
    page.title = data['title']
    page.body = File.read File.join(source, data['file'])
    page.save
  end
end

#sourceObject



13
14
15
# File 'lib/static_docs/importer.rb', line 13

def source
  @source ||= Rails.root.join StaticDocs.sources[namespace]
end