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



160
161
162
# File 'lib/flydata/helper/config_parser.rb', line 160

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

.parse_file(config_path = nil) ⇒ Object



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

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

Instance Method Details

#parse(config_content) ⇒ Object

Raises:



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

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



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

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



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

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