Class: Configarrr::YAML

Inherits:
Base
  • Object
show all
Defined in:
lib/configarrr/yaml.rb

Instance Method Summary collapse

Methods inherited from Base

configure, #configure, #set

Constructor Details

#initialize(options = {}) ⇒ YAML

Returns a new instance of YAML.

Raises:

  • (ArgumentError)

5
6
7
8
9
10
11
# File 'lib/configarrr/yaml.rb', line 5

def initialize(options = {})
  raise ArgumentError, "Please provide a YAML file location." unless options[:file]
  super(options)
  ensure_file_exists
  @yaml = ::YAML.load_file(@file) || {}
  set_yaml
end

Instance Method Details

#saveObject


21
22
23
# File 'lib/configarrr/yaml.rb', line 21

def save
  ::File.open(@file, "w") { |file| file << self.to_hash.to_yaml }
end

#set_yamlObject


13
14
15
16
17
18
19
# File 'lib/configarrr/yaml.rb', line 13

def set_yaml
  if @parent
    @yaml.has_key?(@parent) ? set(@yaml[@parent]) : raise(Configarrr::OptionError, "Please provide a valid parent value. #{@parent} does not exist.")
  else
    set @yaml
  end
end

#to_hashObject


25
26
27
# File 'lib/configarrr/yaml.rb', line 25

def to_hash
  @parent ? @yaml.merge({@parent => super}) : super
end