require "singleton"

module DataHelpers
  class Data
    include Singleton

    class << self
      def all_data
        unless @all_data
          file_path = File.join(Support::Paths.cucumber_root, "features", "yml", yml_name)
          fail "couldn't find #{yml_name} in yml folder!" unless File.exist? file_path

          processed_file = ERB.new(File.read(file_path)).result
          @all_data = YAML.load(processed_file)
        end
        # #clone and #dup are shallow copy. Marshal is easiest way to deep copy
        copy = Marshal.dump(@all_data)
        Marshal.load(copy)
      end

      def snake_case_name
        # DataHelpers::UploadManualAdjustment => upload_manual_adjustment
        helper_name = to_s.split("::").last
        helper_name.split(/(?=[A-Z\d])/).join("_").downcase
      end

      def yml_name
        "#{snake_case_name}.yml"
      end
    end
  end
end
