Class: Perus::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/perus/options.rb

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



5
6
7
# File 'lib/perus/options.rb', line 5

def initialize
    @defaults = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *params, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/perus/options.rb', line 23

def method_missing(name, *params, &block)
    if @options.include?(name.to_s)
        @options[name.to_s]
    else
        @options['__anonymous__'][name.to_s]
    end
end

Instance Method Details

#[](name) ⇒ Object



31
32
33
# File 'lib/perus/options.rb', line 31

def [](name)
    @options[name]
end

#load(path, defaults) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/perus/options.rb', line 9

def load(path, defaults)
    if File.exists?(path)
        user_options = IniParse.parse(IO.read(path))
    else
        user_options = {}
    end

    # options are only one level deep, so resolve conflicts
    # by just merging the two conflicting hashes again
    @options = defaults.merge(user_options) do |key, default, user|
        default.merge(user)
    end
end