class Kamaze::Version

Describe version using a YAML file.

@see github.com/jcangas/version_info

Constants

VERSION

Attributes

file[R]

Get filepath used to parse version (YAML file).

@return [Pathname|String]

parsed[R]

Get parsed data

@return [Hash]

Public Class Methods

new(file = nil) click to toggle source

@param [String|Pathname|nil] file

# File lib/kamaze/version.rb, line 33
def initialize(file = nil)
  (file.nil? ? file_from(caller_locations) : file).tap do |file|
    @file = ::Pathname.new(file).freeze
  end
  parse!
end

Public Instance Methods

to_h() click to toggle source

@return [Hash]

# File lib/kamaze/version.rb, line 56
def to_h
  parsed.clone.freeze
end
to_path() click to toggle source

Return the path as a String.

@see ruby-doc.org/stdlib-2.5.0/libdoc/pathname/rdoc/Pathname.html#method-i-to_path @return [String]

# File lib/kamaze/version.rb, line 64
def to_path
  file.to_s
end
to_s() click to toggle source

@raise [NameError] @return [String]

# File lib/kamaze/version.rb, line 51
def to_s
  [major, minor, patch].map(&:to_i).join('.')
end
valid?() click to toggle source

Denote version is semantically valid

@return [Boolean]

# File lib/kamaze/version.rb, line 43
def valid?
  !!(/^([0-9]+\.){2}[0-9]+$/ =~ self.to_s)
rescue ::NameError
  false
end

Protected Instance Methods

attr_set(attr_name, attr_value) click to toggle source

Define attribute (as “ro“ attr) and set value.

@param [String|Symbol] attr_name @param [Object] attr_value @return [Array|nil]

# File lib/kamaze/version.rb, line 113
def attr_set(attr_name, attr_value)
  attr_name = Dry::Inflector.new.underscore(attr_name.to_s)

  return nil unless eligible_attr?(attr_name)

  self.singleton_class.class_eval do
    attr_accessor attr_name
    # rubocop:disable Style/AccessModifierDeclarations
    protected "#{attr_name}="
    # rubocop:enable Style/AccessModifierDeclarations
  end

  self.__send__("#{attr_name}=", attr_value.freeze)

  [attr_name, attr_value.freeze].freeze
end
eligible_attr?(attr_name) click to toggle source

Denote given attr name is eligible (to be set)

@param [String|Symbol] attr_name @return [Boolean]

# File lib/kamaze/version.rb, line 134
def eligible_attr?(attr_name)
  [attr_name, "#{attr_name}="].each do |v|
    return false if self.respond_to?(v, true)
  end

  true
end
file_from(locations = caller_locations) click to toggle source

Get file automagically

@param [Array] locations @return [Pathname]

# File lib/kamaze/version.rb, line 79
def file_from(locations = caller_locations)
  location = locations.first.path

  Pathname.new(location).dirname.realpath.join('version.yml')
end
parse(file) click to toggle source

Parse given file

@param [Pathname] file @raise [Psych::DisallowedClass] @return [Hash]

# File lib/kamaze/version.rb, line 90
def parse(file)
  YAML.safe_load(file.read) || {}
rescue Errno::ENOENT
  {}
end
parse!() click to toggle source

Parse and set attributes

@return [self]

# File lib/kamaze/version.rb, line 99
def parse!
  @parsed = self.parse(self.file)
                .map { |k, v| self.attr_set(k, v) }
                .compact
                .to_h

  self
end