Class: Liquid::Configuration

Inherits:
Section show all
Defined in:
lib/liquid/configuration.rb

Overview

The Configuration class provides a simple interface to configuration stored inside of YAML files.

Instance Method Summary collapse

Methods inherited from Section

from_hash

Methods inherited from Hash

#deep_merge, #deep_merge!, #delta_merge!

Constructor Details

#initialize {|config| ... } ⇒ Configuration

Create a new Liquid::Configuration object.

Yields:

  • (config)

    The new configuration object.



86
87
88
89
# File 'lib/liquid/configuration.rb', line 86

def initialize
  @mixins = OrderedSet.new
  @callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Liquid::Section

Instance Method Details

#callback(&block) ⇒ void

This method returns an undefined value.

Register a callback for config mixins.



136
137
138
# File 'lib/liquid/configuration.rb', line 136

def callback(&block)
  @callbacks << block
end

#mixin(value) ⇒ void

This method returns an undefined value.

Mixin a configuration snippet into the current section.

Parameters:

  • value (Hash, String)

    A hash to merge into the current configuration. If a string is given a filename is assumed and the given file is expected to contain a YAML hash.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/liquid/configuration.rb', line 98

def mixin(value)
  @mixins << value

  if value.is_a?(String)
    value = YAML.load(File.read(value))
  end

  return unless value

  value = Section.from_hash(value)

  deep_merge!(value.delete(:generic)) if value.has_key?(:generic)

  if value.has_key?(Env.to_sym)
    deep_merge!(value[Env.to_sym])
  else
    deep_merge!(value)
  end
end

#reload!void

This method returns an undefined value.

Reload all mixins.



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/liquid/configuration.rb', line 121

def reload!
  clear

  @mixins.each do |file|
    mixin(file)
  end

  @callbacks.each do |callback|
    callback.call(self)
  end
end