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



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

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

.parse_file(config_path = nil) ⇒ Object



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

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:



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

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



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

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



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

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