Class: Kamaze::Version
- Inherits:
-
Object
- Object
- Kamaze::Version
- Defined in:
- lib/kamaze/version.rb,
lib/kamaze/version.rb,
lib/kamaze/version/version.rb
Overview
Describe version using a YAML file.
Constant Summary collapse
- VERSION =
self.new.freeze
Instance Attribute Summary collapse
-
#file ⇒ Pathname|String
readonly
Get filepath used to parse version (YAML file).
-
#parsed ⇒ Hash
readonly
protected
Get parsed data.
Instance Method Summary collapse
-
#attr_set(attr_name, attr_value) ⇒ Array|nil
protected
Define attribute (as
roattr) and set value. -
#eligible_attr?(attr_name) ⇒ Boolean
protected
Denote given attr name is eligible (to be set).
-
#file_from(locations = caller_locations) ⇒ Pathname
protected
Get file automagically.
-
#initialize(file = nil) ⇒ Version
constructor
A new instance of Version.
-
#parse(file) ⇒ Hash
protected
Parse given file.
-
#parse! ⇒ self
protected
Parse and set attributes.
- #to_h ⇒ Hash
-
#to_path ⇒ String
Return the path as a String.
- #to_s ⇒ String
-
#valid? ⇒ Boolean
Denote version is semantically valid.
Constructor Details
#initialize(file = nil) ⇒ Version
33 34 35 36 37 38 |
# 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 |
Instance Attribute Details
#file ⇒ Pathname|String (readonly)
Get filepath used to parse version (YAML file).
30 31 32 |
# File 'lib/kamaze/version.rb', line 30 def file @file end |
#parsed ⇒ Hash (readonly, protected)
Get parsed data
73 74 75 |
# File 'lib/kamaze/version.rb', line 73 def parsed @parsed end |
Instance Method Details
#attr_set(attr_name, attr_value) ⇒ Array|nil (protected)
Define attribute (as ro attr) and set value.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# 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) ⇒ Boolean (protected)
Denote given attr name is eligible (to be set)
134 135 136 137 138 139 140 |
# 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) ⇒ Pathname (protected)
Get file automagically
79 80 81 82 83 |
# 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) ⇒ Hash (protected)
Parse given file
90 91 92 93 94 |
# File 'lib/kamaze/version.rb', line 90 def parse(file) YAML.safe_load(file.read) || {} rescue Errno::ENOENT {} end |
#parse! ⇒ self (protected)
Parse and set attributes
99 100 101 102 103 104 105 106 |
# 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 |
#to_h ⇒ Hash
56 57 58 |
# File 'lib/kamaze/version.rb', line 56 def to_h parsed.clone.freeze end |
#to_path ⇒ String
Return the path as a String.
64 65 66 |
# File 'lib/kamaze/version.rb', line 64 def to_path file.to_s end |
#to_s ⇒ String
51 52 53 |
# File 'lib/kamaze/version.rb', line 51 def to_s [major, minor, patch].map(&:to_i).join('.') end |
#valid? ⇒ Boolean
Denote version is semantically valid
43 44 45 46 47 |
# File 'lib/kamaze/version.rb', line 43 def valid? !!(/^([0-9]+\.){2}[0-9]+$/ =~ self.to_s) rescue ::NameError false end |