class Kamaze::Version
Describe version using a YAML file.
Constants
- VERSION
Attributes
Get filepath used to parse version (YAML file).
@return [Pathname|String]
Get parsed data
@return [Hash]
Public Class Methods
@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
@return [Hash]
# File lib/kamaze/version.rb, line 56 def to_h parsed.clone.freeze end
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
@raise [NameError] @return [String]
# File lib/kamaze/version.rb, line 51 def to_s [major, minor, patch].map(&:to_i).join('.') end
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
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
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
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 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 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