Class: Lavender::Config

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

Defined Under Namespace

Classes: OpenStruct

Constant Summary collapse

LOCATIONS =
%w[config.yml]

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Config

Returns a new instance of Config.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lavender/config.rb', line 27

def initialize config = nil
  @config = config
  @config ||= {
    'defaults' => {'layout' => 'default', 'processor' => 'haml'},
    'paths' => {
      'pages' => 'pages',
      'layouts' => 'layouts',
      'compiled' => 'compiled',
      'public' => 'public'
    },
    'pwd' => Dir.pwd
  }

  user_config = {}
  LOCATIONS.each do |y|
    if File.exist? y
      user_config = YAML::load_file y
      break
    end
  end

  @config = recursive_merge user_config, @config

  @obj = OpenStruct.new @config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



21
22
23
# File 'lib/lavender/config.rb', line 21

def method_missing(method, *args)
  @obj.send(method, *args)
end

Instance Method Details

#recursive_merge(primary, secondary) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lavender/config.rb', line 53

def recursive_merge primary, secondary
  # merges secondary into primary

  return primary unless primary.is_a? Hash

  output = {}

  primary.each_key do |k|
    if secondary.has_key? k
      output[k] = recursive_merge primary[k], secondary[k]
    else
      output[k] = user_config[k]
    end
  end

  (secondary.keys-primary.keys).each do |k|
    output[k] = secondary[k]
  end

  output
end