class OdaniaStaticPages::Config

Attributes

base_dir[R]
environment[RW]
environments[R]
generator[R]
generator_type[R]
output_nginx_path[R]
output_sites_path[R]
project_dir[R]

Public Class Methods

new(path=nil) click to toggle source
# File lib/odania_static_pages/config.rb, line 12
def initialize(path=nil)
        @base_dir = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..'))
        @project_dir = path.nil? ? Dir.getwd : path
        @output_sites_path = 'sites'
        @output_nginx_path = 'nginx'
        @generator_type = 'Jekyll'
        @environments = {
                develop: Environment.new('_develop', 'DockerCompose'),
                live: Environment.new('_live', 'Rsync')
        }
        @generator = Generator::Jekyll.new(@project_dir, {})
end

Public Instance Methods

config_file() click to toggle source
# File lib/odania_static_pages/config.rb, line 50
def config_file
        File.join(@project_dir, '_config.yml')
end
current_environment() click to toggle source
# File lib/odania_static_pages/config.rb, line 70
def current_environment
        if @environments[@environment].nil?
                puts "Environment #{environment} no found!"
                puts "Available Environments: #{@environments.keys}"
                exit 1
        end
        @environments[@environment]
end
load!() click to toggle source
# File lib/odania_static_pages/config.rb, line 25
def load!
        if File.exist? config_file
                config = YAML.load_file(config_file).stringify_keys!
                @output_sites_path = config['output_sites_path'] unless config['output_sites_path'].nil?
                @output_nginx_path = config['output_nginx_path'] unless config['output_nginx_path'].nil?
                @generator_type = config['generator_type'] unless config['generator_type'].nil?

                unless config['environments'].nil?
                        @environments = {}
                        config['environments'].each_pair do |name, data|
                                @environments[name] = Environment.from_hash data
                        end
                end

                @deploy = Deploy.from_hash(config['deploy']) unless config['deploy'].nil?

                unless config['generator'].nil?
                        generator_config_module = "OdaniaStaticPages::Config::Generator::#{@generator_type}".constantize
                        @generator = generator_config_module.from_hash @project_dir, config['generator']
                end
        end

        self
end
output_nginx_conf_d_path() click to toggle source
# File lib/odania_static_pages/config.rb, line 66
def output_nginx_conf_d_path
        File.join @output_nginx_path, 'conf.d'
end
output_path() click to toggle source
# File lib/odania_static_pages/config.rb, line 58
def output_path
        File.join @project_dir, current_environment.output_path
end
output_site_path() click to toggle source
# File lib/odania_static_pages/config.rb, line 54
def output_site_path
        File.join @project_dir, current_environment.output_path, @output_sites_path
end
pages_path() click to toggle source
# File lib/odania_static_pages/config.rb, line 62
def pages_path
        File.join @project_dir, 'pages'
end
save!() click to toggle source
# File lib/odania_static_pages/config.rb, line 79
def save!
        puts "Writing config file #{config_file}"
        File.write config_file, YAML.dump(to_h)
end

Private Instance Methods

to_h() click to toggle source
# File lib/odania_static_pages/config.rb, line 86
def to_h
        environment_hash = {}
        @environments.each_pair do |name, environment|
                environment_hash[name.to_s] = environment.to_h
        end

        config = {
                output_sites_path: @output_sites_path,
                output_nginx_path: @output_nginx_path,
                generator_type: @generator_type,
                generator: @generator.to_h,
                environments: environment_hash
        }

        config.stringify_keys!
end