Class: Flydata::Helper::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/helper/config_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(config_content) ⇒ Object


158
159
160
# File 'lib/flydata/helper/config_parser.rb', line 158

def self.parse(config_content)
  ConfigParser.new.parse(config_content)
end

.parse_file(config_path = nil) ⇒ Object


154
155
156
# File 'lib/flydata/helper/config_parser.rb', line 154

def self.parse_file(config_path = nil)
  config_path.nil? ? { helper: Config.default } : parse(File.open(config_path).read)
end

Instance Method Details

#parse(config_content) ⇒ Object

Raises:


162
163
164
165
166
167
168
# File 'lib/flydata/helper/config_parser.rb', line 162

def parse(config_content)
  conf = YAML::load(config_content)
  raise ConfigError.new("Invalid conf format.") unless conf and conf.kind_of?(Hash)
  conf = symbolize_keys(conf)
  conf[:helper] = Config.create(parse_helper(conf[:helper]))
  conf
end

#parse_helper(conf) ⇒ Object


170
171
172
173
174
175
# File 'lib/flydata/helper/config_parser.rb', line 170

def parse_helper(conf)
  return Config::DEFAULT_HELPER if conf.nil? or conf.empty?
  conf[:scheduled_actions] = Config::DEFAULT_SCHEDULED_ACTIONS.
    dup.merge(conf[:scheduled_actions] || {})
  conf
end

#symbolize_keys(value) ⇒ Object


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/flydata/helper/config_parser.rb', line 177

def symbolize_keys(value)
  case value
  when Hash
    value.inject({}){|result, (key, value)|
      new_key = case key
                when String then key.to_sym
                else key
                end
      new_value = symbolize_keys(value)
      result[new_key] = new_value
      result
    }
  when Array
    value.collect {|e| symbolize_keys(e)}
  else
    value
  end
end