Class: Halcyon::Config::File

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

Overview

Class to assist with loading configuration from a file.

Examples:

Halcyon::Config::File.new(file_name_or_path).to_hash #=> {...}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, filter_config = true) ⇒ File

Creates a profile with the default paths.

  • file is the path to the file.

  • filter_config specifies whether to filter the contents through ERB before parsing it.



23
24
25
26
27
28
29
30
31
32
# File 'lib/halcyon/config/file.rb', line 23

def initialize(file, filter_config = true)
  if ::File.exist?(file)
    self.path = file
  elsif ::File.exist?(Halcyon.paths.for(:config)/file)
    self.path = Halcyon.paths.for(:config)/file
  else
    raise ArgumentError.new("Could not find #{self.path} (it does not exist).")
  end
  self.content = self.filter(::File.read(self.path), filter_config)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



15
16
17
# File 'lib/halcyon/config/file.rb', line 15

def content
  @content
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/halcyon/config/file.rb', line 14

def path
  @path
end

Class Method Details

.load(path) ⇒ Object

Provides a convenient way to load the configuration and return the appropriate hash contents.



79
80
81
82
83
84
85
86
87
# File 'lib/halcyon/config/file.rb', line 79

def load(path)
  file = File.new(path)
  case path
  when /\.(yaml|yml)/
    file.to_hash
  when /\.(json)/
    file.to_hash(:from_json)
  end
end

.load_from_json(path) ⇒ Object

Loads the configuration file and parses it’s contents as JSON. This is a shortcut method.



99
100
101
# File 'lib/halcyon/config/file.rb', line 99

def load_from_json(path)
  self.new(path).to_hash(:from_json)
end

.load_from_yaml(path) ⇒ Object

Loads the configuration file and parses it’s contents as YAML. This is a shortcut method.



92
93
94
# File 'lib/halcyon/config/file.rb', line 92

def load_from_yaml(path)
  self.new(path).to_hash
end

Instance Method Details

#filter(content, filter_through_erb) ⇒ Object

Filters the contents through ERB.



65
66
67
68
# File 'lib/halcyon/config/file.rb', line 65

def filter(content, filter_through_erb)
  content = ERB.new(content).result if filter_through_erb
  content
end

#inspectObject



70
71
72
# File 'lib/halcyon/config/file.rb', line 70

def inspect
  "#<Halcyon::Config::File #{self.path}>"
end

#to_hash(from = :from_yaml) ⇒ Object

Returns the loaded configuration file’s contents parsed by the marshal format loader (defaulting to YAML, also providing JSON).

Examples:

p = Halcyon.paths.for(:config)/'config.yml'
c = Halcyon::Config::File.new(p)
c.to_hash #=> the contents of the config file parsed as YAML
c.to_hash(:from_json) #=> same as above only parsed as JSON
# parsing errors will happen if you try to use the wrong marshal
# load method

Returns a Mash if the contents parse to a Hash.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/halcyon/config/file.rb', line 48

def to_hash(from = :from_yaml)
  contents = case from
  when :from_yaml
    require 'yaml'
    YAML.load(self.content)
  when :from_json
    JSON.parse(self.content)
  end
  # return mash instead of hash if result is a hash
  if contents.is_a?(Hash)
    contents = Mash.new contents
  end
  contents
end