Class: Sprite::Config

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

Constant Summary collapse

DEFAULT_CONFIG_PATH =
'config/sprite.yml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings_hash) ⇒ Config

Returns a new instance of Config.



33
34
35
# File 'lib/sprite/config.rb', line 33

def initialize(settings_hash)
  @settings = settings_hash
end

Class Method Details

.chop_trailing_slash(path) ⇒ Object

chop off the trailing slash on a directory path (if it exists)



23
24
25
26
# File 'lib/sprite/config.rb', line 23

def self.chop_trailing_slash(path)
  path = path[0...-1] if path[-1] == File::SEPARATOR
  path
end

.path_present?(path) ⇒ Boolean

check if the path is set

Returns:

  • (Boolean)


29
30
31
# File 'lib/sprite/config.rb', line 29

def self.path_present?(path)
  path.to_s.strip != ""
end

.read_config(path = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sprite/config.rb', line 5

def self.read_config(path = nil)
  config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
  
  # read configuration
  if File.exists?(config_path) 
    begin
      File.open(config_path) {|f| YAML::load(f)} || {}
    rescue => e
      puts "Error reading sprite config: #{config_path}"
      puts e.to_s
      {}
    end
  else
    {}
  end
end

Instance Method Details

#public_path(location, relative = false) ⇒ Object

get the disk path for a location within the public folder (if set)



38
39
40
41
42
43
44
45
# File 'lib/sprite/config.rb', line 38

def public_path(location, relative = false)
  path_parts = []
  path_parts << Sprite.root unless relative
  path_parts << Config.chop_trailing_slash(@settings['public_path']) if Config.path_present?(@settings['public_path'])
  path_parts << location
  
  File.join(*path_parts)
end