Class: Geb::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/geb/config.rb

Defined Under Namespace

Classes: ConfigAlreadyExists, ConfigFileNotFound, DestinationDirMissing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Config

initialize the site configuration\

Parameters:

Raises:

  • ConfigFileNotFound if the site directory has no geb.config.yml file



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/geb/config.rb', line 41

def initialize(site)

  # set the site path
  @site = site

  # make sure the site directory has the required geb.config.yml file
  raise ConfigFileNotFound.new("Site path [#{@site.site_path}] has no geb configuration.") unless Geb::Config.site_directory_has_config?(@site.site_path)

  # load the site configuration, if no configuration is found in the file, set it to an empty hash
  @config = YAML.load_file(File.join(@site.site_path, Geb::Defaults::SITE_CONFIG_FILENAME))
  @config ||= {}

end

Class Method Details

.site_directory_has_config?(site_path) ⇒ Boolean

check if the site directory specified has the required geb.config.yml file

Returns:

  • (Boolean)


34
35
36
# File 'lib/geb/config.rb', line 34

def self.site_directory_has_config?(site_path)
  File.exist?(File.join(site_path, Geb::Defaults::SITE_CONFIG_FILENAME))
end

Instance Method Details

#assets_dirString

Note:

the assets directory is relative to the site root

get the configured assets directory

Returns:

  • (String)

    the assets directory



154
155
156
# File 'lib/geb/config.rb', line 154

def assets_dir
  return @config['assets_dir'] || Geb::Defaults::ASSETS_DIR
end

#generate_config_file(destination_directory) ⇒ Object

Note:

the configuration file is generated without the following options (for security reasons):

  • site_name

  • remote_uri

  • remote_path

generate the configuration file

Parameters:

  • destination_directory (String)

    the destination directory where to generate the configuration file

Raises:

  • DestinationDirMissing if the destination directory doesn’t exist

  • ConfigAlreadyExists if the configuration file already exists



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/geb/config.rb', line 63

def generate_config_file(destination_directory)

  # make sure the destination directory exists and there is no configuration file
  raise DestinationDirMissing.new("Specified directory [#{destination_directory}] missing.") unless File.directory?(destination_directory)
  raise ConfigAlreadyExists.new("Specified directory [#{destination_directory}] already has geb config.") if File.exist?(File.join(destination_directory, Geb::Defaults::SITE_CONFIG_FILENAME))

  # initialize a new hash to store the configuration
  new_config = {}

  # add existing configuration to the new configuration
  new_config['local_port'] = local_port           if local_port
  new_config['output_dir'] = output_dir           unless output_dir == Geb::Defaults::OUTPUT_DIR
  new_config['assets_dir'] = assets_dir           unless assets_dir == Geb::Defaults::ASSETS_DIR
  new_config['page_extensions'] = page_extensions unless page_extensions == Geb::Defaults::PAGE_EXTENSIONS
  new_config['template_paths'] = template_paths   if template_paths
  new_config['template_and_partial_identifier'] = template_and_partial_identifier unless template_and_partial_identifier == Geb::Defaults::TEMPLATE_AND_PARTIAL_IDENTIFIER

  # check if site variables are used
  if @config['site_variables']

    # initialize local and release site variables
    new_config['site_variables'] = {}
    new_config['site_variables']['local'] = {}
    new_config['site_variables']['release'] = {}

    # check if any local variables are used
    new_config['site_variables']['local'] = @config['site_variables']['local'] if @config['site_variables']['local']

    # check if any release variables are used, add only variable names and blank values
    if @config['site_variables']['release']

      # copy the local variables to the release variables, but set the values to emtpy strings
      new_config['site_variables']['release'] = @config['site_variables']['release'].keys.each_with_object({}) { |key, hash| hash[key] = '' }

    end # if

  end # if

  # write the new configuration to the destination directory
  File.open(File.join(destination_directory, Geb::Defaults::SITE_CONFIG_FILENAME), 'w') do |file|

    # generate configuration file header
    file.write("#\n")
    file.write("# Geb #{Geb::VERSION} Site Configuration\n")
    file.write("# Generated by site template for #{site_name}\n")
    file.write("#\n")
    file.write("# For more information on the configuration options, see the fully document file at: \n")
    file.write("# https://github.com/mainfram-work/geb/blob/main/lib/geb/samples/geb.config.yml\n")
    file.write("#\n")

    # write the new configuration to the file
    file.write(new_config.to_yaml)

  end # File.open

end

#get_site_variablesArray

Note:

the configured site variables are different depending on the environment (local or release)

get the configured partial paths

Returns:

  • (Array)

    the partial paths



180
181
182
183
184
185
186
187
188
# File 'lib/geb/config.rb', line 180

def get_site_variables

  # check if the site is releasing and return the site variables
  environment = (@site.releasing ? 'release' : 'local')

  # return the site variables for the environment
  return( @config['site_variables']&.[](environment) || {}).dup

end

#local_portInteger

get the configured local port

Returns:

  • (Integer)

    the local port



140
141
142
# File 'lib/geb/config.rb', line 140

def local_port
  return @config['local_port'] || nil
end

#output_dirString

Note:

the assets directory is relative to the site root

get the configured output directory

Returns:

  • (String)

    the output directory



147
148
149
# File 'lib/geb/config.rb', line 147

def output_dir
  return @config['output_dir'] || Geb::Defaults::OUTPUT_DIR
end

#page_extensionsArray

get the configured page extensions

Returns:

  • (Array)

    the page extensions



160
161
162
# File 'lib/geb/config.rb', line 160

def page_extensions
  return @config['page_extensions'] || Geb::Defaults::PAGE_EXTENSIONS
end

#remote_pathString

get the configured remote path

Returns:

  • (String)

    the remote path



134
135
136
# File 'lib/geb/config.rb', line 134

def remote_path
  return @config['remote_path'] || nil
end

#remote_uriString

get the configured remote uri

Returns:

  • (String)

    the remote uri



128
129
130
# File 'lib/geb/config.rb', line 128

def remote_uri
  return @config['remote_uri'] || nil
end

#site_nameString

get the configured site name, if not set, use the site directory name

Returns:

  • (String)

    the site name



122
123
124
# File 'lib/geb/config.rb', line 122

def site_name
  return @config['site_name'] || File.basename(@site.site_path)
end

#template_and_partial_identifierRegexp

get the configured template and partial identifier

Returns:

  • (Regexp)

    the template and partial identifier



166
167
168
# File 'lib/geb/config.rb', line 166

def template_and_partial_identifier
  return @config['template_and_partial_identifier'] || Geb::Defaults::TEMPLATE_AND_PARTIAL_IDENTIFIER
end

#template_pathsArray

Note:

the template paths are relative to the site root

get the configured template paths

Returns:

  • (Array)

    the template paths



173
174
175
# File 'lib/geb/config.rb', line 173

def template_paths
  return @config['template_paths'] || []
end