Class: Zwite::Site

Inherits:
Object show all
Defined in:
lib/zwite/core/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Site

Initialization



64
65
66
67
68
69
70
71
# File 'lib/zwite/core/site.rb', line 64

def initialize(path)
  self.path = path
  self.config_path = self.path + "config.yml"
  self.apps_path = self.path + "apps"
  self.output_path = self.path + "public"
  self.cache_path = self.path + ".cache"
  self.load
end

Instance Attribute Details

#appsObject

Returns the value of attribute apps.



17
18
19
# File 'lib/zwite/core/site.rb', line 17

def apps
  @apps
end

#apps_pathObject

Returns the value of attribute apps_path.



11
12
13
# File 'lib/zwite/core/site.rb', line 11

def apps_path
  @apps_path
end

#cache_pathObject

Returns the value of attribute cache_path.



13
14
15
# File 'lib/zwite/core/site.rb', line 13

def cache_path
  @cache_path
end

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/zwite/core/site.rb', line 15

def config
  @config
end

#config_pathObject

Returns the value of attribute config_path.



10
11
12
# File 'lib/zwite/core/site.rb', line 10

def config_path
  @config_path
end

#liquid_contextObject

Returns the value of attribute liquid_context.



19
20
21
# File 'lib/zwite/core/site.rb', line 19

def liquid_context
  @liquid_context
end

#output_pathObject

Returns the value of attribute output_path.



12
13
14
# File 'lib/zwite/core/site.rb', line 12

def output_path
  @output_path
end

#pathObject

Properties



9
10
11
# File 'lib/zwite/core/site.rb', line 9

def path
  @path
end

#pluginsObject

Returns the value of attribute plugins.



16
17
18
# File 'lib/zwite/core/site.rb', line 16

def plugins
  @plugins
end

Instance Method Details

#buildObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/zwite/core/site.rb', line 89

def build
  unless files_changed?
    return
  end
  
  puts "Building site..."
  
  begin
    self.reset
    self.preprocess
    self.generate
  rescue Exception => e
    puts "ERROR BUILDING SITE"
    puts "-------------------"
    puts e
  end
  
  puts "Building site finished..."
  
end

#files_changed?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
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
# File 'lib/zwite/core/site.rb', line 21

def files_changed?
  @timestamps ||= {}
  old_timestamps = @timestamps.dup
  
  # save timestamps
  @timestamps = {}
  Dir[self.apps_path + "**/*"].each do |p|
    path = Pathname.new(p)
    @timestamps[path.to_s] = path.mtime
  end
  
  # if we have no old_timestamps probably a first generation
  unless old_timestamps.any?
    return true
  end
  
  # if we have no timestamps probably a bug
  unless @timestamps.any?
    return true
  end
  
  # check if we deleted any files
  old_timestamps.each do |path, timestamp|
    if @timestamps.fetch(path, nil).nil?
      return true
    end
  end
  
  # check if we added any files or modified any
  @timestamps.each do |path, timestamp|
    old_timestamp = old_timestamps.fetch(path, nil)
    if old_timestamp.nil? || old_timestamp != timestamp
      return true
    end
  end
  
  return false
end

#generateObject



125
126
127
128
129
130
# File 'lib/zwite/core/site.rb', line 125

def generate
  self.apps.each do |app|
    app.generate
  end
  
end

#loadObject

Actions



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zwite/core/site.rb', line 77

def load
  self.config = YAML.load_file(self.config_path)
  
  self.plugins = Zwite::Plugin.subclasses
  
  self.apps = []
  self.config["apps"].each do |a|
    self.apps << App.new(self, self.apps_path + a["name"], a["mount"])
  end
  
end

#preprocessObject



117
118
119
120
121
122
123
# File 'lib/zwite/core/site.rb', line 117

def preprocess
  self.liquid_context = {}
  self.apps.each do |app|
    app.preprocess
    self.liquid_context[app.name] = app
  end
end

#resetObject



110
111
112
113
114
115
# File 'lib/zwite/core/site.rb', line 110

def reset
  if self.output_path.exist?
    self.output_path.rmtree
  end
  self.output_path.mkpath
end