Sha256: 242119c0a3d02a036da8f8278c78dee703c8bc1bca951d0a3f73fafb3c3e6e3c

Contents?: true

Size: 1.45 KB

Versions: 40

Compression:

Stored size: 1.45 KB

Contents

class Card
  class FileCardCreator
    # Helper methods to write to files and the console.
    module OutputHelper
      def write_to_mod rel_dir, filename
        dir = File.join "mod", @mod, rel_dir
        FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

        path = File.join dir, filename
        log_file_action path do
          File.open(path, "w") do |opened_file|
            yield(opened_file)
          end
        end
      end

      def log_file_action path
        status, color =
          if File.exist?(path) && !@force
            ["file exists (use 'force=true' to override)", :yellow]
          else
            status = File.exist?(path) ? "overridden" : "created"
            yield
            [status, :green]
          end

        color_puts status, color, path
      end

      # insert content into a file at a given line number
      def write_at fname, at_line, sdat
        open(fname, "r+") do |f|
          (at_line - 1).times do # read up to the line you want to write after
            f.readline
          end
          pos = f.pos # save your position in the file
          rest = f.read # save the rest of the file
          f.seek pos # go back to the old position
          f.puts sdat # write new data & rest of file
          f.puts rest
          color_puts "created", :green, fname
        end
      end

      def color_puts colored_text, color, text=""
        puts "#{colored_text.send(color.to_s)} #{text}"
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
card-1.101.0 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.100.0 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.6 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.5 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.4 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.3 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.2 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.1 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.99.0 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.98.3 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.98.2 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.98.1 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.98.0 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.97.0.1 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.97.0 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.96.8 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.96.7 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.96.6 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.96.5 lib/card/tasks/card/file_card_creator/output_helper.rb
card-1.96.4 lib/card/tasks/card/file_card_creator/output_helper.rb