Module: Nugrant::Helper::Bag

Defined in:
lib/nugrant/helper/bag.rb

Class Method Summary collapse

Class Method Details

.parse_data(filepath, filetype, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nugrant/helper/bag.rb', line 15

def self.parse_data(filepath, filetype, options = {})
  return if not File.exists?(filepath)

  File.open(filepath, "rb") do |file|
    parsing_method = "parse_#{filetype}"
    return send(parsing_method, file.read)
  end
rescue => error
  if options[:error_handler]
    # TODO: Implements error handler logic
    options[:error_handler].handle("Could not parse the user #{filetype} parameters file '#{filepath}': #{error}")
  end
end

.parse_json(data_string) ⇒ Object



29
30
31
# File 'lib/nugrant/helper/bag.rb', line 29

def self.parse_json(data_string)
  MultiJson.load(data_string)
end

.parse_yml(data_string) ⇒ Object



33
34
35
36
37
# File 'lib/nugrant/helper/bag.rb', line 33

def self.parse_yml(data_string)
  YAML::ENGINE.yamler = 'syck' if defined?(YAML::ENGINE)

  YAML.load(data_string)
end

.read(filepath, filetype, options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/nugrant/helper/bag.rb', line 9

def self.read(filepath, filetype, options = {})
  data = parse_data(filepath, filetype, options)

  return Nugrant::Bag.new(data)
end