Class: Amber::SiteConfiguration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/amber/site_configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, root_dir, options = {}) ⇒ SiteConfiguration

accepts a file_path to a configuration file.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/amber/site_configuration.rb', line 57

def initialize(site, root_dir, options={})
  @children = []
  @path_prefix = nil
  @site = site
  @root_dir = File.expand_path(find_in_directory_tree('amber', 'config.rb', root_dir))
  if @root_dir == '/'
    puts "Could not find amber/config.rb in the directory tree. Run `amber` from inside an amber website directory"
    exit(1)
  end
  @pages_dir   = File.join(@root_dir, 'pages')
  @dest_dir    = File.join(@root_dir, 'public')
  @config_dir  = File.join(@root_dir, 'amber')
  @config_file = config_path('config.rb')
  @menu_file   = config_path('menu.txt')
  @locales_dir = config_path('locales')
  @layouts_dir = config_path('layouts')
  @title = "untitled"
  @pagination_size = 20

  @menu = Menu.new('root')
  @menu.load(@menu_file) if @menu_file

  self.eval
  @path_prefix = options[:path_prefix] if options[:path_prefix]
  self.cleanup

  reset_timestamp
  Render::Layout.load(@layouts_dir)
end

Instance Attribute Details

#childrenObject

an array of SiteConfigurations that are attached as sub-sites to this one.



36
37
38
# File 'lib/amber/site_configuration.rb', line 36

def children
  @children
end

#config_dirObject

Returns the value of attribute config_dir.



26
27
28
# File 'lib/amber/site_configuration.rb', line 26

def config_dir
  @config_dir
end

#config_fileObject

Returns the value of attribute config_file.



27
28
29
# File 'lib/amber/site_configuration.rb', line 27

def config_file
  @config_file
end

#default_localeObject

Returns the value of attribute default_locale.



16
17
18
# File 'lib/amber/site_configuration.rb', line 16

def default_locale
  @default_locale
end

#dest_dirObject

Returns the value of attribute dest_dir.



25
26
27
# File 'lib/amber/site_configuration.rb', line 25

def dest_dir
  @dest_dir
end

#layouts_dirObject

Returns the value of attribute layouts_dir.



28
29
30
# File 'lib/amber/site_configuration.rb', line 28

def layouts_dir
  @layouts_dir
end

#localesObject

Returns the value of attribute locales.



15
16
17
# File 'lib/amber/site_configuration.rb', line 15

def locales
  @locales
end

#locales_dirObject

Returns the value of attribute locales_dir.



31
32
33
# File 'lib/amber/site_configuration.rb', line 31

def locales_dir
  @locales_dir
end

Returns the value of attribute menu.



21
22
23
# File 'lib/amber/site_configuration.rb', line 21

def menu
  @menu
end

Returns the value of attribute menu_file.



30
31
32
# File 'lib/amber/site_configuration.rb', line 30

def menu_file
  @menu_file
end

#pages_dirObject

Returns the value of attribute pages_dir.



24
25
26
# File 'lib/amber/site_configuration.rb', line 24

def pages_dir
  @pages_dir
end

#pagination_sizeObject

Returns the value of attribute pagination_size.



14
15
16
# File 'lib/amber/site_configuration.rb', line 14

def pagination_size
  @pagination_size
end

#path=(value) ⇒ Object

Sets the attribute path

Parameters:

  • value

    the value to set the attribute path to.



29
30
31
# File 'lib/amber/site_configuration.rb', line 29

def path=(value)
  @path = value
end

#path_prefixObject Also known as: path

Returns the value of attribute path_prefix.



17
18
19
# File 'lib/amber/site_configuration.rb', line 17

def path_prefix
  @path_prefix
end

#root_dirObject

Returns the value of attribute root_dir.



23
24
25
# File 'lib/amber/site_configuration.rb', line 23

def root_dir
  @root_dir
end

#short_pathsObject

Returns the value of attribute short_paths.



19
20
21
# File 'lib/amber/site_configuration.rb', line 19

def short_paths
  @short_paths
end

#timestampObject

Returns the value of attribute timestamp.



32
33
34
# File 'lib/amber/site_configuration.rb', line 32

def timestamp
  @timestamp
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/amber/site_configuration.rb', line 13

def title
  @title
end

Class Method Details

.load(site, root_dir, options = {}) ⇒ Object

CLASS METHODS



46
47
48
# File 'lib/amber/site_configuration.rb', line 46

def self.load(site, root_dir, options={})
  SiteConfiguration.new(site, root_dir, options)
end

Instance Method Details

#cleanupObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amber/site_configuration.rb', line 96

def cleanup
  @locale ||= I18n.default_locale
  I18n.default_locale = @locale
  @locales ||= [@locale]
  @locales.map! {|locale|
    if Amber::POSSIBLE_LANGUAGE_CODES.include?(locale.to_s)
      locale.to_sym
    else
      nil
    end
  }.compact
  if @path_prefix
    @path_prefix.gsub!(%r{^/|/$}, '')
    @path_prefix = nil if @path_prefix == ''
  end
end

#config_path(file) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/amber/site_configuration.rb', line 121

def config_path(file)
  path = File.join(@config_dir, file)
  if File.exist?(path)
    path
  else
    nil
  end
end

#evalObject

def pages_changed?

self.changed? || @children.detect {|child| child.changed?}

end



117
118
119
# File 'lib/amber/site_configuration.rb', line 117

def eval
  self.instance_eval(File.read(@config_file), @config_file)
end

#find_in_directory_tree(target_dir_name, target_file_name, directory_tree = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/amber/site_configuration.rb', line 134

def find_in_directory_tree(target_dir_name, target_file_name, directory_tree=nil)
  search_dir = directory_tree || Dir.pwd
  while search_dir != "/"
    Dir.foreach(search_dir) do |f|
      if f == target_dir_name && File.exist?(File.join(search_dir, f,target_file_name))
        return search_dir
      end
    end
    search_dir = File.dirname(search_dir)
  end
  return search_dir
end

#map(path_to_directory_source, options = {}) ⇒ Object

map(‘/path’ => ‘../othersite’)



90
91
92
93
94
# File 'lib/amber/site_configuration.rb', line 90

def map(path_to_directory_source, options={})
  path, root_dir = path_to_directory_source.to_a.first
  config = self.load(@site, root_dir, {:path_prefix => path})
  @site.add_config(config)
end

#reset_timestampObject



130
131
132
# File 'lib/amber/site_configuration.rb', line 130

def reset_timestamp
  @timestamp = File.mtime(@pages_dir)
end