Class: Magneto::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/magneto/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, filters) ⇒ Site

Returns a new instance of Site.



7
8
9
10
11
12
13
14
# File 'lib/magneto/site.rb', line 7

def initialize(config, filters)
  super()
  @config = config
  @filters = filters
  @items_path = @config[:source_path] + '/items'
  @items_path.freeze
  reset
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/magneto/site.rb', line 5

def config
  @config
end

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/magneto/site.rb', line 5

def filters
  @filters
end

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/magneto/site.rb', line 5

def items
  @items
end

#items_pathObject (readonly)

Returns the value of attribute items_path.



5
6
7
# File 'lib/magneto/site.rb', line 5

def items_path
  @items_path
end

#templatesObject (readonly)

Returns the value of attribute templates.



5
6
7
# File 'lib/magneto/site.rb', line 5

def templates
  @templates
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/magneto/site.rb', line 16

def generate
  find_items
  find_templates
  find_existing_output

  ScriptContext.new(@config.deep_merge({
    :config => @config,
    :site => self
  }))

  reset
end

#writeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/magneto/site.rb', line 29

def write
  return if @written

  puts 'Writing output...'
  @written = true
  output = Set.new unless @existing_output.empty?

  @items.each do |item|
    item.write

    unless @existing_output.empty?
      next if item.abandoned?
      path = item.destination_path

      while path.start_with? @config[:output_path] do
        output << path
        path = File.dirname(path)
      end
    end
  end

  unless @existing_output.empty?
    obsolete_output = @existing_output - output
  
    unless obsolete_output.empty?
      if @config[:remove_obsolete]
        puts 'Removing obsolete output...'

        obsolete_output.to_a.sort.reverse.each do |path|
          if File.directory? path
            FileUtils.rmdir path
          else
            FileUtils.rm path
          end
        end
      else
        puts 'Listing obsolete output...'
        puts obsolete_output.to_a.sort.reverse
      end
    end
  end

  puts 'Site generation succeeded.'
end