Sha256: b706927b092ef2cf4a77f5400a7e31826ed65e21fdf9b20874261710eb07d7d0

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'multi_json'
require 'yaml'

require 'nugrant/bag'

module Nugrant
  module Helper
    module Bag
      def self.read(filepath, filetype, options = {})
        data = parse_data(filepath, filetype, options)

        return Nugrant::Bag.new(data)
      end

      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

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

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

        YAML.load(data_string)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nugrant-1.4.2 lib/nugrant/helper/bag.rb
nugrant-1.4.1 lib/nugrant/helper/bag.rb