Sha256: d9f14c34750c4e956d4859c983cb022e9a23e2d639aabc2b7e01d98c9d56689b

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'yaml'
require_relative 'vendor/version'

module Epuber
  class Lockfile
    attr_accessor :defined_from_file

    # @param [Hash] data
    #
    def initialize(data = {})
      @internal_data = data
    end

    # Load existing lockfile from given path, or create new one. You can define default attributes in block.
    #
    # @return [self]
    #
    def self.from_file(file_path)
      hash = if File.exist?(file_path)
               YAML.safe_load(File.read(file_path))
             else
               {}
             end

      # backward compatibility for version 0.5 and older
      hash['epuber_version'] = hash.delete('version') if hash.include?('version')

      inst = new(hash)
      inst.defined_from_file = file_path
      yield inst if hash.empty? && block_given?
      inst
    end

    # @return nil
    #
    def write_to_file
      return if defined_from_file.nil?

      File.write(defined_from_file, @internal_data.to_yaml)
    end

    # @return [Epuber::Version]
    #
    def epuber_version
      Version.new(@internal_data['epuber_version'])
    end

    # @param [Epuber::Version] new_version
    #
    def epuber_version=(new_version)
      @internal_data['epuber_version'] = new_version.to_s
    end

    # @return [Epuber::Version]
    #
    def bade_version
      value = @internal_data['bade_version']
      value = Version.new(value) unless value.nil?
      value
    end

    # @param [Epuber::Version] new_version
    #
    def bade_version=(new_version)
      @internal_data['bade_version'] = new_version
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
epuber-0.10.3 lib/epuber/lockfile.rb
epuber-0.10.2 lib/epuber/lockfile.rb
epuber-0.10.1 lib/epuber/lockfile.rb
epuber-0.9.3 lib/epuber/lockfile.rb
epuber-0.9.2 lib/epuber/lockfile.rb
epuber-0.9.1 lib/epuber/lockfile.rb
epuber-0.9.0 lib/epuber/lockfile.rb
epuber-0.8.0 lib/epuber/lockfile.rb