Class: Rutty::Config

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

Overview

Flexible config class able to use both hash accessors and object accessors. Nested attributes are also instances of this class, allowing for object (dot) style accessor chaining.

Examples:

config = Config.new :foo => "bar", :test => "baz", :widget => { :another => "string" }

config.foo            # => "bar"
config.widget.another # => "string"

See Also:

Author:

  • Michael Jackson

  • Josh Lindsey

Since:

  • 2.0.0

Direct Known Subclasses

Node

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new Rutty::Config populated with the specified data hash.

Parameters:

  • data (Hash) (defaults to: {})

    Data hash to iterate over

See Also:

Since:

  • 2.0.0



40
41
42
43
# File 'lib/rutty/config.rb', line 40

def initialize(data={})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ void

Allows for object accessor (dot) syntax to access the stored data. If the missing method ends with an equals, calls #[]=, otherwise calls #[]

Parameters:

  • sym (Symbol)

    The method symbol

  • args (*Array)

    The splatted array of method arguments

Since:

  • 2.0.0



92
93
94
95
96
97
98
# File 'lib/rutty/config.rb', line 92

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Class Method Details

.load_config(dir) ⇒ Rutty::Config

Loads the default config data from the yaml file in the specified config dir.

Parameters:

Returns:

See Also:

Since:

  • 2.0.0



27
28
29
30
31
32
# File 'lib/rutty/config.rb', line 27

def load_config dir
  require 'yaml'
  
  data = YAML.load(File.open(File.join(dir, Rutty::Consts::GENERAL_CONF_FILE)).read)
  Rutty::Config.new data
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the specified key via Hash accessor syntax.

Parameters:

  • key (Symbol, String)

    The key to lookup

Returns:

  • (Object)

    The object corresponding to the key

Since:

  • 2.0.0



61
62
63
# File 'lib/rutty/config.rb', line 61

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ void

Set the specified key as the specified value via Hash accessor syntax.

Parameters:

  • key (Symbol, String)

    The key to set

  • value (Object)

    The value to set into the key

Since:

  • 2.0.0



70
71
72
73
74
75
76
# File 'lib/rutty/config.rb', line 70

def []=(key, value)
  if value.class == Hash
    @data[key.to_sym] = Config.new(value)
  else
    @data[key.to_sym] = value
  end
end

#to_hashHash

Simply returns this instance’s @data attribute, which is internally stored as a hash.

Returns:

  • (Hash)

    The @data attribute

Since:

  • 2.0.0



82
83
84
# File 'lib/rutty/config.rb', line 82

def to_hash
  @data
end

#update!(data) ⇒ void

Updates this instance’s @data attribute with the specified data hash.

Parameters:

  • data (Hash)

    Data hash to iterate over

See Also:

Since:

  • 2.0.0



50
51
52
53
54
# File 'lib/rutty/config.rb', line 50

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end