Sha256: 4b1ca0751066d345946e86a7aac76fc1913699745b73f700a3f8765304a93116

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

require 'arli'
require 'yaml'
require 'forwardable'
require 'arduino/library'
require 'hashie/mash'

require_relative 'formats'

module Arli
  module Lock
    class File
      attr_accessor :lock_file_path,
                    :config,
                    :formatter,
                    :file,
                    :format,
                    :arlifile

      def initialize(config: Arli.config, arlifile: nil)
        self.config         = config
        self.arlifile       = arlifile
        self.format         = config.arlifile.lock_format
        self.formatter      = set_formatter(format)
        self.lock_file_path = "#{config.arlifile.path}/#{config.arlifile.name}.#{formatter.extension}"
        self.file           = ::File.open(lock_file_path, 'w')
      end

      def lock(*libraries)
        append(formatter.header)
        libraries.each do |lib|
          append(formatter.format(lib))
        end
        append(formatter.footer)
      ensure
        close
      end

      def lock!(*args)
        lock(*args)
      end

      def close
        file.close rescue nil
      end

      def append(line = nil)
        return unless line
        line.end_with?("\n") ? file.print(line) : file.puts(line)
      end

      private

      def set_formatter(format)
        klass = format.to_s.capitalize.to_sym
        klazz = ::Arli::Lock::Formats.const_get(klass)
        klazz.new(self)
      rescue NameError
        raise Arli::Errors::ArliError,
              "invalid format #{format}, Arli::Lock::Formats::#{klass} does not exist"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
arli-1.4.0 lib/arli/lock/file.rb
arli-1.3.0 lib/arli/lock/file.rb
arli-1.2.1 lib/arli/lock/file.rb
arli-1.1.0 lib/arli/lock/file.rb
arli-1.0.2 lib/arli/lock/file.rb
arli-1.0.1 lib/arli/lock/file.rb
arli-1.0.0 lib/arli/lock/file.rb